Programming in C: Unit III (b): Pointers

Understanding the Computer's Memory

Pointers

Every computer has a primary memory. All data and programs need to be placed in the primary memory for execution. RAM is a collection of memory locations (often known as cells) and each location has a specific address.

Unit III : Functions and Pointers

CHAPTER 8 : POINTERS

Takeaways

• Pointer expressions

• Pointers with functions

• Arrays of pointers

• Pointers to pointers

• Pointer arithmetic

• Pointers with arrays

• Pointers with 2D and 3D arrays

• Dynamic memory allocation                             

• Null and generic pointers

• Pointers with strings

• Function pointers


UNDERSTANDING THE COMPUTER'S MEMORY

Every computer has a primary memory. All data and programs need to be placed in the primary memory for execution. RAM (Random Access Memory which is a part of the primary memory) is a collection of memory locations (often known as cells) and each location has a specific address. Each memory location is capable of storing 1 byte of data (though new computers are able to store 2 bytes of data but in this book we have been talking about locations storing 1 byte of data). Therefore, a char type data needs just 1 memory location, an int type data needs 2 memory locations. Similarly, float and double type data need 4 and 8 memory locations, respectively.

Generally, the computer has three areas of memory each of which is used for a specific task. These areas of memory include-stack, heap, and global memory.

Stack A fixed size of memory called system stack is allocated by the system and is filled as needed from the bottom to the top, one element at a time. These elements can be removed from the top to the bottom by removing one element at a time, i.e., the last element added to the stack is removed first.

When the program has used the variables or data stored in the stack, it can be discarded to enable the stack to be used by other programs to store their data. We have already read a little bit on the system stack in Chapter 4 when we discussed Recursion. We will read more about them in the chapter on Stacks.

Note

System stack is the section of memory that is allocated for automatic variables within functions.

Heap It is a contiguous block of memory that is available for use by programs when the need arises. A fixed size heap is allocated by the system and is used by the system in a random fashion.

The addresses of the memory locations in heap that are not currently allocated to any program for use are stored in a free list. When a program requests a block of memory, the dynamic allocation technique (discussed at the end of this chapter) takes a block from the heap and assigns it to the program. When the program has finished using the block, it returns the memory block to the heap and the addresses of the memory locations in that block are added to the free list.

Compared to heaps, a stack is faster but smaller and expensive. When a program begins execution with the main() function, all variables declared within main() are allocated space on the stack. Moreover, all the parametres passed to a called function are stored on the stack.

Global memory The block of code that is the main() program (along with other functions in the program) is stored in the global memory. The memory in the global area is allocated randomly to store the code of different functions in the program in such a way that one function is not contiguous to another function. Besides, the function code, all global variables declared in the program are stored in the global memory area.

Other memory layouts C provides some more memory areas such as text segment, BSS, and shared library segment.

• The text segment is used to store the machine instructions corresponding to the compiled program. This is generally a read-only memory segment.

• BSS (Block Started by Symbol) is used to store un- initialized global variables.

• Shared library segment contains the executable image of shared libraries that are being used by the program.

Programming in C: Unit III (b): Pointers : Tag: : Pointers - Understanding the Computer's Memory