c style string memory leak question

This is a discussion on c style string memory leak question within the C++ Programming forums, part of the General Programming Boards category; Code: char * cstring; cstring=new char[15]; cstring = "I forget"; Does this cause a memory leak because the memory allocated ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    c style string memory leak question

    Code:
    char * cstring;
    cstring=new char[15];
    cstring = "I forget";
    Does this cause a memory leak because the memory allocated with new is lost when the pointer cstring is assigned to the address of the string literal "I forget".
    Meaning 15*sizeof(char) was lost?

    This is a take off of a question posted on a news group I can't respond to so I'm asking here. This is strictly to have a better understanding of legacy code. std::string I know is prefered.

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    after allocating memory with new, and you don't need it any more use delete:

    delete [] cstring;

    [] means that you are deleting an array.

    and you can't use:
    cstring = "something";

    use strcpy instead:

    strcpy(cstring, "something");

    hth

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788

    Re: c style string memory leak question

    Originally posted by curlious
    Code:
    char * cstring;
    cstring=new char[15];
    cstring = "I forget";
    Does this cause a memory leak because the memory allocated with new is lost when the pointer cstring is assigned to the address of the string literal "I forget".
    Meaning 15*sizeof(char) was lost?
    Yes

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Posted by glUser3f@
    and you can't use:
    cstring = "something";
    Yes you can, that code is perfectly valid (apart from the memory leak). The variable cstring is a pointer and can therefore be made to point to a string literal, without the need for strcpy().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    and what happens when the string literal goes out of scope?
    I mean:

    in some function:

    Code:
    cstring = "something";
    return cstring;
    the function returns a pointer to arbitrary memory, got my point?

    and to clarify things, if cstring isn't pointing to some valid memory (ie returned by operator new) you can't use strcpy either.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    > and what happens when the string literal goes out of scope?

    String literals never go out of scope during the lifetime of a program.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>what happens when the string literal goes out of scope?
    Already answered, but I'll repeat: They have static storage duration, therefore they exist throughout. Your example is perfectly valid, and so is this:
    Code:
    #include <iostream>
    using namespace std;
    
    const char *foo(int digit)
    {
      const char *rc;
      switch (digit)
      {
        case 1: rc = "one"; break;
        case 2: rc = "two"; break;
        // etc
        default: rc = "oops"; break;
      }
    
      return rc;
    }
    
    int main()
    {
      cout << foo(1) <<endl;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    really, you learn something new everyday
    thx for the info

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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