// Watch malloc() fail.
// Note: This goes without a problem if the above call to WSAStartup()
// is commented out.
malloc(8);
if(errno !=0){
perror("Bad malloc!");
exit(errno);
}
return(0);
}
I compile and link with MinGW, like this:
$/c/MinGW/bin/g++ -o test.exe test.cpp -lws2_32
When I run test.exe, I get this:
$./test.exe
Bad malloc!: Result too large
If I comment out the call to WSAStartup(), the call to malloc() has no problem.
I have no errors when I compile and run the same program in the same way on Windows XP, instead of Windows 7.
If anyone has an explanation or workaround for this, I would really appreciate the help.
int main(int argc, char *argv[])
{
WSAData wsaData;
//ZeroMemory(wsaData, sizeof WSAData);
perror("Before WSAStartup()");
// Start Windows sockets
if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0){
fprintf(stderr,"Failed WSAStartup() \n");
return(-1);
}
printf("GetLastError(): %d\n", GetLastError());
perror("After WsaStartup");
errno = 0;
// Watch malloc() fail.
// Note: This goes without a problem if the above call to WSAStartup()
// is commented out.
malloc(8);
if(errno !=0){
perror("Bad malloc!");
exit(errno);
}
return(0);
}
This will print:
Microsoft Windows [Version 6.1.7600]
(c) 2009 Microsoft Corporation. Toate drepturile rezervate.
C:\Users\modoran\Folder nou\wsastartup\bin\Debug>wsastartup.exe
Before WSAStartup(): No error
GetLastError(): 0
After WsaStartup: Result too large
C:\Users\modoran\Folder nou\wsastartup\bin\Debug>
malloc does not write to errno global variable (or I am wrong ?)
EDIT: The problem is in MAKEWORD(2,2) macro, that is setting errno global variable, thre is no problem with WSAStartup(). You could ignore it.
I'm only asking because I don't know your skill level but instead of allocating 8 bytes of data for no aparent reason, would it be better to use the 'new' operator? By the way 'malloc(...)' returns a pointer to the block of memory that it makes that you aren't using so what ever your trying to do with this function is going to fail anyway.