Results 1 to 4 of 4

Thread: The buffer when using recv

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2003
    Posts
    68

    The buffer when using recv

    Hi.


    All examples I have seen that use recv (in C wich is what I use) has had the buffer (that is filled up with recv) declared as:

    char buffer[256];

    I have done the following:

    char *buffer=NULL;
    buffer=malloc((BUFFSIZE)*sizeof(char));

    and it all seem to work fine until the end of my function where I try to free the allocated memory with:

    free(buffer);

    Then the program buggs out on me.
    Since I´m running MS VC++ I´ve done some debugging on it but haven´t found out why this is happeninig. buffer is allocated and contains info when trying to do the free().

    So I wonder if it might be so that recv messes up my allocated buffer since I did get it thru malloc?

    Or do anyone have another point of view?

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    This runs error free on my system using straight ansi C:

    Code:
    #include <stdlib.h>
    #define BUFFSIZE 256
    int main(int argc, char *argv[]){
       char * buffer=NULL;
       buffer=malloc(BUFFSIZE * sizeof(char));
       memset(buffer, 0x00, (BUFFSIZE * sizeof(char)) );
       free(buffer);
    }
    You have to be diddling with buffer somewhere, or dereferencing it:

    buffer=NULL;

    someplace else in your code

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2003
    Posts
    68
    You did not call recv.
    I wonderd if that messed things up or not.

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    If you're talking about sockets (winsock), recv() does not invalidate the buffer pointer.

    You have to be doing something to the pointer yourself, in your code.

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