Jump to content

Strorage classes

- - - - -

  • Please log in to reply
No replies to this topic

#1
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
In C code you may encounter the following type specifiers:
const

volatile

static
But what do they mean? Well, in your computer's memory there are several sections, called segments. There are 6 used by the C language: Data, rodata, bss, heap, stack, text. Each of these has a different way to put data in it.

float x; //bss: uninitialised space

float y = 2.0; //data: initialised space 

const float pi = 3.1415; //rodata: read only space

int main(){

     int i; //stack: local space

     static int j; //bss: uninitialised space

     static int k = 4; //data: initialised space

     char * s = malloc(20); //heap: dynamically allocated space

}

So, the rules are:
code goes in the text segment.
global stuff
goes in bss if not initialised
goes in data if initialised
goes in rodata if const.
local stuff
normally goes on the stack
if static
goes in bss if not initialised
goes in data if initialised
goes in rodata if const
dynamic allocations come from the heap.

Ja mata!
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users