Results 1 to 6 of 6
  1. #1
    Mookamoka's Avatar
    Join Date
    Feb 2011
    Gender
    male
    Posts
    9
    Reputation
    10
    Thanks
    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

  2. #2
    master131's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    Melbourne, Australia
    Posts
    8,858
    Reputation
    3438
    Thanks
    101,667
    My Mood
    Breezy
    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.
    Last edited by master131; 02-03-2011 at 10:03 PM.
    Donate:
    BTC: 1GEny3y5tsYfw8E8A45upK6PKVAEcUDNv9


    Handy Tools/Hacks:
    Extreme Injector v3.7.3
    A powerful and advanced injector in a simple GUI.
    Can scramble DLLs on injection making them harder to detect and even make detected hacks work again!

    Minion Since: 13th January 2011
    Moderator Since: 6th May 2011
    Global Moderator Since: 29th April 2012
    Super User/Unknown Since: 23rd July 2013
    'Game Hacking' Team Since: 30th July 2013

    --My Art--
    [Roxas - Pixel Art, WIP]
    [Natsu - Drawn]
    [Natsu - Coloured]


    All drawings are coloured using Photoshop.

    --Gifts--
    [Kyle]

  3. The Following User Says Thank You to master131 For This Useful Post:

    Mookamoka (02-04-2011)

  4. #3
    Mookamoka's Avatar
    Join Date
    Feb 2011
    Gender
    male
    Posts
    9
    Reputation
    10
    Thanks
    1
    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?

  5. #4
    whit's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    7,159
    Reputation
    490
    Thanks
    2,253
    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

  6. The Following 2 Users Say Thank You to whit For This Useful Post:

    Hell_Demon (02-05-2011),Mookamoka (02-04-2011)

  7. #5
    Mookamoka's Avatar
    Join Date
    Feb 2011
    Gender
    male
    Posts
    9
    Reputation
    10
    Thanks
    1
    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?

  8. #6
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    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 >_<
    Last edited by 'Bruno; 02-04-2011 at 04:41 PM.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  9. The Following User Says Thank You to 'Bruno For This Useful Post:

    Mookamoka (02-04-2011)