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
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