Thread: Memory leak?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    12

    Memory leak?

    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.

    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;
    }
    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?

    If so, then I'd do something like this:

    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;
    }
    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.

    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++.
    If at first you don't succeed, punch your monitor, kick your chair, strangle your boss, and toss your computer out of the nearest window. Then, once you've relieved a portion of your frustration, try again.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    This will NOT cause a memory leak:

    char * str = "abcdef";

    All string literals exist only once in the program. They are stored in the data section of the program. When you do the above command, it makes a new pointer, and fills it with the address where the string is located (in the data section). You CANNOT use delete on this point -- you never allocated that memory; it is memory that is in the data section which is loaded and unloaded automatically.

    This, however, WOULD cause a memory leak:

    char * str = new char[7];
    strcpy(str, "abcdef");
    str = buffer;

    because every time you run the function, you allocate 7 characters that you don't free. As you wrote it, you never allocate memory (you merely point to automatically allocated mem) and you never need to free it. There is no leak.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    12

    ok cool...

    Thanks
    If at first you don't succeed, punch your monitor, kick your chair, strangle your boss, and toss your computer out of the nearest window. Then, once you've relieved a portion of your frustration, try again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM