Why to avoid uninitialized pointers?
Rohit Kumar Sharma
Software Engineer (C++, AI/ML) @Autodesk | Ex-Altair | BITS Pilani
In day-to-day life, as a C++ developer using pointers is very common. But improper use of pointers may lead to severe problems. One such use case is uninitialized pointers.
What is uninitialized pointer?
Why uninitialized pointers are dangerous?
Uninitialized pointers mostly lead to run time crashes. It is also observed that uninitialized pointers and variables can cause run time crashes in release mode but a similar is not observed in debug mode. Such issues are hard to track down for a developer.
Let us dive into the topic to understand the reason behind the run time crash.
Under most of the compilers, if we use an uninitialized pointer then it will use whatever bits are in the memory in which pointer resides as if it were an address (pointing to some random location). When we try to manipulate the underlying data at such random location then it usually leads to the crash as soon as we attempt to dereference the uninitialized pointer. This can be easily understood from following diagram:
Difficulties associated with uninitialized pointers:
SWE-1 @UKG | Ex- Full Stack Developer @AI4Bharat | Frontend Developer @Uniflik Pvt Ltd | Ex - SDE @Indian Oil | GDSC Open Source Lead | NIT AP CSE'24 | TnP Volunteer
1 年Well written sir Rohit Kumar Sharma Sir I have messaged for could you please check your DM actually it's very important for me ????
Senior Software Development Engineer (Meshing and Geometry) | C++ Developer | Computer Graphics | Data Structures And Algorithms | Low Level Design
2 年A very well written article, Please do write such interesting and cute concepts more often