Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What all local variables goto Data/BSS segment?

Tags:

c++

c

nm

The man page of nm here: MAN NM says that

The symbol type. At least the following types are used; others are, as well, depending on the object file format. If lowercase, the symbol is usually local; if uppercase, the symbol is global (external)

And underneath it has "b" and "B" for "uninitialized data section (known as BSS)" and "d" and "D" for "initialized data section"

But I thought local variables always goto Stack/Heap and not to "Data" or "BSS" sections. Then what local variables is nm talking about?

like image 235
Kartik Anand Avatar asked Oct 21 '25 13:10

Kartik Anand


2 Answers

"local" in this context means file scope.

That is:

static int local_data = 1; /* initialised local data */
static int local_bss; /* uninitialised local bss */
int global_data = 1; /* initialised global data */
int global_bss; /* uninitialised global bss */

void main (void)
{
   // Some code
}
like image 145
kaylum Avatar answered Oct 23 '25 04:10

kaylum


Function-scope static variables go in the data or BSS (or text) sections, depending on initialization:

void somefunc(void)
{
    static char array1[256] = "";            // Goes in BSS, probably
    static char array2[256] = "ABCDEF…XYZ";  // Goes in Data
    static const char string[] = "Kleptomanic Hypochondriac";
                                             // Goes in Text, probably
    …
}

Similar rules apply to variables defined at file scope, with or without the static storage class specifier — uninitialized or zero-initialized data goes in the BSS section; initialized data goes in the Data section; and constant data probably goes in the Text section.

like image 27
Jonathan Leffler Avatar answered Oct 23 '25 02:10

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!