Results 1 to 7 of 7

Thread: Leaking Memory

  1. #1

    Thread Starter
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394

    Leaking Memory

    Is this function leaking memory?

    PHP Code:
    char** VirtualSplitString(charpStringchar sepintuBound)
    {
        
    unsigned long s 0unsigned long i 0;
        
    char** pArray = (char**)calloc(0sizeof(char));

        
    //allocate empty storage
        
        //the first array
        
    pArray[0] = (char*)calloc(1024sizeof(char));

        
    //separate an array...
        
    while(*pString)
        {
            
    //go through the string, find position
            
    if(*pString == sep)
            {    
                
    i++;
                
    0//current position
                //allocate space for new input
                
    pArray[i] = (char*)calloc(1024sizeof(char));
            }
            else
            {
                
    //get the current character and set it
                
    pArray[i][s] = *pString;

                
    //increase character position
                
    s++;
            }

            
    //increase string position
            
    pString++;

        }
        
        
    uBound = ++i;

        
    //return the pArray data type
        
    return pArray;
    }; 
    thanks for any help...
    VS.NET 2003

    Need to email me?

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Yes. For each calloc() call you should call free().

    Actually, it could be quite large, quite fast because you are allocating 1024 bytes repeatedly.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And the first calloc call is wrong.
    a) You must allocate char*, not char.
    b) You must allocate more than 0 items. Actually you must allocate one item per substring in pString. You either have to reallocate when the buffer is too small or you traverse the string once and check how many substrings there are, then allocate, then traverse it a second time.

    Of course if you free() the memory you allocated you can't return it anymore.
    The user must pass an array that is large enough, which you just fill.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    Thanks for your replies

    i see the point, so can i do something like this

    PHP Code:
    char** p;

    VirtualSplit(...

    for(
    int i 0boundi++)
      
    free(p[i]); 
    would that work?

    sorry i am new to these double pointers so i get confused fast. and these memory leaks are fairly bad because i am writing an FTP server... no good if it drains memory.
    VS.NET 2003

    Need to email me?

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    After you've freed all p[i] you also need to free p itself.

    It basically works, but it is bad manner to write it this way. It is better if the caller of the function allocates memory.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    What CB means - if a function allocates memory, it frees memory it allocated also. This isn't so much that it's 'bad form', whichit is.

    Without doing that you can't easily find memory leaks or other problems - especially six months later when you have totally forgotten your code.

    The only other 'out' is to use global variables - ones defined outside of a function. Most professional db coders (Oracle, etc)
    tend to do this because it reduces the total number of lines and allows debugging any module in the presence of most of the important variables. It has serious downsides as well. Like huge loadable image size, excessive demand-zero faulting on load....


  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    ... name conflicts, thread sync problems...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width