Thread: delete dynamically allocated char array

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    delete dynamically allocated char array

    is there anything specific we need to do to delete a dynamically allocated char array.

    i'm using the below code, and it is providing my asert errors.

    Code:
    	delete [] messagePtr1;
    	messagePtr1=NULL;
    	delete [] messagePtr2;
    	messagePtr2=NULL;
    	delete [] messagePtr3;
    	messagePtr3=NULL;
    it works in the same program for an integer array.

    please someone, if you know of an error in my syntax please tell.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How you delete something depends entirely on how you allocated it in the first place

    Plus, if you've corrupted memory, say by stepping off the end of the array, your delete code could be correct, but is failing because of problems elsewhere
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    allocation

    i allocate it similar to the following.
    Code:
     
    char* messagePtr;
    
    messagePtr=new char[]="System Check";
    isn't this correct?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    nope, it's not correct, I wonder whether your code even compiles, in case it compiles, you are allocating memory by "new char[]" and then, instead of assigning it to messagePtr and setting its contents to "System Check", the address of "System Check" assigned to messagePtr (it's a char array after all), causing a memory leak.
    Later when deleting messagePtr (delete [] messagePtr) you are actually trying to delete "System Check" memory, and since it's not valid heap memory, your getting an error.
    The correct way to do it is:
    Code:
    #define MAX_MESSAGE_SIZE 1024
    
    char* messagePtr;
    messagePtr = new char[MAX_MESSAGE_SIZE];
    strcpy(messagePtr, "System Check");
    
    // later
    delete [] messagePtr;
    Anyways, instead of using char* and heap memory, why don't you simply std::string?

    Code:
    #include <string>
    using namespace std;
    
    
    string message;
    message = "System Check";
    
    // no need to delete anything, std::string takes care of such stuff for you

  5. #5
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Why do you allocate heap memory at all if you then just copy into the newly allocated buffer a static string?

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    I'm new to dynamic memory.
    I assumed it is better to create and delete it.
    But from your posts I'm assuming I should allow it to die out naturally.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  7. #7
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    There's no way memory can "die out naturally", if you don't free it, it will cause a memory leak; however, some classes of the standard library encapsulate the memory management thing, so you don't have to worry about it.

  8. #8
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    Code:
     
    #define MAX_MESSAGE_SIZE 1024
    
    char* messagePtr;
    messagePtr = new char[MAX_MESSAGE_SIZE];
    strcpy(messagePtr, "System Check");
    
    // later
    delete [] messagePtr;
    Would this print the information inaccurately (trailing garbage values)?
    Suppose I use the following.
    Forgive me for not trying this myself, but I'm no where near an operable c++ compiler.
    Code:
     
    cout<<messagePtr;
    ------------------------------------------------------------------------------
    //Displays (I believe)
    System Check 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 000000
    ------------------------------------------------------------------------------

    Another follow up question is.
    Does the string class automatically deallocate the memory?
    Or is there follow up code that needs to be executed to prevent a memory leak?
    Last edited by xviddivxoggmp3; 11-23-2003 at 05:02 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM