【发布时间】:2012-05-06 22:14:41
【问题描述】:
在我的函数中,我为一个名为 messagePacket 的结构分配内存并填充它
struct messagePacket *packet = malloc(sizeof(struct messagePacket));
//fill
当我尝试将指针转换为 (uint8_t *) 时,gcc 会抛出一条警告:大整数隐式截断为无符号类型
sendBuf(..., (uint8_t *)packet);
我已经能够很好地执行以下操作,并且我知道我可以使用这种方法作为解决方法。我在这里是因为我宁愿从中学习也不愿解决它。
uint8_t *buf = malloc(sizeof(struct messagePacket));
struct messagePacket 的大小 = 1209 B。我最好的猜测是内存块超级大,我被存储在一个高内存地址中,例如 16 字节地址?但这不符合我可以 malloc 相同大小的 uint8_t * 的事实。
【问题讨论】:
-
sendBuf是做什么的? -
@cnicutar 我认为这并不重要,因为演员阵容似乎是个问题
-
@pivotnig 我认为这确实很重要,因为该演员阵容可能是多余的或完全错误的。按照标准,这已经是大错特错了。
-
sendBuf 接受 uint8_t *,创建类似的 uint8_t *recvBuf = sizeof(struct messagePacket),send() 的数据通过端口,select() 和 recv()数据
-
code #include <stdio.h> #include <stdlib.h> struct messagePacket { int a; int b[2000]; }; void sendBuf(char * d) { int c; c=*d; } int main() { struct messagePacket *packet = malloc(sizeof(struct messagePacket)); sendBuf((unsigned char*)packet); return 0; }我看不到错误。
标签: c memory-management casting compiler-errors truncate