Memory and pointers in C/C++

Structure in computer memory.



This is the sectionof memory used for local variable storage. Every time you call a function, all of the function’s local variables get created on the stack. It’s called the stack because it’s like a stack of plates: variables get added to the stack when you enter a function, and get taken off the stack when you leave. Weird thing is, the stack actually works upside down. It starts at the top of memory and grows downward.



This is a section of memory we haven’t really used yet. The heap is for dynamic memory:pieces of data that get created when the program is running and then hang around a long time


A global variable is a variable that lives outside all of the functions and is visible to all of them. Globals get created when the program first runs, and you can update them freely.


Constants are also created when the program first runs, but they are stored in read-only memory. Constants are things like string literalsthat you will need when the program is running, but you’ll never want them to change.


Finally, the code segment. A lot of operating systems place the code right down in the lowest memory addresses. The code segment is also read-only. This is the part of the memory where the actual assembled code gets loaded.

Note 1

  • Variables are allocated storage in memory.
  • Local variables live in the stack.
  • Global variables live in the globals section
  • Pointers are just variables that store memory addresses.
  • The &operator finds the address of a variable.
  • The *operator can read the contents of a memory address
  • The *operator can also set the contents of a memory address.

Note 2

  • An array variable can be used as a pointer.
  • The array variable points to the first  element in the array.
  • If you declare an array argument to a function, it will be treated as a pointer
  • The sizeof operator returns the space taken by a piece of data.
  • You can also call sizeoffor a data type, such as sizeof(int).
  • sizeof(a pointer)returns 4 on 32-bit operating systems and 8 on 64-bit.

Note 3

  • Array variables can be used as pointers…but array variables are not quite the same.
  • Sizeof is different for array and pointer variables.
  • Array variables can’t point to anything else.
  • Passing an array variable to a pointer decays it
  • Arrays start at zero because of pointerarithmetic
  • Pointer variables have types so they can adjust pointer arithmetic.

Note 4

  • If you see a *in a variable declaration, it means the variable will be a pointer.
  • String literalsare stored in read-only memory.
  • If you want to modify a string, you need to make a copy in a new array.
  • You can declare a charpointer as const char *to prevent the code from using it to modify a string.



References : Head first C


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: