Working With Pointers In C++

Working With Pointers In C++

One of the most powerful instruments offered in c++ interview questions for a programmer is pointers. Pointers in C++ are used to directly access totally different memory locations which might be allocated while a program is being compiled and executed. This provides an infinite quantity of flexibility, depth and power to the program being written and adds one other dimension to the item oriented paradigm.

Nevertheless, as we all know from our childhood superhero comics: With nice power comes great responsibility (God that sounded so cliché). Thus, pointers in C++ are a double-edged sword. A few of such pointer problems or pointer hazards are memory leak or inaccessible memory and dangling pointers.

This article covers some principal pointer hazards and the steps that should be taken by any C++ programmer to avoid such pointer hazards.

Pointer Hazards

The primary pointers hazards are:

1. Memory Leak
2. Dangling Pointer
3. Bad pointer

Memory Leak:
Memory leaks are a common incidence in many C++ codes. Basically, this happens when a pointer, already pointing at a memory block, to illustrate 'A', is reallocated to a different memory block, say, 'B'. As the one way to access allocated memory blocks directly is thru pointers, the primary location, A, now becomes fully inaccessible memory, leading to a memory leak. As the compiler doesn't de-allocate memory by itself (the programmer has to make use of the delete keyword) and as our pointer is now pointing towards memory location B, this causes wastage of memory and assets of the system being used.

int *ptr = nullptr;
ptr = new int;
*ptr = 5;
ptr = new int;

Here, the memory house that stores the integer '5' now cannot be accessed because the pointer 'ptr' is pointing towards another memory location now. As a result the area that the system makes use of for storing one integer is wasted till the end of program execution.

As a rule of thumb to avoid memory leak, all the time ensure that the number of 'delete' and 'new' keywords in your program are equal, as every time you allocate a new memory using 'new', it is advisable to de-allocate it using 'delete'.

Dangling Pointer:
A dangling pointer is a pointer which points to a location in memory that's not part of your program. It occurs when a programmer fails to point a pointer towards one other memory location after using the 'delete' statement.

int *ptr = new int;
*ptr = 10;
delete ptr;
cout *pr;

Discover that here, the integer memory that was allocated to the pointer has been deleted but we're still attempting to dereference the pointer and output its value. It could lead to very critical conditions in your program leading to an infinite loop or a program crash.

Bad pointer:
All the time initialize a pointer when creating it, in any other case it could develop into a bad pointer and level to any arbitrary location within the memory. Leaving this pointer uninitialized could lead to system crashes. As an uninitialized pointer factors to a random memory location, this location could very well be a memory location previously used by another pointer, leading to main problems in the program.

int *ptr;
cout *pr;

Normally you need to first initialize it to a NULL value and some other worth later in your program. The new customary has created the keyword 'nullptr' for this purpose.

int * ptr = nullptr;

It's simple to see that pointers in C++ ought to be handled with extreme care and caution. There are lots of prospects which will lead to crashes and bugs in your programs while using pointers, however for those who attempt to keep away from the pointers hazards talked about above, you should be usually advantageous!!!