Placement of initialized data

  • Thread starter matthias.kessler
  • Start date
M

matthias.kessler

I've got an issue understanding where my compiler places data. I'm
developing for an embedded target, so using as little RAM as possible
is very important. Anonymous strings are stored in ROM and I was
looking for a way of doing the same for anonymous arrays. Using the
gcc on a windows machine gave the results I expected, both fpData of
Foo and Bar are stored in the same location. When I switched to the
target compiler however, the behaviour was quite different. The string
still was in ROM, but the array was copied to the stack.

The following code shows what I'm trying to do:

typedef unsigned char uint8;

class Foo
{
public:
Foo(const char* data) :
//the assembly simply initializes fpData with the
location in ROM
fpData(data)
{
printf("Foo: %p\n", fpData);
}
private:
const char* fpData;
};

class Bar
{
public:
Bar(const uint8* data) :
//the assembly copies the whole array to the stack and
then initializes
//fpData with the location on the stack
fpData(data)
{
printf("Bar: %p\n", fpData);
}
private:
const uint8* fpData;
};

int main(int argc, char* argv[])
{
Foo f("some string");
Bar b((const uint8[]){0x00,0x01,0x02});
printf("&Foo: %p\n", &f);
printf("&Bar: %p\n", &b);
return 0;
}

My assumtion was that the compiler should treat both, the string and
the anonymous array, the same way as initialized const data. Is there
any reason why it would behave differently for the anonymous array?

Thanks in advance

matthias
 
A

Alf P. Steinbach

* (e-mail address removed):
int main(int argc, char* argv[])
{
Foo f("some string");
Bar b((const uint8[]){0x00,0x01,0x02});
printf("&Foo: %p\n", &f);
printf("&Bar: %p\n", &b);
return 0;
}

My assumtion was that the compiler should treat both, the string and
the anonymous array, the same way as initialized const data. Is there
any reason why it would behave differently for the anonymous array?

"Anonymous arrays" are not part of standard C++, so the compiler can do
anything it wants. Where data is placed is not part of standard C++, so
the compiler can do anything it wants. In short, the compiler can do
anything it wants.

In order to get more predictable behavior, consider using standard C++:

static unsigned char const data[] = {0x00,0x01,0x02};
int main()
{
Foo f( "some string" );
Bar b( data );
...
}

Perhaps that will even make the compilers you're using, place the data
in ROM.

Cheers, & hth.,

- Alf
 
L

Laurent D.A.M. MENTEN

I suggest you investigate in linker scripts (this will give you control
on the physical layout of the binary file) and the gcc extentions that
allow you to place a piece of data in an arbitrary section (something like

char data[1024] __attribute__((section("section_name"))) = {0};

if I remember well). All of this is described in ld and gcc info files
respectively.
 
M

matthias.kessler

* (e-mail address removed):


int main(int argc, char* argv[])
{
Foo f("some string");
Bar b((const uint8[]){0x00,0x01,0x02});
printf("&Foo: %p\n", &f);
printf("&Bar: %p\n", &b);
return 0;
}
My assumtion was that the compiler should treat both, the string and
the anonymous array, the same way as initialized const data. Is there
any reason why it would behave differently for the anonymous array?

"Anonymous arrays" are not part of standard C++, so the compiler can do
anything it wants. Where data is placed is not part of standard C++, so
the compiler can do anything it wants. In short, the compiler can do
anything it wants.

In order to get more predictable behavior, consider using standard C++:

static unsigned char const data[] = {0x00,0x01,0x02};
int main()
{
Foo f( "some string" );
Bar b( data );
...
}

Perhaps that will even make the compilers you're using, place the data
in ROM.

Yes, I tried this before and it works fine, as expected. The other
solution is somewhat "syntactic sugar" for my use case.
Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top