Where are constant variables stored in C?

Viewed 92279

I wonder where constant variables are stored. Is it in the same memory area as global variables? Or is it on the stack?

14 Answers

offcourse not , because

1) bss segment stored non inilized variables it obviously another type is there.

       (I) large static and global and non constants and non initilaized variables it stored .BSS section.

       (II) second thing small static and global variables and non constants and non initilaized variables stored in .SBSS section this included in .BSS segment.

2) data segment is initlaized variables it has 3 types ,

      (I) large static and global and initlaized and non constants variables its stord in .DATA section.
      (II) small static and global and non constant and initilaized variables its stord in .SDATA1 sectiion.
     (III) small static and global and  constant and initilaized OR non initilaized variables its stord in .SDATA2 sectiion.

i mention above small and large means depents upon complier for example small means < than 8 bytes and large means > than 8 bytes and equal values.

but my doubt is local constant are where it will stroe??????

This is specific to Win32 systems.

enter image description here

It's compiler dependence but please aware that it may not be even fully stored. Since the compiler just needs to optimize it and adds the value of it directly into the expression that uses it.

I add this code in a program and compile with gcc for arm cortex m4, check the difference in the memory usage.

Without const:

int someConst[1000] = {0};

enter image description here

With const:

const int someConst[1000] = {0};

enter image description here

I checked on x86_64 GNU/Linux system. By dereferencing the pointer to 'const' variable, the value can be changed. I used objdump. Didn't find 'const' variable in text segment. 'const' variable is stored on stack. 'const' is a compiler directive in "C". The compiler throws error when it comes across a statement changing 'const' variable.

Related