[英]In C, how to malloc and free a SOCKET (already a pointer)
SOCKET
來自winsock.h
聲明為
SOCKET BillerSocket;
據我所知, SOCKET
已經是偽裝的指針,所以我不應該做free(*BillerSocket);
。
我用它malloc
BillerSocket = malloc(sizeof(SOCKET));
我得到這個警告
[警告]分配使指針從整數變為無強制轉換[默認啟用]
當我free
了SOCKET
free(BillerSocket);
我收到警告和注釋,似乎可以告訴我該怎么辦。
[警告]傳遞參數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
[注意]預期為“ void *”,但參數為“ SOCKET”類型
如何刪除警告?
如果更仔細地查看winsock.h
,您會發現SOCKET
不是指針,而是一個整數:
typedef UINT_PTR SOCKET;
UINT_PTR
不是UINT
的指針( PUINT
和LPUINT
是)。 它是與指針大小相同的UINT
。 因此, UINT_PTR
在32位進程中為4字節,在64位進程中為8字節。 有關更多詳細信息,請參見MSDN文檔:
UINT_PTR
一個未簽名的INT_PTR。在BaseTsd.h中聲明此類型,如下所示:
#if defined(_WIN64) typedef unsigned __int64 UINT_PTR; #else typedef unsigned int UINT_PTR; #endif
您無需使用malloc()
和free()
來管理SOCKET
值,而必須使用socket()
和closesocket()
來代替:
SOCKET BillerSocket;
...
BillerSocket = socket(...);
...
closesocket(BillerSocket);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.