Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Safe" pointer values interval?

Tags:

c++

c

pointers

First of all I am not very familiar with the stack and heap.

In a lot of programs I see that pointers are checked for NULL. But that doesn't prevent a crazy address like 0x002011 to be passed.

My question: is there "safe" address interval, that I can check a pointer belongs to and be reasonably sure it's valid, before dereferencing it?

like image 875
catfish_deluxe_call_me_cd Avatar asked Jun 26 '26 06:06

catfish_deluxe_call_me_cd


1 Answers

No. Make sure to initialize pointers to NULL when creating a new variable, and then only change the value with malloc (when C) or new (when C++) or other allocating functions (or assigning to another valid pointer or NULL). Set back to NULL after free and delete respectively.

like image 120
littleadv Avatar answered Jun 28 '26 20:06

littleadv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!