简体   繁体   中英

memory loss in the following C code

I am getting memory loss of 16 byte in the following code for queuing. 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:

             =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. 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.

This is where you allocated the lost memory.

Valgrind cannot report where you lost it, it only can keep track of allocations and deallocations.

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 ?

  [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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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