Which comes first...delete [] or NULL?

This is a discussion on Which comes first...delete [] or NULL? within the C++ Programming forums, part of the General Programming Boards category; I've allocated memory for a pointer with new like this: Code: char * szBuffer=new char [256]; I use it several ...

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    Which comes first...delete [] or NULL?

    I've allocated memory for a pointer with new like this:
    Code:
    char * szBuffer=new char [256];
    I use it several times in the program, rewriting data into it.
    When I quit at the end of the program, i get an error. Debug says it happens when i call delete [] on the szBuffer pointer.
    currently heres what i do:
    Code:
    delete [] szBuffer;
    szBuffer=NULL;
    as i've been told szBuffer is turned to 0 length when NULL is used...but should that be used before or after delete?
    thanks for your help.
    PHP and XML
    Let's talk about SAX

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you did it correctly, I would suspect that something happened elswhere in your program that caused the crash. You can have memory overwrites and such that don't cause crashes immediately. check the pointer to make sure it's the same address as was allocated.

  3. #3
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    actually, i was messing around and i flipped the order, and that worked as well. No error this time. And i don't seem to be leaking memory in the background. The debugger didn't catch anything anyway. But i think you're right too, i got that example straight out of the book, delete then NULL. But perhaps it doesn't matter. thanks.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    it does matter. if you set the pointer null and you delete a null ptr you are running into system memory. Plus, you let go of the address of the actual memory. THAT'S BAD!!!

  5. #5
    Unregistered
    Guest
    The order is strict.

    It is a good habit to set it to NULL after you delete it, just to make
    sure you wont mess up with it later, the memory has already been deleted, you are not supposed to access it using the pointer.

    The other way, the memory never gots recycled and it is memory
    leak. It is safe to delete a NULL pointer though, no action is performed.

    what do I know?

  6. #6
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    ok...then what should i do? If i go back to delete [] then NULL, i'll get the error. Is there something i need to do to the pointer before i delete it that will help. Basically szBuffer points to 256 bytes of memory that hold the results of the windows api function ReadFile(). Currently in the arguments to ReadFile, i tell it to use the szBuffer pointer, and read in 256 bytes. Now all this is in a loop (not the delete part) and the loop runs through each file in a directory, reading 256 bytes of info. Could that be it?
    PHP and XML
    Let's talk about SAX

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    now i have NO idea what else youre doing in your program
    but with me, usually when i get an error like that during delete,
    it means somewhere in the program i probly wrote past the end of the 256 chars. maybe just step through the code once see if you can catch anything

    thats the annoying thing about memory accesing, you usually get the error way after you made the mistake.

  8. #8
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    um...

    if i assign new data to a pointer...does it automatically overwrite? Perhaps that could be a problem...
    PHP and XML
    Let's talk about SAX

  9. #9
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537

    I think...

    if its dynamic memory it doesnt overwrite, it just points to the new memory and hence a memory leak is caused. If it is static, like in a char array, then it overwrites the data and if its too big will cause segmentation faults.
    Couldn't think of anything interesting, cool or funny - sorry.

  10. #10
    tgm
    tgm is offline
    Registered User tgm's Avatar
    Join Date
    Jun 2002
    Posts
    150
    Are you using multiple 'new' statements? That might be the problem (if that's in your loop).

    It's important to understand that when you use 'delete' you're not actually deleting the memory, it's just freed to be used again. The data is still there (as long as it hasn't been overwritten) and can still be accessed by the pointer. That's why you set the pointer to NULL after you delete; so there's no way to access the memory anymore, by accident. If you set the pointer to NULL before releasing the memory then there is no way to release it later (or access it). The same error can happen if you use multiple 'new' statements, you lose the pointer and thus the ability to free the memory.

    Each new statement should match up with a delete statement.
    Last edited by tgm; 08-04-2002 at 03:00 PM.

  11. #11
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    the new statement is not in the loop, and neither is the delete. I now see why delete comes before NULL.
    Like it says up top, this is the format of the pointer:
    Code:
    char * szBuffer=new char [256];//have to use pointer for certain functions
    ...
    /*should be
    delete [] szBuffer; <--Causes an error when the program closes, one of the last things done before return 0 is this step.
    szBuffer=NULL; 
    */
    //but i have the following
    szBuffer=NULL;//causes memory leak but no error
    delete [] szBuffer;
    I don't declare static...is that whats causing the problem? that it keeps allocating more and more memory without freeing it all? If declaring static would fix the problem...how would i go about doing it? like so?:
    Code:
    new static char * szBuffer;//now how do i make it 246 bytes in size?
    memory management is not one of my strong points...I've never been taught the do's and don'ts so i play it by ear and end up with these errors, so if you could bear with me that'd be great. thanks.
    PHP and XML
    Let's talk about SAX

  12. #12
    Unregistered
    Guest
    Could you try to change

    char * szBuffer=new char [256];

    to

    char * const szBuffer=new char [256];

    and see whether your code still compiles?

    It looks like you let szBuffer pointed to OSCore and then try
    to delete it.

    Or you can print the two addresses, make sure you are deleting
    the correct location, also, why not post a little more of your code?

    Again, what do I know?

  13. #13
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    i'll post the cpp...it's pretty well commented i think...but if you have questions, just post the suspicious code and i'll go into depth for you. thanks all.
    Attached Files Attached Files
    PHP and XML
    Let's talk about SAX

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    do this:

    delete szBuffer;
    szBuffer = NULL;

    this macro is usefull:

    #define SAFE_DELETE(p) if(p){ delete p; p = NULL; }

    #define SAFE_DELETE_ARRAY(p) if(p){ delete [] p; p = NULL; }

    use like this:

    SAFE_DELETE(szBuffer); //can skip the ;


    struct B{
    char b;
    int c;
    long a;
    };

    B monkey = new B[100];

    SAFE_DELETE_ARRAY(monkey);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  2. Syntax Error??
    By Kennedy in forum C Programming
    Replies: 8
    Last Post: 09-06-2006, 12:04 PM
  3. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  4. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 02:52 PM
  5. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 04:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21