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_ad dr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Establish the connection to the echo server */
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_ad
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Establish the connection to the echo server */
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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