C++ char array strings memory leak?

when i do this
char* string = "hello";

should i first do this?
delete string;

before i do this?
string = "a diffrent hello";


when i call delete string i get some kind of error though.
i wonder where is data allocated when i write the char array string or whatever its called "hello" is it heap or stack?

thanks for any help im curious.
«1

Comments

  • Note that "char* string" is a pointer, not an actual string. All the literal values like "hello" and "a diffrent hello" are stored in the data section of the program. The "string" variable holds the [italic]pointer[/italic] to the "hello" string, it doesn't hold the string itself. When the "string" variable is set to point to "a diffrent string", then the "hello" string still remains in memory, because it is part of the program, not a dynamically allocated string, so you can't free it. delete is used to free memory that is allocated dynamically at run-time.
  • [color=Blue]Short answer: 'delete' is used ONLY for pointers allocated with 'new' and never on anything else.[/color]
  • Also, dynamically allocated arrays/strings should be deleted with the "delete[]" operator, not the "delete" operator. This syntax is exceptionally moronic, and likely the most common cause for memory leaks in C++.
  • Okay so they are located in the data section and shouldnt/cant be deleted.

    But what if i sometimes want to this

    string = new char[]{"a hello in heap"};

    and then after do this

    string = "a hello in data";

    what should i do in between to check if the current string points to data or heap so that i can delete it? if i try to delete something in data ill get an runtime-error, so i have to do some kind of check before i try to delete.


  • It doesn't make sense to re-assign a pointer pointing at the heap without first deleting the data it points at.

    I can't see any reason why you would need reuse such a pointer to suddenly point at static data. Sounds like muddy program design.
  • Okay so i've decided ill only use char strings from heap or whatever they are called.

    is there an easy way to write them? something like
    char* string = new char[]{"hello from heap"};

    i get syntax errors from the above line
    error C2143: syntax error : missing ';' before '{'

    or something in style with
    char* string = new "hello from heap";
    error C2059: syntax error : 'string'

    because the main advantage of static char-array-strings from data was that they were easy to write.
  • [color=Blue]Easier to use good old C:
    [/color]
    [code]
    char* s = strdup ("hello frop heap");

    ... working with 's' ...

    free (s);
    [/code]
  • Except that strdup() isn't C, it's a third party function. And you can be dang sure it will mess up your program if you delete [] stuff allocated with strdup().
    A better way is strcpy:

    char* str = new char[x];
    strcpy(str, "blabla");
  • But using strcpy will the compiler first create a string in data then a make a copy of it to a string in heap?
  • [color=Blue]Anything in double quotation marks is a string constant and it will take room in a program data section. However, if strcpy() is used with pointers, then compiler simply passes parameters as they are. In short, if you write: "strcpy (s, "text");" - then the "text" will be created in data section. However, if you write: "strcpy (s1, s2);" - there are no strings in data section.[/color]
  • OK thanks for the help.
  • Ah i did some "soul-searching" and i found that making a private copy of the string inside the object would be better because it supports the object-oriented private idea.
  • What about something like this?

    char* test;
    cin >> test;

    The text entered by the user can be of any length. This looks like dynamically allocated space to me. Where is this stored? Why don't I have to deallocate this space afterwards?

    Or command line arguments:
    int main(int argc, char* argv[])

    The program can not know during compilation what length the arguments will be. How come we can store strings of variable length in char pointers here? Where are they saved?
  • : What about something like this?
    :
    : char* test;
    : cin >> test;
    :
    : The text entered by the user can be of any length. This looks like
    : dynamically allocated space to me. Where is this stored? Why don't I
    : have to deallocate this space afterwards?

    [color=Green]That will crash, because cin doesn't allocate memory itself, it expects you to do it. test is an uninitialized variable here and a good compiler will also warn you about this. You just allocate a reasonably large buffer for it so that it doesn't overflow and take care of freeing it yourself later too.[/color]

    : Or command line arguments:
    : int main(int argc, char* argv[])
    :
    : The program can not know during compilation what length the
    : arguments will be. How come we can store strings of variable length
    : in char pointers here? Where are they saved?

    [color=Green]There's a lot that goes on before a program reaches the main function. Typically what happens is the C++ runtime looks at the command line provided by the system, allocates memory for each argument and copies the arguments to their respective buffers. It makes an array of pointers to the arguments and passes it to the main function of the program(in argv). This memory is freed when the main function returns.

    A character array is in most cases terminated with a null character(0). This is how we can find out the length of it.
    [/color]
  • Thank you very much for the explanation.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories