1

I am getting a two instances of memory leaks that I can't seem to solve. The first instance occurs from char *temp = (char*)malloc(sizeof(char*));, I use this variable at the beginning of a if statement and make sure to free it at the end, but I'm still getting a leak. The second occurs from s = create(); and s = (Stack*)malloc(sizeof(Stack));.

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{ char **data; int top; int size; }Stack; typedef enum { FALSE, TRUE } bool; Stack* create(){ Stack *s; s = (Stack*)malloc(sizeof(Stack)); s->top = -1; s->size = 10; s->data = (char**)malloc(s->size*sizeof(char*)); return s; } void deleteStack(Stack* ps){ if(ps == NULL){ printf("No memory allocated in Stack.\n"); } while(ps->top >= 0){ free(ps->data[ps->top]); ps->top--; } free(ps->data); } bool full( Stack s){ if(s.top == s.size-1) return TRUE; return FALSE; } void push( Stack* ps, char* str){ if(full(*ps)){ int tempsize = ps->size; ps->size += 10; ps->data = realloc(ps->data, ps->size * sizeof(char*)); // char **temp = realloc(ps->data, ps->size*sizeof(char*)); // if(temp == NULL){ // perror("realloc"); // printf("Error! memory not allocated.\n"); // exit(-1); // } // ps->data = temp; printf("Stack capacity has grown from %d elements to %d elements\n", tempsize, ps->size); } ps->data[++ps->top] = strdup(str); } bool empty( Stack s){ if(s.top == -1) return TRUE; return FALSE; } char* pop( Stack* ps){ if(empty(*ps)) return NULL; return ps->data[ps->top--]; } int main(int argc, char *argv[]){ printf("Assignment 2 Problem 1 by Jasmine Ramirez\n\n"); FILE *input = fopen("data_a2.txt", "r"); if(input == NULL){ perror("fopen"); printf("File %s not found.\n", "data_a2.txt"); exit(EXIT_FAILURE); } Stack *s; s = create(); <---16 bytes in 1 block definitely lost char str[255]; char *temp = (char*)malloc(sizeof(char)); <---1 bytes in 1 block definitely lost int i = 0; while(fscanf(input, "%s\n", str) == 1){ if(strcmp(str, "pop") == 0){ temp = pop(s); i--; printf("# of elements after popping: %d\tstring popped: %s\n", i, temp); free(temp); } else{ push(s, str); i++; } } deleteStack(s); fclose(input); return 0; } 

I can't figure out the reason for the 1 byte lost in temp and s is supposed to be taken care of by deleteStack().

4
  • 1
    In this code — char *temp = (char*)malloc(sizeof(char)); int i = 0; while(fscanf(input, "%s\n", str) == 1){ if(strcmp(str, "pop") == 0){ temp = pop(s); i--; printf("# of elements after popping: %d\tstring popped: %s\n", i, temp); free(temp); } you leak because you unconditionally overwrite temp with the value from pop() and then free what pop() returned. That makes it impossible to free what was originally allocated. And deleteStack() needs free(ps); at the end. Commented Jun 27, 2017 at 3:54
  • deleteStack should not go on to do ps->top in the case of ps == NULL Commented Jun 27, 2017 at 4:50
  • You are allowed to use empty lines when writing C code, turning it readable to humans. Commented Jun 27, 2017 at 8:43
  • Agree with @Lundin. Please make it human readable. Commented Jun 27, 2017 at 10:08

2 Answers 2

3

At this statement

temp = pop(s); 

Pointer to earlier malloced memory is lost so it is leaked.

In deleteStack, you have not freed ps, so that's a leak too, as memory allocated to Stack *s; in create never gets released.

Sign up to request clarification or add additional context in comments.

Comments

3

You should free(ps) at the end of deleteStack.

The temp pointer to the single mallocd char is overwritten with the result from pop, so it is leaked. Why do you need to allocate that single char? Just initialize it to NULL, or leave it uninitialized.

In production code, you should always test the value returned from malloc or realloc, and should never assign the result of realloc over your only pointer to the memory you are trying to reallocate. If it reurns NULL, you just leaked it.

Also, in deleteStack, you test for NULL, but then continue to dereference the pointer anyway.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.