Tuesday, November 15, 2011

Process Memory and Pointers

Friends, here are the two questions for today !

1. What is the size of a pointer in C ?
     That's a loaded question. The size of a pointer is based on the underlying machine. A 16-bit machine will have pointers of size 2 bytes. Likewise, 32-bit machines will have pointers of size 4 bytes. Can you guess the size of a pointer on a 64-bit machine? Note that this is not necessarily true on all the systems, there are a few exceptions, but this answer should generally hold good.
     Another point to note is that size of the pointer doesn't depend on what it is pointing to. For instance, an integer pointer and a character pointer will have the same size on a machine.

2. How is the process memory organized ?
    Simple answer is that it depends on the architecture on which the process is getting executed. However, the general organization of the process memory is given below for UNIX, while other systems have some mutated versions of the same:
 

Text
Data
BSS
Heap

Stack


Text : Contains executable read-only data
Data: Contains initialized global and static variables used by the program
BSS: Contains uninitialized global and static variables. Note that they are internally initialized to zero, but placed in BSS (Block Started by Symbol).
Heap: Dynamically allocated memory is taken out of this memory partition. All your malloc/realloc/free calls operate in this region.
Stack: Used to maintain function calls. More famously known as Program Stack. Note that Heap and Stack grow towards each other.

Click here for more information.

No comments:

Post a Comment