473,785 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,0x0 2});
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 2013
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("sectio n_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,0x0 2});
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
4497
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 makes sense that if you have virtual destructors, they are eventually used in the explicit destructor call when using the placement new semantic: class A {
4
4739
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 14882:2003(E) §8.5 says:   To zero-initialize an object of type T means: 5   -- if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
20
4155
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
1519
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 'cards'. At the front end I configure the memory such that - say card 1 will transfer data to card 2 at a specified region. A header is accompanied with each data transfer. So now lets say card 1 transferred the header to location 0xD0000000 of...
4
2649
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 the STL vector in a user specified location in memory.Still the STL container (vector) resides only within the process address space. But I would want my STL container class(the vector skeleton as such) as well as the data objects to be placed in...
15
4664
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
5833
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 current file, line and function: setOwner(const char *file, const u32 line, const char *func) {
15
5026
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 - but for the sake of this post ... I'm searching for an answer that assumes said decision. If I allocate memory in the class declaration: char buffer;
9
3297
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 the construction of object using 'placement new'. So, Effectively it should be taking more time . What do you think ? I think,
0
9483
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.