Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Array size depending on symbol address

Reply
Thread Tools

Array size depending on symbol address

 
 
usenet@zevv.nl
Guest
Posts: n/a
 
      11-29-2005
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
 
Reply With Quote
 
 
 
 
Gordon Burditt
Guest
Posts: n/a
 
      11-29-2005
>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
 
Reply With Quote
 
 
 
 
usenet@zevv.nl
Guest
Posts: n/a
 
      11-29-2005
>>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
 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      11-29-2005
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.
 
Reply With Quote
 
usenet@zevv.nl
Guest
Posts: n/a
 
      11-29-2005
Skarmander <> wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(Encryption Package) error: cannot find symbol symbol: class BaseNCode clusardi2k@aol.com Java 6 08-29-2012 08:33 PM
Address of array && address of pointer to array Stanley Rice C Programming 33 09-20-2011 12:47 AM
Setting size of JFrame depending on display config bernd Java 0 08-03-2008 06:39 PM
Why ":symbol" failed but 'symbol' successed with JRuby 1.0.3? Song Ma Ruby 2 07-20-2008 04:08 AM
what's differnece between #ifdef symbol and #if defined(symbol) baumann@pan C Programming 1 04-15-2005 08:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57