Memory allocation with gcc on AVR (Arduino)

So the Arduino environment has the following template code when you start up a new project:

void setup() { //code to be run at startup }

void loop() { //run forever }

Suppose I define a class at the top of my "sketch" like so:

struct Foo { void do_it() { return 1; } };

I then try to create a "global" instance of the class outside the "setup" section:

Foo foo;

This compiles fine, except that when you attempt to use "do_it" from within the "loop" section the code doesn't seem to execute properly.

If I do the following:

Foo *foo;

void setup() { foo = new Foo; }

It works as expected, but then...something? is being placed into heap memory. My expectation would be that on a Harvard architecture uP, the only things being put on the heap when a new object is instantiated would be the non-const class member variables with static storage duration, as the code has its own program memory, but that doesn't appear to be what is happening.

Any insight into what avr-gcc is actually doing in this case would be appreciated.

Reply to
bitrex
Loading thread data ...

Sorry, that should be:

struct Foo { int do_it() { return 1; } };

Reply to
bitrex

What do you mean "the code doesn't seem to execute properly" ? The function merely returns 1, so there is not much to go wrong. Can you post the entire code?

The struct has size 1, even though it has no data members (as 1 is the minimum size a struct/class can have).

Reply to
David Brown

It `should' work... [..]

Have a look at the assembler output. If that thing is just a wrapper for gcc, it should be easy to get at. AFAIR there were two variants. one for each file and one representing the final binary. You 'll want the latter, so you can check if the init code is doing sthg funny. Map file may be helpful, too.

Reply to
Johann Klammer

To see the assembly code of the whole program:

avr-objdump -D myfile.elf >myfile.dis

Substitute the linked file name instead of myfile.

To see the assembly code generated for a module by gcc, add to gcc command line: '-Wa,-adhlms=mymodule.lst'.

Substitute the module name instead of mymodule. You can leave the quotes out if the file name does not contain spaces or other weird.

If you're using make you can use in the compile rule:

-Wa,-adhlms=$(@:.o=.lst)

--

-TV
Reply to
Tauno Voipio

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.