c - Valgrind error but no leak -



c - Valgrind error but no leak -

this question has reply here:

c linked list valgrind invalid read of size 1 reply

my understanding every malloc should free before exit. based on valgrind report, not have leak. said, valgrind reporting code has error: address 0x402613c 4 bytes within block of size 8 free'd

for brevity, below snips of portions of linked list code shows node type , sections of code malloc or free node.

typedef struct node { int n; struct node* next; } node; // global variable head of list node* head = null; int main(void) { // user menu while (true) { printf("please take alternative (0, 1, 2): "); int alternative = getint(); switch (option) { // quit case 0: free_nodes(head); printf("goodbye!\n"); homecoming 0; // snipped: code calls insert , print functions bool insert_node(int value) { // create new node node* new_node = malloc(sizeof(node)); if (new_node == null) { homecoming false; } // snipped: code adds nodes linked list } /** * frees of nodes in list upon exiting program. */ void free_nodes(node* list) { // initialize pointer node* curr_node = head; // initialize variable end of list bool is_end = false; // free nodes while (!is_end) { // if empty list, free node , exit if (curr_node->next == null) { free(curr_node); is_end = true; } // else free node list until end of list if found else { free(curr_node); curr_node = curr_node->next; } } }

the error telling you're using pointer freed memory after freed it:

void *m = malloc(8); char *s = m + 4; free(m); *s = 29; // generate warning. int c = *s; // generate warning.

and, looking @ code, blatant illustration above (as bluepixy points out in comment):

free(curr_node); curr_node = curr_node->next;

fix:

node *next = curr_node->next; free(curr_node); curr_node = next;

c malloc valgrind free

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -