Thread: What is delete in this case causing error ?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    59

    Exclamation What is delete in this case causing error ?

    Hello everybody,

    Can anyone suggest why delete is not working in the following code :
    Code:
    char *str = new char[12];
    str = "Hello World";
    cout <<"\n\n The string is : "<< str;
    //delete str;

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by techie_san778 View Post
    Hello everybody,

    Can anyone suggest why delete is not working in the following code :
    Code:
    char *str = new char[12];
    str = "Hello World";
    cout <<"\n\n The string is : "<< str;
    //delete str;
    Use:

    Code:
    delete []str;
    That is the syntax for deleting an array - its a bit different from the syntax for deleting a single variable
    Last edited by Aslaville; 02-06-2015 at 07:21 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Aslaville is right to say that you should match new char[12] with delete[]. However, there's another mistake:
    Code:
    str = "Hello World";
    Since you assign a pointer to the first element of the string literal to str, you can no longer use delete[] on str. In fact, you have a memory leak since you now have no way to call delete[] on the memory allocated in the previous line.

    There are a few ways to solve this, but I suggest simply using std::string:
    Code:
    std::string str = "Hello World";
    cout << "\n\n The string is : " << str;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    59
    @laserlight, please suggest other methods that you know.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    You could use some form of copying if you want to explicitly use new(). Or you need t o assign the value for each character manually.

    This link might help : c - Why can a string be assigned to a char* pointer, but not to a char[] array? - Stack Overflow

    It seemed kind of relevant to your usage.

  6. #6
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by laserlight View Post

    Aslaville is right to say that you should match new char[12] with delete[]. However, there's another mistake
    Yes, Am always underestimating things

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete causing SIGSEGV
    By Neo1 in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2010, 06:40 AM
  2. Function causing error
    By smitsky in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2004, 07:01 PM
  3. log function causing an error
    By jbsloan in forum C Programming
    Replies: 2
    Last Post: 06-20-2004, 08:27 PM
  4. Computer differences causing error?
    By conright in forum Windows Programming
    Replies: 9
    Last Post: 01-06-2003, 03:48 PM
  5. Code causing error
    By Inept Pig in forum C Programming
    Replies: 7
    Last Post: 11-12-2002, 03:44 AM