When I try to send a code to the testing server, I get an error, "memory leak"

#include <stdio.h> #include <stdlib.h> struct arr { int *a, min, k, cap; }; struct queue { struct arr **heap; int cap, count; }; struct queue initPriorityQueue (int); void insert(struct queue *, struct arr *); void swapQ(struct queue *, int, int); void deleteMin (struct queue *); void increaseKey (struct queue *); void heapify(struct queue *, int); int main(int argc, char *argv[]) { int k; scanf("%d", &k); int *arr = (int *)malloc(sizeof(int) * k); struct queue q = initPriorityQueue(k); for (int i = 0; i < k; i++) { scanf("%d", &arr[i]); } for (int i = 0; i < k; i++) { struct arr *elem = (struct arr *)malloc(sizeof(struct arr)); elem->a = (int *)malloc(sizeof(int) * arr[i]); for (int j = 0; j < arr[i]; j++) { scanf("%d", &elem->a[j]); } elem->min = 0; elem->cap = arr[i]; elem->k = elem->a[elem->min]; insert (&q, elem); } while (q.count > 0) { printf("%d ", q.heap[0]->k); if (q.heap[0]->min == q.heap[0]->cap - 1){ deleteMin(&q); } else { increaseKey(&q); } } for (int i = 0; i < k; i++) { free(q.heap[i]->a); free(q.heap[i]); } free(q.heap); free(arr); return 0; } struct queue initPriorityQueue(int n){ struct queue q; q.heap = (struct arr **)malloc(sizeof(struct arr *) * n); q.cap = n; q.count = 0; return q; } void insert (struct queue *q, struct arr *elem){ int i = q->count; q->count++; q->heap[i] = elem; for (;i > 0 && q->heap[(i - 1) / 2]->k > q->heap[i]->k; i = (i - 1) / 2) { swapQ(q, i, (i - 1) / 2); } } void swapQ (struct queue *q, int i, int j){ struct arr *t = q->heap[i]; q->heap[i] = q->heap[j]; q->heap[j] = t; } void increaseKey (struct queue *q){ int i = q->heap[0]->min; q->heap[0]->min++; q->heap[0]->k = q->heap[0]->a[i + 1]; heapify(q, 0); } void heapify(struct queue *q, int i){ int l = 2 * i + 1; int r = 2 * i + 2; int largest; if (l < q->count && q->heap[l]->k < q->heap[i]->k){ largest = l; }else largest = i; if (r < q->count && q->heap[r]->k < q->heap[largest]->k){ largest = r; } if (largest != i){ swapQ(q, i, largest); heapify(q, largest); } } void deleteMin (struct queue *q){ q->count--; if (q->count > 0){ q->heap[0] = q->heap[q->count]; heapify(q, 0); } } 

Closed due to the fact that participants from Vlad from Moscow , 0xdb , aleksandr barakin , iluxa1810 , Jarvis_J on Dec 31 '18 at 8:42 are off topic .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - aleksandr barakin, iluxa1810, Jarvis_J
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • And how did you decide that there is a memory leak? - Vlad from Moscow
  • @VladfromMoscow testing server does not accept, leaks - Andrei Vladislavov
  • What is a testing server? - NewView
  • You in main () in a loop allocate memory with the command: struct arr *elem = (struct arr *)malloc(sizeof(struct arr)); . And at each following iteration you forget about the selected piece (perevydelyaete new piece). And in general, I did not see that this elem (or did I just not notice?). - Vladimir
  • one
    Yes, I saw. Now the line in the deleteMin() function calls up the question: q->heap[0] = q->heap[q->count]; what's going on here? Where do <former> arrays elem and elem->a from q->heap[0] elem->a ? And yet, at the same time, should k decrease in that cycle, where you release memory at the end? - Vladimir

0