[英]In C, how to malloc and free a SOCKET (already a pointer)
the SOCKET
is from winsock.h
SOCKET
来自winsock.h
Declared as 声明为
SOCKET BillerSocket;
As far as I know SOCKET
is already a pointer in disguise, so I shouldn't do free(*BillerSocket);
据我所知,
SOCKET
已经是伪装的指针,所以我不应该做free(*BillerSocket);
. 。
I malloc
it with 我用它
malloc
BillerSocket = malloc(sizeof(SOCKET));
I get this warning 我得到这个警告
[Warning] assignment makes integer from pointer without a cast [enabled by default] [警告]分配使指针从整数变为无强制转换[默认启用]
When I free
a SOCKET with 当我
free
了SOCKET
free(BillerSocket);
I get a warning and a Note which seems to tell me what I have to do. 我收到警告和注释,似乎可以告诉我该怎么办。
[Warning] passing argument 1 of 'free' makes pointer from integer without a cast [enabled by default] [警告]传递参数1到'free'使指针从整数开始而没有强制转换[默认启用]
129 0 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\combaseapi.h In file included from c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\combaseapi.h
14 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\objbase.h from c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\objbase.h
17 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\ole2.h from c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\ole2.h
12 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\wtypes.h from c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\wtypes.h
10 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\winscard.h from c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\winscard.h
97 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\windows.h from c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\windows.h
1 main.c
from main.c
438 16 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\stdlib.h
[Note] expected 'void *' but argument is of type 'SOCKET' [注意]预期为“ void *”,但参数为“ SOCKET”类型
How do I remove the warnings? 如何删除警告?
If you look at winsock.h
more closely, you will see that SOCKET
is not a pointer, it is an integer: 如果更仔细地查看
winsock.h
,您会发现SOCKET
不是指针,而是一个整数:
typedef UINT_PTR SOCKET;
UINT_PTR
is not a pointer to a UINT
( PUINT
and LPUINT
are). UINT_PTR
不是UINT
的指针( PUINT
和LPUINT
是)。 It is a UINT
that is the same size as a pointer. 它是与指针大小相同的
UINT
。 So UINT_PTR
is 4 bytes in a 32bit process and is 8 bytes in a 64bit process. 因此,
UINT_PTR
在32位进程中为4字节,在64位进程中为8字节。 See MSDN's documentation for more details on that: 有关更多详细信息,请参见MSDN文档:
Windows Data Types Windows数据类型
UINT_PTR
UINT_PTR
An unsigned INT_PTR.一个未签名的INT_PTR。
This type is declared in BaseTsd.h as follows:
在BaseTsd.h中声明此类型,如下所示:
#if defined(_WIN64) typedef unsigned __int64 UINT_PTR; #else typedef unsigned int UINT_PTR; #endif
You do not use malloc()
and free()
to manage SOCKET
values, you have to use socket()
and closesocket()
instead: 您无需使用
malloc()
和free()
来管理SOCKET
值,而必须使用socket()
和closesocket()
来代替:
SOCKET BillerSocket;
...
BillerSocket = socket(...);
...
closesocket(BillerSocket);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.