Pointers from beginner to advance. PDF

Title Pointers from beginner to advance.
Author Gaurav Sharma
Course Computer science engneering
Institution Chandigarh University
Pages 15
File Size 664.4 KB
File Type PDF
Total Downloads 11
Total Views 141

Summary

This is the notes of pointers in c++ language....


Description

Pointers Q1 What is pointer? A pointer is a variable that holds a memory address, usually the location of another variable in memory The pointers are one of C++ most useful and strongest features. The correct understanding and use of pointers is critical to successful features programming in C++. Three reasons that support this are: 1. Pointers provide the means through which the memory location of a variable can be directly accessed and hence can be manipulated in the way as required. 2. Pointers support C++ dynamic allocation routines. 3. Pointers can improve the efficiency of certain routine. Pointers are one of the strongest and also one of the most dangerous and valuable features of C++. For instance, uninitialized, or wild pointers can cause your system to crash. Perhaps worse, it is easy to use pointers incorrectly, causing bugs that are very difficult to find. So, it because very much necessary to understand and grasp this important concept in to order to exploit its power and use without creating problems in a program.

Idea behind pointers Every byte in the computer's memory has an address. Addresses are numbers, just as your house numbers. Moreover, the address number start at 0 and go up from there - 1,2,3 and so on. Example, if you have 640kB memory example 640 X 1024bytes is 655360. bytes of memory, then the first byte will be having address 0, second as 1, third as 2 and so on. The highest address will be 655,359. A variable storing a memory address is called a pointer as it points to a specific memory location whose address it is storing under its name.

C++ Memory MAP

Pointers have a close relation with memory as they store address of a memory location and also they facilitate C++ dynamics memory allocation routines, we must understand the way C++ organize memory for its programs. After compiling a program C++ creates four logical distinct regions of memory that are used for four distinct specific functions. one region in the memory holds the compiled code of the program. Every instructions and every functions of the program start at a particular address. The next region is the memory area where the global variables of the program are stored. Global variables remain in the memory as long as program continues. The third region known as Stack, is used for great many of things while your program executes. The stack is used for holding the return addresses at function calls, arguments passed to the functions, and local variables for functions. The local variables remain in memory as long as the function continues and after that they are erased from the memory. The stack also stores the current state of the CPU. The Heap memory area is a region of free memory from which chunks of memory are allocated via C++'s dynamic memory allocation functions.

Dynamic and Static Allocation of Memory The golden rule of computers states that anything and everything (data or instruction) that needs to be processed must be loaded into internal memory before its processing takes place. There fore, every data and instruction that is being executed must be allocated some area in the main (internal) memory. The main (internal) memory is allocated in two ways: 1. Statically 2. Dynamically

Static Memory Allocation When the amount of memory to be allocated is known beforehand and the memory is allocated during compilation itself, it is referred to as static memory allocation. For instance, when you declare a variable as follows:

// Example short avar;

then, in such a case the computer knows that what the length of a short variable is Generally, it is 2 bytes. So, a chunk of 2 bytes internal memory will be allocated to variable avar during compilation itself. Thus, it is an example of static memory allocation

Dynamic Memory Allocation When the amount of memory to be allocated is not known beforehand rather it is required to allocate (main) memory as and when required during runtime (when the program is actually executing) itself, then, the allocation of memory at run time is referred to as dynamic memory allocation. C++ offers two operators for dynamic memory allocation-new and delete. The operator new allocates the memory dynamically and returns a pointer storing the memory address of the allocated memory. The operator delete deallocates the memory (the reverse of new) pointed by the given pointer. More about dynamic allocation and the operators new and delete, we'll learn in the coming sections in this chapter.

Free Store Free Store is a pool store of unallocated heap memory store given to a program that is used by the program for dynamic allocation during execution. Every program is provided with a pool of unallocated heap memory that it may utilize during execution. This pool of available memory is referred to as the program's free store. The allocated free store memory is unnamed. Objects allocated on the free store are manipulated indirectly through pointers. Another aspect of free store is that the allocated memory is uninitialized. The programmer is responsible for initializing it explicitly The free store memory is allocated through the use of new operator and deallocated through delete operator. Free store memory is (dynamically) allocated during run-time and static memory allocation takes place during compile-time. An object's life time as long as the object remains in the memory during the program execution, is known as an object's extent. Global variables or the variables having file scope are spoken of as having static extent. That means, the storage storage is allocated to them before the program's start-up and remains bound to the variable throughout the program execution. Variables having local scope are spoken of as having local extent. Storage is allocated to them at each entry in to the local scope (as soon as their local scope starts) and on exit (from the local scope ), the storage is freed up. A local variable with static specifier has static extent. They means, the dynamically allocation objects do not have any predefined scope. They remain in memory until explicitly removed using delete. Note

The storage allocated through the use of operator new remains bound to the object until it is

explicitly deallocated by the programmers.

Declaration and Initialization of Pointers Pointer variables are declared like normal variables except for the addition of the unary * character. The general form of a pointer declaration is as follows: type * var_name;

int * ptr //create an integer pointer iptr int * cptr //create an character pointer cpter int * fptr //create an float pointer fptr

When we say that iptr is an integer pointer, it means that the means that the memory location being pointed to by iptr can hold only integer values. In other word, it can be stated that iptr is a pointer to integer. Similarly in the above given example cptr, is a pointer to a character fptr is a pointer to a floating-point value. Thus, it is the base type of the pointer that defines what types of variables that pointer can point to. However , all the pointers arithmetic is done relative to its base type, so it is important to declare the pointers correctly. Technically any type of pointer can point anywhere in the memory. Two special operators * and & are used with pointers. The & is a unary operator that returns the memory address of its operands.

int i = 25; int * iptr; iptr = &i;

The above given assignment statement stores the memory address of I (& returns memory address of its operand) into iptr so that iptr is now pointing to the memory location of i. The expression &i will return an int pointer value because i is an integer thus, &i will return an int pointer value because I is an integer thus, &i will return a pointer to integer. Similarly, if ch is a character variable, then &ch will return a character pointer value.

#include using namespace std; int main(){ // &return memory address of its operand // *returns the value of the variable located at the address following it int val = 25; int *ptr = &val; cout...


Similar Free PDFs