WSAStartup causes malloc() failure on Windows 7


On my Windows 7 platform, when I call WSAStartup(), a subsequent call to malloc() fails. Here is the entire test program:

/*******************************************************************************
* test.cpp
* --------
* Trying to figure out why WSAStartup causes malloc to fail.
*******************************************************************************/
#include <stdio.h>
#include <windows.h>

int main(int argc, char *argv[])
{
WSAData wsaData;

// Start Windows sockets
if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0){
fprintf(stderr,"Failed WSAStartup() \n");
return(-1);
}

// 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.

malloc() return a NULL pointer on failure, but you don't check that in this code ...

However, i compiled and tested myself that code in windows 7 and WSAStartup() itself failed. Look at this test code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.
Last edited on
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.
Topic archived. No new replies allowed.