"new" in C++ does not create code on the heap

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

"new" in C++ does not create code on the heap

683 Views
donden
Contributor II

I have created a C++ project in Codewarrior 10.3 targeting Kinetis K61FN1M0VMJ12.  I am attempting to do a Bootloader that will reprogram Flash in the field.  I need to instantiate my Flash class in RAM so it can perform flash erase and write operations.  However, using "new" only creates a RAM pointer to the flash-based code.  How can I get the class (about 1200 bytes long) onto the heap?

I am using "new" in C++ with the intent of creating a copy of a class on the heap. The location on the heap is necessary for hardware reasons in writing to Flash memory.  I am using:

static Watchdog *wd;

Flash    *flash;

wd = new(std::nothrow) Watchdog;

flash = new(std::nothrow) Flash();

The problem is that the only thing that ends up in RAM is the pointer.

I am using gnu C++ compiler chain on eclipse for Codewarrior.  Does anyone know of a compiler directive that is causing the problem, or that would actually create an instance on the stack?  All the manuals I have indicate that “new” should instantiate the object on the heap.

BTW, Watchdog *wd is used by  various functions in the main.cpp file.  Flash *flash is used in two or three files that need special access to Flash registers in the processor.

Regards,

Don DenTandt

Labels (1)
Tags (3)
0 Kudos
2 Replies

530 Views
JerryFan
NXP Employee
NXP Employee

1. The pointer flash pointed to heap after the newing?

2. It is expected that the new will malloc mem for the class on heap and then call the class's construct function. And it will not "creating a copy of a class on the heap" unless you do it in the construct function

530 Views
donden
Contributor II

The pointer *flash was created on the heap, but it points to the original code in flash memory.

I will try creating that RAM copy myself to see how/if it works for me.

Thank you.

0 Kudos