c - Valgrind blocks are lost -


i'm trying write data structure stack in c. each element of stack string. below 3 files used. first 1 main other ones header stack.h , file stack.c. declared stackelement pointer element. stackt struct define stack. problem appears when trying free allocate memory, because when run valgrind

==31235== error summary: 2 errors 2 contexts (suppressed: 0 0) ==31235==  ==31235== 1 errors in context 1 of 2: ==31235== invalid free() / delete / delete[] / realloc() ==31235==    @ 0x4c2bdec: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31235==    0x400893: stackdestroy (in /test) ==31235==    0x400b61: main (in /test) ==31235==  address 0x51fc150 0 bytes inside block of size 4 free'd ==31235==    @ 0x4c2bdec: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31235==    0x400b55: main (in /test)  24 bytes in 1 blocks lost in loss record 1 of 1 

could me please?

int main(){ stackt s; int r = 3, i; stackinit(&s, 2, r); printf("test\n"); stackelementt s0; s0 = malloc((r + 1) * sizeof(unsigned char)); memset(s0, 1, r + 1); s0[r] = 0; (i = 0; < r; i++) {     s0[i] = random() & 0xff; } stackpush(&s, s0); free(s0); stackdestroy(&s); return 0; } 

stack.h

typedef unsigned char * stackelementt; typedef struct {   stackelementt *contents;   int maxsize;   int top; } stackt; 

stack.c

void stackinit(stackt *stackp, int maxsize, int r) { stackelementt *newcontents; int i; r = 3; newcontents = malloc(sizeof(stackelementt) * maxsize);  if (newcontents == null) {         fprintf(stderr, "insufficient memory initialize stack.\n");         exit(1);  }  (i = 0; < maxsize; i++) {     if ((newcontents[i] = malloc(r * sizeof(stackelementt))) == null) {         fprintf(stderr, "insufficient memory initialize stack.\n");         exit(1);      } }  stackp->contents = newcontents; stackp->maxsize = maxsize; stackp->top = -1; /* i.e., empty */ }  void stackdestroy(stackt *stackp) { int i; (i = 0; < stackp->maxsize; i++)     free(stackp->contents[i]); free(stackp->contents); stackp->contents = null; stackp->maxsize = 0; stackp->top = -1;  } void stackpush(stackt *stackp, stackelementt element) { if (stackisfull(stackp)) {     fprintf(stderr, "can't push element on stack: stack full.\n");     exit(1);  } stackp->contents[++stackp->top] = element; } 

edit:

using answers below comment line free(s0), next error now:

==31855== 24 bytes in 1 blocks lost in loss record 1 of 1 ==31855==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31855==    0x4007f9: stackinit (in /test) ==31855==    0x400aa9: main (in /test) 

the first valgrind message pretty clear: you're freeing same block of memory twice, first free(s0) in main , second time in stackdestroy. chang code doesn't - either having stack take ownership of objects (so frees them , main doesn't), or vice-versa (the stack not take ownership of objects , not free them).

the "blocks lost" matter because allocate blocks of memory each stack slot in loop in stackinit, , stackpush overwrites pointers can never release original memory allocated used stack slots. there @ least 2 possible resolutions here well: don't pre-allocate blocks of memory in stackinit (just copy pointers in stackpush), or copy element data (instead of pointer) in stackpush.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -