Take a look at the example code below. Its nothing serious, just something I put here as a scaled-down representation of something I am doing on a larger scale. Assume this function is only one part of a much bigger program.
My question is that, given the operation done above, does this result in a small memory leak, or will the original string pointed to by myString be wiped out when the function exits? If not, then will the program which calls this function have an extra "Your Mom" just sitting around in memory?Code:int someUselessFunction( ) { char *myString = "Your Mom"; char buf[32]; /* do some stuff here regarding string and store the resulting value in buf */ /* make myString point to the result in buf */ myString = buf; /* do whatever the program needs with myString here */ return 0; }
If so, then I'd do something like this:
Now, if this is, in fact, what I need to do, then what is the syntax for deleting a pointer in C? I know in C++ the syntax is delete.Code:int someUselessFunction( ) { char *myString = "Your Mom"; char *temp = NULL; char buf[32]; /* do some stuff here regarding string and store the resulting value in buf */ /* I'll assume you know what's going on here */ temp = myString; myString = buf; (delete?) temp; return 0; }
If my question is dumb and pointless from the start, then, well, at least humor me, it's been a long time since I've worked with C/C++.