【问题标题】:Realloc stops program on second loopRealloc 在第二个循环中停止程序
【发布时间】:2017-11-20 04:26:34
【问题描述】:

我试图为我的数据结构分配一些内存(从互联网数据包中获取数据),但我遇到了一个问题,即我的程序在进入第二个循环时就停止了。第一个循环很完美,结构内的所有数据都是正确的。这是代码:

    struct ipOut *ipHeadr = NULL;
    struct ipAddr *addrHeadr = NULL;
    struct hexOut *hexHeadr = NULL;
    struct icmpOut *icmpHeadr = NULL;
    struct igmpOut *igmpHeadr = NULL;
    struct tcpOut *tcpHeadr = NULL;
    struct udpOut *udpHeadr = NULL;

    int main() {

ipHeadr = (struct ipOut*)malloc(sizeof(ipHeadr));
addrHeadr = (struct ipAddr*)malloc(sizeof(addrHeadr));
hexHeadr = (struct hexOut*)malloc(sizeof(hexHeadr));
icmpHeadr = (struct icmpOut*)malloc(sizeof(icmpHeadr));
igmpHeadr = (struct igmpOut*)malloc(sizeof(igmpHeadr));
tcpHeadr = (struct tcpOut*)malloc(sizeof(tcpHeadr));
udpHeadr = (struct udpOut*)malloc(sizeof(udpHeadr));

struct sockaddr saddr;
unsigned char *buff = (unsigned char *)malloc(65536);
int sock = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
if (sock < 0){
    printf ("Error creating socket");
    return 1;
}
int loop =  1;
while (loop == 1){
    int saddrLength = sizeof saddr;
    rawData = recvfrom(sock, buff, 65536, 0, &saddr, &saddrLength);
    printf("raw data %d\n", rawData);
    if(rawData <0 )
    {
        printf("Failed to get packets\n");
        return 1;
    }
    gettingPacket(buff, rawData);
}
return 0;
    }

    void gettingPacket(unsigned char * buff, int data){
packetNum++;
ipHeadr = (struct ipOut*)realloc(ipHeadr, sizeof(ipHeadr)*packetNum);
addrHeadr = (struct ipAddr*)realloc(addrHeadr,sizeof(addrHeadr)*packetNum);
hexHeadr = (struct hexOut*)realloc(hexHeadr, sizeof(hexHeadr)*packetNum);
struct iphdr *iph = (struct iphdr*)buff;


switch (iph -> protocol)
{
    case 1:
        icmpNum++;
        icmpHeadr = (struct icmpOut*)realloc(icmpHeadr, sizeof(icmpHeadr)*icmpNum);
        icmpOutput(buff, data);
        hexDataOut(buff, data);
        break;
    case 2:
        igmpNum++;
        igmpHeadr = (struct igmpOut*)realloc(igmpHeadr,sizeof(igmpHeadr)*igmpNum);
        igmpOutput(buff, data);
        hexDataOut(buff, data);
        break;
    case 6:
        tcpNum++;
        tcpHeadr = (struct tcpOut*)realloc(tcpHeadr,sizeof(tcpHeadr)*tcpNum);
        tcpPacketOutput(buff, data);
        hexDataOut(buff, data);
        break;
    case 17:
        udpNum++;
        udpHeadr = (struct udpOut*)realloc(udpHeadr, sizeof(udpHeadr)*udpNum);
        udpPacketOutput(buff, data);
        hexDataOut(buff, data);
        break;

}
    }

调试告诉代码在这一行停止:

    ipHeadr = (struct ipOut*)realloc(ipHeadr, sizeof(ipHeadr)*packetNum);

我认为某处存在内存泄漏,但我找不到。

【问题讨论】:

  • 提示 Type *pointer = malloc(sizeof(*pointer));, sizeof(pointer) 是指针大小。 sizeof(Type)sizeof(*pointer) 是类型大小。
  • 不要投malloc 和朋友的结果!
  • 谢谢你,它工作,我是白痴。 )
  • 使用Valgrind 查找泄漏。昨天发布了 3.13.0 版本。

标签: c memory struct malloc realloc


【解决方案1】:

你没有用任何数据填充你的缓冲区,所以它们有点没用。

另外,在您对 realloc 的调用中:

icmpHeadr = (struct icmpOut*)realloc(icmpHeadr, sizeof(icmpHeadr) * icmpNum);
/* the size given above is wrong, sizeof(icmpHeadr) is the size of a pointer. */

您应该将 realloc 和缓冲区内的数据副本放在一起。如:

void main(..)
{
void* tmp;

/*  no need to initialize your arrays here
    a NULL value would indicate an empty array */

/*...*/

  case 1:
    /* icmpHeadr can be NULL on first call to realloc that's not a problem
       but realloc could fail if you run out of memory...
    */
    tmp = realloc(icmpHeadr, sizeof(struct icmpOut)*(icmpNum + 1));
    if (tmp)
    {
       icmpHeadr = (struct icmpOut*)tmp;
       memcpy(&icmpHeadr[icmpNum], buff, sizeof(struct icmpOut));
       ++icmpNum;
       icmpOutput(buff, data);
       hexDataOut(buff, data);
    }
    else 
    {
      /* you've ran out of memory 
         You could keep counting icmp messages
         but that would mess your array size, in case memory 
         becomes available later.

         ...or you could simply exit your app.  Avoiding running out of
         memory would necessitate a major rewrite of your app, anyway.
      */
    }
    break;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-12
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多