2

I'm having an interesting problem initialising an array within my header.

I have:

static u32 TxBuffer_Data[MAX_PKT_LEN_WORDS] = { 10 };
static u32 RxBuffer_Data[MAX_DMA_RX_FIFOMODE_WORDS] = { 0 };

Now, I want both to be within the .data section of an embedded processor, i.e. allocated at compile time and initialised, ideally to zeros.

Now, the syntax here is correct as per How to initialize all members of an array to the same value?.

When I run my code, I grab the addresses of these two buffers, the Txbuffer is indeed within the .data region, however the RxBuffer is within .bss which is reserved for non-initialised compile time allocated variables. If I change that { 0 } to { 10 } the RxBuffer is put into the .data section correctly.

Why can't I initialise data to zeros and still have it defined as initialised?

Thanks. Ed

2
  • 2
    What compiler are you using? What optimization flags do you have? Maybe, because you initialize the whole array to zero, and the BSS is also initialized to zero, it made a small "optimization" by putting the array in BSS? Commented Mar 31, 2016 at 14:33
  • 1
    Better not to put them in a header anyway but in the module itself, with extern declarations in the header. Commented Mar 31, 2016 at 14:43

1 Answer 1

2

Data in the bss section is zero-initialized. Declaring the array as:

static u32 RxBuffer_Data[MAX_DMA_RX_FIFOMODE_WORDS] = { 0 };

or

static u32 RxBuffer_Data[MAX_DMA_RX_FIFOMODE_WORDS];

is actually the same. In both cases the array is zero-initialized and most likely the compiler will place the array in bss.

Sign up to request clarification or add additional context in comments.

4 Comments

Specifically the flags are: 'Invoking: MicroBlaze gcc compiler' mb-gcc -Wall -O0 -g3 -c -fmessage-length=0 -I../../XpsMBprjFlitesHub2_bsp_standalone_2/microblaze_0/include -mno-xl-reorder -mlittle-endian -mcpu=v8.50.c -mno-xl-soft-mul -Wl,--no-relax -ffunction-sections -fdata-sections -MMD -MP -MF"src/FLITES_HUB_MB_Main.d" -MT"src/FLITES_HUB_MB_Main.d" -o "src/FLITES_HUB_MB_Main.o" "../src/FLITES_HUB_MB_Main.c"
The reason for the confusion is that the Xilinx documentation states that: ".bss This section contains un-initialized data. This section has the w (read-write) flag and must be mapped to RAM." However as you mentioned, the .bss section is zero-initialised therefore initialised.
Just for additional info, placing the array in bss instead of data section is an optimization, variables in the data section require the corresponding initialization values in the binary image, whereas variables in the bss don't. In your case, placing RxBuffer_Data in bss instead of data section saves 4 * MAX_DMA_RX_FIFOMODE_WORDS bytes of flash
@E.Fisher The BSS contains data that you don't initialize (and apparently initialize to zero), it's the compiler runtime support code that "initializes" the BSS. It's an important distinction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.