473,404 Members | 2,178 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,404 software developers and data experts.

Array size depending on symbol address

Hello,

I am running my code on an embedded platform without OS. I have defined some
data in a section called .eeprom. The section is defined by the linker
script and starts at address zero. The symbol __eeprom_end is defined by the
linker script as well, and lies at the end of this section. The address of
__eeprom_end is thus equal to the number of bytes in the .eeprom section.

My problem : I want to declare an array in .bss with the same size of this
section. My naive approch was :

extern void *_eeprom;
char eeprom_shadow[(int)&_eeprom_end];

This fails miserably, ofcourse : the compiler doesn't know the address of
__eeprom before the linker is done, so it can not declare the array.

Is there a trick to allocate the memory for this array at compile time ? I'd
rather not use malloc() and friends, since I am working on a platform with a
very small amount of memory, and don't like using dynamic memory here.

Thanks,
--
:wq
^X^Cy^K^X^C^C^C^C
Nov 29 '05 #1
4 2018
>I am running my code on an embedded platform without OS. I have defined some
data in a section called .eeprom. The section is defined by the linker
script and starts at address zero. The symbol __eeprom_end is defined by the
linker script as well, and lies at the end of this section. The address of
__eeprom_end is thus equal to the number of bytes in the .eeprom section.

My problem : I want to declare an array in .bss with the same size of this
section. My naive approch was :

extern void *_eeprom;
char eeprom_shadow[(int)&_eeprom_end];

This fails miserably, ofcourse : the compiler doesn't know the address of
__eeprom before the linker is done, so it can not declare the array.

Is there a trick to allocate the memory for this array at compile time ? I'd
rather not use malloc() and friends, since I am working on a platform with a
very small amount of memory, and don't like using dynamic memory here.


You have a fundamental chicken-or-the-egg problem here. A change
in the size of the array can change the size of the code (because,
for example, instructions can have short or long offsets), which
can change the size of the .eeprom section, which can change the
size of the array, which can change the size of the code AGAIN,
which can change the size of the .eeprom section AGAIN, ad nauseum.

Gordon L. Burditt
Nov 29 '05 #2
>>I am running my code on an embedded platform without OS. I have defined some
data in a section called .eeprom. The section is defined by the linker
script and starts at address zero. The symbol __eeprom_end is defined by the
linker script as well, and lies at the end of this section. The address of
__eeprom_end is thus equal to the number of bytes in the .eeprom section.

My problem : I want to declare an array in .bss with the same size of this
section. My naive approch was :

extern void *_eeprom;
char eeprom_shadow[(int)&_eeprom_end];

This fails miserably, ofcourse : the compiler doesn't know the address of
__eeprom before the linker is done, so it can not declare the array.

Is there a trick to allocate the memory for this array at compile time ? I'd
rather not use malloc() and friends, since I am working on a platform with a
very small amount of memory, and don't like using dynamic memory here.


You have a fundamental chicken-or-the-egg problem here. A change
in the size of the array can change the size of the code (because,
for example, instructions can have short or long offsets), which
can change the size of the .eeprom section, which can change the
size of the array, which can change the size of the code AGAIN,
which can change the size of the .eeprom section AGAIN, ad nauseum.

Gordon L. Burditt


Hi Gordon,

Yes, this would be a problem if my the rest of my code would change anything
to the .eeprom section. This is not the case however, since I specifically
declare some variables with attributes to place them in the .eeprom section,
and no other data will go there. (The rest is in the usual places, .text,
..data, .bss)

--
:wq
^X^Cy^K^X^C^C^C^C
Nov 29 '05 #3
us****@zevv.nl wrote:
Hello,

I am running my code on an embedded platform without OS. I have defined some
data in a section called .eeprom. The section is defined by the linker
script and starts at address zero. The symbol __eeprom_end is defined by the
linker script as well, and lies at the end of this section. The address of
__eeprom_end is thus equal to the number of bytes in the .eeprom section.

My problem : I want to declare an array in .bss with the same size of this
section. My naive approch was :

extern void *_eeprom;
char eeprom_shadow[(int)&_eeprom_end];

This fails miserably, ofcourse : the compiler doesn't know the address of
__eeprom before the linker is done, so it can not declare the array.

Is there a trick to allocate the memory for this array at compile time ?


Your identifiers are all over each other. Is _eeprom the address of the
section .eeprom? Is _eeprom_end a typo or intended to be the same as
__eeprom_end? What is __eeprom with two underscores?

In any case, as you say, all this information is available at link time.
There is simply no way to turn these symbols or their addresses into
compile-time constants if you don't already have the information available,
and global array sizes must be compile-time constants.

Any reason why you can't have the linker declare a section .eeprom_shadow of
equal size and use the address of that? Indexing would work the same; the
only restriction is that you still don't know the size at compile time, but
that shouldn't be too much of a problem, since you'd have __eeprom_end (or
_eeprom_end) available at runtime.

S.
Nov 29 '05 #4
Skarmander <in*****@dontmailme.com> wrote:
us****@zevv.nl wrote:
Hello,

I am running my code on an embedded platform without OS. I have defined some
data in a section called .eeprom. The section is defined by the linker
script and starts at address zero. The symbol __eeprom_end is defined by the
linker script as well, and lies at the end of this section. The address of
__eeprom_end is thus equal to the number of bytes in the .eeprom section.

My problem : I want to declare an array in .bss with the same size of this
section. My naive approch was :

extern void *_eeprom;
char eeprom_shadow[(int)&_eeprom_end];

This fails miserably, ofcourse : the compiler doesn't know the address of
__eeprom before the linker is done, so it can not declare the array.

Is there a trick to allocate the memory for this array at compile time ?
Your identifiers are all over each other. Is _eeprom the address of the
section .eeprom? Is _eeprom_end a typo or intended to be the same as
__eeprom_end? What is __eeprom with two underscores?


Yes, definitely all over eachother, very confusing, I'm sorry. It should
read '__eeprom_end' everywhere.
Any reason why you can't have the linker declare a section .eeprom_shadow of
equal size and use the address of that? Indexing would work the same; the
only restriction is that you still don't know the size at compile time, but
that shouldn't be too much of a problem, since you'd have __eeprom_end (or
_eeprom_end) available at runtime.


Yes, having the linker create another section should work ofcourse, thanks
for the hint,

--
:wq
^X^Cy^K^X^C^C^C^C
Nov 29 '05 #5

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

Similar topics

5
by: Paul C-T | last post by:
Hi, Am I trying to be too clever here? I am trying to write a PHP page to enable me to enter values into a form then write those values to a text file. I want to use the form & table that...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
8
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
11
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I've noticed a few threads (full of sound and fury, signifying nothing) here recently about allocation of large memory blocks. I'm about to start on a personal pet project where I'll be using...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
8
by: Ben | last post by:
Hi, I am having trouble debugging a segmentation fault...here's my data structure: typedef struct CELL *pCELL; /* Pointers to cells */ struct CELL { SYMBOL symbol; pCELL prev_in_block;...
49
by: vfunc | last post by:
If I have a large array 10,000+ elements then how do I reserve memory for this ? Currently I get a segmentation fault. Dynamic reservation is good, but allowing a chunk for the program is an...
11
by: rayreeves | last post by:
How do I declare an array of references to the elements of an array of values? Ray
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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...
0
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...

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.