Thread: Increasing memory allocation in function

  1. #1
    Registered User
    Join Date
    Dec 2008
    Location
    Sweden, Malmö
    Posts
    5

    Increasing memory allocation in function

    Hello everyone, I am trying to get a function to increase memory allocation for a LPSTR(char *) in a function. Basically I am aiming for a string concatenation function which includes the calloc/free - reducing the amount of code in my program.
    However, my function is not working as I intended. At first I wrote a variant(LPSTRAddToOriginal or variant 0) that I hoped would take an LPSTR and increase memory allocation in the function and do the strcat:ing there. That function makes the strcat alright but when the execution returns to the main(), the LPSTR is back to its original state. Also, calling it several times without freeing the original LPSTR leaks memory.
    My second attempt(LPSTRAddToOriginal2 or variant 1) returns a LPSTR and this actually works but with again, calling it several times without freeing the original LPSTR leaks memory.

    There is something I am missing here, should it not be possible to do this?
    Thankful for any advice!
    /Anders

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int LPSTRAddToOriginal(LPSTR lpzOriginalString, LPSTR lpzStringToAdd)
    {
      LPSTR lpzTemp;
      
      lpzTemp = (char *)calloc(strlen(lpzOriginalString) + strlen(lpzStringToAdd) + 1,sizeof(char));
      strcpy(lpzTemp, lpzOriginalString);
      strcat(lpzTemp, lpzStringToAdd);
      
      free(lpzOriginalString);
      lpzOriginalString = (char *)calloc(strlen(lpzTemp) + 1, sizeof(char));
      strcpy(lpzOriginalString, lpzTemp);
      printf("lpzTemp: %s\n", lpzTemp);
      free(lpzTemp); 
     
      return 0;   
    }
    
    LPSTR LPSTRAddToOriginal2(LPSTR lpzOriginalString, LPSTR lpzStringToAdd)
    {
      LPSTR lpzTemp;
      
      lpzTemp = (char *)calloc(strlen(lpzOriginalString) + strlen(lpzStringToAdd) + 1,sizeof(char));
      strcpy(lpzTemp, lpzOriginalString);
      strcat(lpzTemp, lpzStringToAdd);
      
      free(lpzOriginalString);
      lpzOriginalString = (char *)calloc(strlen(lpzTemp) + 1, sizeof(char));
      strcpy(lpzOriginalString, lpzTemp);
      printf("lpzTemp: %s\n", lpzTemp);
      free(lpzTemp); 
     
      return lpzOriginalString;   
    }
    
    int main(int argc, char *argv[]) {
      LPSTR lpzFirst;
      LPSTR lpzSecond;
      LPSTR lpzSpace;
      
      int iResult;
      int iCtrl;
      int iVariant;
      if (argc>1) 
         {
         iVariant=1; 
         }
         else
         {
         iVariant=0;
         }
      lpzSecond="Second";
      lpzSpace="-";
      lpzFirst ="First"; 
      
      // Test loop
      for (iCtrl=1;iCtrl<10;iCtrl++)
        { 
        if (iVariant==0)
          {
          // variant 0    - Does not work and leaks memory   
          iResult = LPSTRAddToOriginal(lpzFirst, lpzSpace);    
          iResult  = LPSTRAddToOriginal(lpzFirst,lpzSecond);
          }
        else
          {
          // variant 1 - Works but leaks memory
          lpzFirst="First";
          lpzFirst=LPSTRAddToOriginal2(lpzFirst, lpzSpace);    
          lpzFirst=LPSTRAddToOriginal2(lpzFirst, lpzSecond);              
          }  
          printf("%i lpzFirst: %s\n",iVariant, lpzFirst);    
        
        }
      return 0;
    }
    Last edited by Ramses800; 12-15-2008 at 09:31 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You need pass pointer to pointer to modify the original pointer inside the funtion

    You could use realloc - there is a chance it will increase the buffer size without moving the original string
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    realloc() should be used
    vart's comment is for the first function. The second function's logic is OK.

    Why do you say it leaks memory?
    You allocate memory.
    Inside the function you free and allocate again memory for the original. You also allocate and free for the temporary.
    When you call it again you will again free the memory and then allocate. So everything seems ok.
    What you DON'T do is you don't free memory at the end of the program. That is your free missing.

    EDIT: You might realize that you don't really have to do all this. You create what you want inside the temporary string. Why not return that? Like:
    Code:
    LPSTR LPSTRAddToOriginal2(LPSTR lpzOriginalString, LPSTR lpzStringToAdd)
    {
      LPSTR lpzTemp;
      
      lpzTemp = (char *)calloc(strlen(lpzOriginalString) + strlen(lpzStringToAdd) + 1,sizeof(char));
      strcpy(lpzTemp, lpzOriginalString);
      strcat(lpzTemp, lpzStringToAdd);  
      free(lpzOriginalString); 
      return lpzTemp ;   
    }
    Even better is to do this, since you cannot use the original pointer, since you freed memory. That is what vart's comment was:
    Code:
    int LPSTRAddToOriginal(LPSTR* lpzOriginalString, LPSTR lpzStringToAdd)
    {
      LPSTR lpzTemp;
      
      lpzTemp = (char *)calloc(strlen(*lpzOriginalString) + strlen(lpzStringToAdd) + 1,sizeof(char));
      if (lpzTemp == NULL) return -1; //check for success since you return a value
      strcpy(lpzTemp, *lpzOriginalString);
      strcat(lpzTemp, *lpzStringToAdd);  
      free(*lpzOriginalString); 
      *lpzOriginalString = lpzTemp;
      return 0;
    }
    Last edited by C_ntua; 12-15-2008 at 10:23 AM.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Location
    Sweden, Malmö
    Posts
    5
    Thank You for Your help, that works!

    cheers
    /Anders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM