Connecting Tech Pros Worldwide Forums | Help | Site Map

Placement of initialized data

matthias.kessler@gmail.com
Guest
 
Posts: n/a
#1: Sep 3 '07
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


Laurent D.A.M. MENTEN
Guest
 
Posts: n/a
#2: Sep 3 '07

re: Placement of initialized data


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.

matthias.kessler@gmail.com wrote:
Quote:
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
>
Closed Thread


Similar C / C++ bytes