Memory allocation with gcc on AVR (Arduino)

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
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.

Re: Memory allocation with gcc on AVR (Arduino)
On 08/09/2016 11:55 AM, bitrex wrote:
Quoted text here. Click to load it

Sorry, that should be:

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


Re: Memory allocation with gcc on AVR (Arduino)
On 09/08/16 17:55, bitrex wrote:
Quoted text here. Click to load it

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?

Quoted text here. Click to load it

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

Quoted text here. Click to load it


Re: Memory allocation with gcc on AVR (Arduino)
On 08/09/2016 05:55 PM, bitrex wrote:
Quoted text here. Click to load it
It `should' work...
[..]
Quoted text here. Click to load it

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.  


Re: Memory allocation with gcc on AVR (Arduino)
On 9.8.16 18:55, bitrex wrote:
Quoted text here. Click to load it


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




Site Timeline