I am writing a function to wrap the winsock2.h function gethostname. I am concerned I have introduced a memory leak by using malloc, as if I put a free() after the return statement it will not fire. Can someone guide me?
Charlie Benger-Stevenson
Hart Hill IT Ltd
Code:
char * getLocalHostName(){
char * hostName = (char *)malloc(256);
int nameLen = 256;
if (gethostname(hostName,nameLen)>0){
return "Error retrieving local host name";
}
else{
return hostName;
free(hostName);
}
}
Charlie Benger-Stevenson
Hart Hill IT Ltd