473,395 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Placement of initialized data

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

Sep 3 '07 #1
1 1996
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.

ma**************@gmail.com wrote:
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
Sep 3 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
5
by: ma740988 | last post by:
I've got a struct which is comprised of POD types. I know in advance where in memory the struct resides. To make a long story short, I'm doing transfers of 16 bit ADC sampled data between...
4
by: sreedhar.cs | last post by:
Hi all, In my application,I want to place a vector in a specific location in shared memory.(a user supplied pointer). I understand that the STL allocator mechanism places the data objects within...
15
by: mangesh | last post by:
This code is from c++ faq in section 11 : void someCode() { char memory; void* p = memory; Fred* f = new(p) Fred(); f->~Fred(); // Explicitly call the destructor for the placed object }
5
by: Lagarde Sébastien | last post by:
Hello, I write code to debug new call with following macro: #define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-), FALSE) ? NULL : new The setOwner allow to save the...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
9
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.