简体   繁体   English

以下C代码中的内存丢失

[英]memory loss in the following C code

I am getting memory loss of 16 byte in the following code for queuing. 我在下面的代码排队中得到16字节的内存丢失。 Could you please let me know how can get rid of this problem? 您能否让我知道如何摆脱这个问题? The code is: 代码是:

      void enqueue( enqueuenode * queueNode1 ,bplus *bplusNew){
      [98] -> enqueue *queue=NULL;
              queue = malloc(sizeof(enqueue_node));
              queue->bplus = bplusNew;
              queue->next= NULL;
                if(queueNode1->headNode == NULL){
                   queueNode1->headNode=queueNode1->tailNode = queue ;
                   }
                 else{
                 queueNode1->tailNode->next = queue;
                 queueNode1->tailNode = queue;
                 }
            }

Following are two strucutres 以下是两个结构

         typedef struct enqueue_help{
           bplus bplusNode;
           struct enqueue_help * next;
         }*enqueue,enqueue_node;

        typedef struct enqueuenode_help{
          enqueue  headNode;
          enqueue  tailNode;
        }*enqueuenode,enqueuenode_node;

And for the above code following is the valgrind output: 对于上面的代码,以下是valgrind输出:

             =23800== 272 (16 direct, 256 indirect) bytes in 1 blocks are definitely lost in loss record 8 of 12
             ==23800==    at 0x4C2260E: malloc (vg_replace_malloc.c:207)
             ==23800==    by 0x4024BD:  enqueue(bplus.c:98)
             ==23800==    by 0x40260A:  PrintBplus (bplus.c:202)
             ==23800==    by 0x40286F: main (bplus.c:1251)
             ==23800== 

Here enqueuenode is the pointer for the structure which holds two enqueue as a head node and tail node. 这里enqueuenode是指针保持两个结构enqueue作为首节点和尾节点。 This is for traversal of queue during dequeuing. 这是为了在出队期间遍历队列。 Each queue is a pointer for the structure which holds some node address which needs to be queued. 每个queue都是结构的指针,该结构包含一些需要排队的节点地址。

This is where you allocated the lost memory. 在这里分配丢失的内存。

Valgrind cannot report where you lost it, it only can keep track of allocations and deallocations. Valgrind无法报告丢失的位置,它只能跟踪分配和释放。

Maybe you lose some nodes in one of your algorithms, which should be easy to test since the number of nodes decreases, but it's also possible that there is a bug in the code that frees the data structure. 也许您丢失了一种算法中的某些节点,由于节点数减少,因此应该很容易测试,但是代码中有可能会释放数据结构的错误。

Is there a reason why a pointer to enqueue is assigned a memory block that's sized for an enqueuenode ? 为什么要给指向enqueue的指针分配一个大小为enqueuenode的内存块,这是有原因的吗?

  [98] -> enqueue *queue=NULL;
          queue = malloc(sizeof(enqueuenode));

If, as you say, enqueuenode contains two enqueue 's, the second one could be the memory you're missing. 如您所说,如果enqueuenode包含两个enqueue ,则第二个可能是您缺少的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM