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().
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 overwritetempwith the value frompop()and then free whatpop()returned. That makes it impossible to free what was originally allocated. AnddeleteStack()needsfree(ps);at the end.deleteStackshould not go on to dops->topin the case ofps == NULL