[Question] NULL pointers...

Posts 1–6 of 6 · Page 1 of 1
[Question] NULL pointers...
So i learned about pointers a while back, and i figured since i was concentrating on rereading tutorials before i learn windows API and go ahead and tackle a 1500 page book i would look over them again, seeing as they're essential for hacking.

I came across null pointers, and they were explained in 4 lines. I know that in order to create a pointer that basically points to nothing you just make the value of the address its pointing to 0 (int *ptr = 0 or use the NULL keyword, which i didnt bother to learn how to use.

The tutorial also said that null pointers are useful in many situations, although i'm not quite sure when they would be used...

so my question isn't necessarily about how to incorporate null pointers in my code, but where i would use them... like in what situation would i need to make pointers that point to nothing....?

thanks :x
After deleting a pointer that was created using the 'new' keyword you should set it's value to NULL otherwise if you delete it again somewhere else in the code, anything could happen. This is called undefined behaviour. Also, when initializing pointers you should assign it to a NULL value if you are not using it straight away.

Quote from C++ Primer:
After deleting a pointer, the pointer becomes what is referred to as a dangling pointer. A dangling pointer is one that refers to memory that once held an object but does so no longer. A dangling pointer can be the source of program errors that are difficult to detect.

Setting the pointer to 0 (or NULL) after the object it refers to has been deleted makes it clear that the pointer points to no object.
Quote Originally Posted by master131 View Post
After deleting a pointer that was created using the 'new' keyword you should set it's value to NULL otherwise if you delete it again somewhere else in the code, anything could happen. This is called undefined behaviour. Also, when initializing pointers you should assign it to a NULL value if you are not using it straight away.

Quote from C++ Primer:
Thaaaanks!!

this brings me to my next question :3

in what kind of situation would i need to delete a pointer? and you do so by using delete *ptr if ptr is the pointer name.... after using say, int *ptr = new whatever right?
Quote Originally Posted by Mookamoka View Post
Thaaaanks!!

this brings me to my next question :3

in what kind of situation would i need to delete a pointer? and you do so by using delete *ptr if ptr is the pointer name.... after using say, int *ptr = new whatever right?
Well if you accolate it in heap memory using the "new" identifier then your going to have to deaccolate it using delete as it wont be destoyed after the scope ends
Quote Originally Posted by whit View Post


Well if you accolate it in heap memory using the "new" identifier then your going to have to deaccolate it using delete as it wont be destoyed after the scope ends
So if i used a pointer called ptr1 in a function, i would use the delete keyword to "delete" ptr1 and make it a dangling pointer and reuse it in another function if i wanted to save memory?
malloc/new for allocation
free()/delete for deallocation

actually most of the times you clear pointers just to be sure that you don't create memory leaks with your application. not that it is really necessary.

And a pointer is something that points to a memory location, so... it's just a pointer. The problem is in what is was pointing at.

Let's say you have the pointer pointed at some place on memory and placed a string in there. If you point it to null without clearing memory first, you are creating a memory leak. With this i mean, you are actually leaving the string in memory, just not pointing to it. That's why you should always clear the memory after using the pointer for something else, or after finishing using it.

(after the application close, windows will clear the memory, but still, get used to it)

Edit: Just saw that master posted pretty much this.. Sorry >_<
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us