Thread: delete new char[] correctly ?

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    8

    delete new char[] correctly ?

    Hi padawan here So do l have this situacion. l call a function twice that returns a c-string, and l save into the same char* pointer (* c_string). but is correct to delete that (* c_string) before assigning the return value again to foo() ?. Because if l dont part of the content remains, when l stepped in the debugger l can see it. Thanks in advance.

    Code:
    char *foo (const char *_s) {
       char *str = new char[40];
       strcpy(str, _s);
       strcat(str, " *** ");
       strcat(str, _s);
       return str;
    }
    
    int main(int argc, char* argv[]) {
       char *c_string;
    
       c_string = foo("monster");
       printf("%s\n", c_string);
       delete []c_string; // is this correct before calling foo() again?
    
       c_string = foo("paragraph another");
       printf("%s\n", c_string);
       delete []c_string;
    
       getch();
       return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,084
    Yes, you should do so otherwise you technically have a memory leak (although this is a short running program so it doesn't matter).
    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

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    8
    thats what l was thinking thank you. Yes this a small part of a program.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    765
    Another tip, using only ISO functions from string.h:

    Code:
    char *foo( char *s )
    {
      char *p;
      char tmp;
      ssize_t size;
    
      // get the size of final string.
      size = snprintf( &tmp, 1, "%s *** %s", s, s );
    
      // allocate space, fill the buffer and return the pointer.
      p = new char[size+1];
      sprintf( p, "%s *** %s", s, s );
      return p;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-28-2015, 06:10 AM
  2. char** within struct not printing correctly.
    By aj74 in forum C Programming
    Replies: 1
    Last Post: 11-12-2011, 11:11 AM
  3. Delete first 4 chars in char array
    By cagney58 in forum C Programming
    Replies: 5
    Last Post: 05-03-2009, 07:00 PM
  4. Delete char from string?
    By samuelmoneill in forum C Programming
    Replies: 8
    Last Post: 04-24-2009, 03:08 PM
  5. Did I Use Delete Correctly?
    By Artist_of_dream in forum C++ Programming
    Replies: 5
    Last Post: 11-22-2004, 10:21 AM

Tags for this Thread