【发布时间】:2012-11-19 21:39:36
【问题描述】:
我收到了这个警告
warning C4309: 'initializing' : truncation of constant value
当我尝试执行我的 dll 时,它只发送 4 个字节而不是 10 个字节。
有什么问题?
这是我的代码:
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags)
{
cout << "[SEND:" << len << "] ";
for ( int i = 0; i < len; i++ ) {
printf( "%02x ", static_cast<unsigned char>( buf[i] ) );
}
printf("\n");
//causing the warning:
char storagepkt[] = {0x0A, 0x00, 0x01, 0x40, 0x79, 0xEA, 0x60, 0x1D, 0x6B, 0x3E};
buf = storagepkt;
len = sizeof(storagepkt);
return pSend(s, buf, len, flags);
}
更新
int (WINAPI *pSend)(SOCKET s, const char* buf, int len, int flags) = send;
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags);
更新
按照建议,我尝试了 memcpy:
memcpy((char*) buf, storagepkt, sizeof(storagepkt));
更新
unsigned char storagepkt[] = {0x0A, 0x00, 0x01, 0x40, 0x79, 0xEA, 0x60, 0x1D, 0x6B, 0x3E};
修复它。
【问题讨论】:
-
代码调用
pSend(),但没有显示。取而代之的是MySend()。是否有错别字或遗漏了什么? -
我在走弯路。两者都被声明了:)
-
@wallyk
pSend正在从内部调用MySend -
哪一行导致了警告信息?你确定 pSend() 真的有效吗?
-
我认为这只是在抱怨你正在用整数初始化一个 char 数组,但由于它们都适合一个 8 位(无符号)整数,你可以忽略警告