Link to home
Start Free TrialLog in
Avatar of sheek
sheek

asked on

malloc vs memset

I was wondering what's the difference between malloc and memset and in what situations do you use one over the other.

In this senario is it possible to use malloc instead of memset?


/* Construct the server address structure */

    memset(&echoServAddr, 0, sizeof(echoServAddr));     /* Zero out structure */
    echoServAddr.sin_family      = AF_INET;             /* Internet address family */
    echoServAddr.sin_addr.s_addr = inet_addr(servIP);   /* Server IP address */      
    echoServAddr.sin_port        = htons(echoServPort); /* Server port */
    /* Establish the connection to the echo server */
Avatar of knightmad
knightmad

  Well, as far as I know, memset is used when you want to fill a previously allocated buffer with certain value, while malloc is used to allocate memory ... I dont believe you can use malloc in this situation, because (it seems like) echoServAddr is not a pointer, don't need to be allocated, only filled with zeroes.
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sheek

ASKER

Thanks Knightmad and kdo, sorry knightmad i couldn't split the points couldn't figure out how to do it.
No problem :) too few points to split