UnicMinds

When does pointer dereference crash? - UnicMinds

Why does pointer dereference crash sometimes?

Every program has some allocated memory in the form of heap, stack, static data, registers, and more. When the program is trying to access a memory location that is not allocated or that it is not allowed to access because of OS protection then the error that occurs is called a Segmentation Fault. These are common issues in C and C++ programming and a core dump occurs and it can be a bit more than tedious to go through these errors.

Dereferencing a pointer without initializing it will almost always lead to unpredictable behavior or a crash due to a segmentation fault. When you declare a pointer (e.g., int* p;), it points to an arbitrary location in memory. This means it doesn’t point to a valid object or memory region.

If you try to dereference this pointer (e.g., *p = 42;), you are attempting to access memory that the pointer does not point to. This can lead to various outcomes:

  • Crash: In many cases, the program will attempt to access a memory location that it does not have permission to access, leading to a segmentation fault or access violation, which typically causes the program to crash.
  • No Immediate Crash: In some scenarios, the memory address may be valid but not allocated for your use, which could lead to unpredictable behavior, such as overwriting important data or causing other parts of the program to behave incorrectly.
  • Subtle Bugs: In rare cases, if the memory happens to be writable and does not cause an immediate fault, the program might continue running, but it could corrupt data and lead to bugs that are hard to trace.

Solution: First allocate memory for an object. How you should allocate, depends on how you intend to use the object. A simple way to allocate an object is to use an automatic variable:

int main() { 

int object; 

int* pointer = &object; // you can assign the pointer to the specific address of the existing object 

*pointer = 10; // dereferencing is OK 

}

When you declare a variable “int a” then memory is allocated because you’re telling the computer to allocate memory for the variable’s data. So, if you declare an int variable in a method, it’s stack frame will take up an extra 4 bytes of memory. Always remember that allocating memory to the object is important, else you will segfault when dereferencing the pointer.. 

int *a = new int; //memory allocation

*a = 10; 

delete a;

Also, let’s assume you do the below in a function.

Member *m = new Member();

In this case the object is allocated on the heap memory and the pointer is allocated on the stack and the pointer is pointing to the object created on the heap memory.

Similarly, a pointer is usually the size of the word in the system. So, on a 32-bit architecture, 32 bits, and on a 64-bit architecture, all pointers are 64-bit. For example, in a 64 bit computer, the word size is 64 bit and the pointer if allocated to a 32 bit int (as shown below), then it will crash.

int copy_of_pointer = ptr;

So, to ensure your C++ code is portable, you must avoid unspecified behavior. 

  • Do not assume anything about the datatypes other than what the C++ standard specifies. That is, a char is at least 8 bit, both short and int are at least 16 bits, and so on.
  • Don’t try to do pointer wizardry (casting between pointer types or storing pointers in integral types). Don’t use void*.
  • Don’t use a unsigned char* to read the value representation of a non-char object.
  • Be cautious in performing operations that may over or underflow. Think carefully when doing bit-shift operations.
  • Be careful when doing arithmetic on pointer types.

Hope this is useful, thank you.

You may like to read: About PCEP Certification, Summer Coding Courses for Kids, & Coding Competitions for Kids

Leave a Comment

Your email address will not be published. Required fields are marked *

BOOK A FREE TRIAL