Thread: segfault on realloc

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    segfault on realloc

    Hi everyone,

    i hope someone could run over my code to see what im doing wrong...

    this is not the total code but enough to see the problem.. i hope..

    i have a function which reads from a socket and and dynamicly manages memory for a buffer. This buffer is first allocated in the main method. the problem is that i get a segfault sometimes in the realloc function in the main method just after "before realloc in entry ... " and sometimes in the strcat function To my knowledge is this buffer always big enough to contain the string
    hope someone can help me...

    my code looks like this..





    Code:
    # define BUFFER  1024
    static xmlString = NULL;
    static _2k =2
    
    main(){
    
    /* if pointer points to other than null than this service has been called before so reset and reuse it */ 
    _2k = 2; 
    
    if (xmlString != NULL ){ 
     deb_log(0, "%s", "before: realloc in entry"); 
     ptr = (void * )realloc(xmlString, (_2k * RS_BUFFER)); 
     if ( ptr == NULL ){ 
     realloc(xmlString, 0 ); 
     return MALLOC_ERROR + MV + "Error while allocating memory"; 
    } 
    xmlString = ptr; 
    
    deb_log(0, "%s", "after: realloc in entry"); 
    memset(xmlString, 0, (_2k * RS_BUFFER)); 
    deb_log(0, "%s", "after: memset"); 
    
    
    receiveHttpMsg(&xmlString, fd );
    
    } 
    
    int receiveHttpMsg(char ** buf, int fd){ 
    
    int  totalBytesRcvd; 
    void * tmp; 
    char * readBuffer = (char * ) calloc(1, BUFFER); 
    int bytesRcvd = 1; /* Bytes read in single recv() */ 
    
    /* Receive response from the server */ 
    printf("Received: "); 
    while (bytesRcvd > 0) { 
    
     /* Receive up to the buffer size (minus 1 to leave space for 
     a null terminator) did this in loop because otherwise the whole message could not be read 
     */ 
      if ((bytesRcvd = recv(fd, readBuffer, (BUFFER -1) , 0)) > 0){ 
      totalBytesRcvd += bytesRcvd; /* total bytes read in loop*/ 
      readBuffer[bytesRcvd] = '\0'; 
      printf(" [%d][%d][%d]\n", strlen(*buf), strlen(readBuffer), _2k * RS_BUFFER); 
        
        if ( (strlen(*buf) +strlen(readBuffer) -1) > _2k * RS_BUFFER){ 
         _2k++; 
         tmp = (void * )realloc(*buf, (_2k * RS_BUFFER)); 
      
        if ( tmp == NULL ){ 
         printf("error while reallocating .. \n"); 
         realloc(*buf, 0 ); 
         free(readBuffer); 
         return -1; 
        } 
       
       *buf = tmp; 
       (*buf)[totalBytesRcvd]= '\0'; 
       printf(" buffer enlarged [%d]\n", strlen(*buf)); 
       } 
       
       /* STRCAT SEGFAULT*/
       deb_log(0, "%s", "tocopy"); 
       strcat(*buf, readBuffer); 
       deb_log(0, "%s", "copy--done"); 
    
    /* memset(readBuffer, 0 , BUFFER); */ 
    
      } 
    /* host closed connection so we cleanup socket */ 
    else{ 
    printf("close connection \n"); 
    close(fd); 
    free(readBuffer); 
    break; 
    } /* end else */
    
    }  /* end while */
    
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    static xmlString = NULL;
    static _2k =2
    How about you actually provide a type for your variables?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    3
    could someone give me a real answer please ?

    of course i declared..

    static char * xmlString = NULL;
    static int _2k = 2;
    Last edited by ziel; 03-16-2003 at 05:06 AM.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If you want a "real" answer, then post some "real" code please.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Hope this answer is real enough.

    The function realloc() deallocates the old pointer. This means that after realloc() xmlString == NULL. So you can not reallocate it. If you want to be sure it is NULL, then add a check. If it is not NULL, then use free() to free the still allocated memory.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What is this:
    >>return MALLOC_ERROR + MV + "Error while allocating memory";

    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does realloc cause a segfault here?
    By Boxknife in forum C Programming
    Replies: 3
    Last Post: 05-06-2009, 10:54 PM
  2. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  3. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  4. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  5. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM