malloc vs memset

sheek used Ask the Experts™
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 */
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
  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.
They are completely different functions.

malloc() reserves a block of memory from the heap.  memset () assigns a value to a block of memory.

main ()
  char *BlockData;

  BlockData = malloc (1000);  /*  reserved 1000 bytes  */

  memset (BlockData, 0, 1000);  /*  zero the block  */




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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial