top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Errors!! Memory is free.

+2 votes
459 views

Can you tell me why I have bunch of errors here?? All memory is free....But I have to fix error....I cant find them :(

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"

#define STRSIZE 128 /*assume string representation fits*/

/* Return true if the list h is empty
 * and false otherwise.
 */
int is_empty(List *h) {
    return h == NULL;
}

/* Create a list node with value v and return a pointer to it 
 */
List *create_node(int v) {
    List *node = malloc(sizeof(List));
    free(node);
    node->value = v;
    node->next = NULL;
    return node;
}
/* Insert a new node with the value v at the 
 * front of the list. Return the new head of 
 * the list.
 */
List *add_node(List *h, int v) {
    List *node = create_node(v);
    node->next = h;
    return node;
}

/* Remove the first node in the list h that
 * has the value v.  Return the head of the
 * list.
 */
List *remove_node(List *h, int v) {
    List *curr = h;

    /* Handle the cases when list is empty or the value
     * is found at the front of the list */

if (h == NULL) {
        return h;
    } else if (h->value == v) {
        h = h->next;
        return h;
    }

    /* Look for the value in the list (keeping the pointer to
     * the previous element so that we can remove the right 
     * element) */
    while (curr->next != NULL && curr->next->value != v) {
        curr = curr->next;
    }

    if (curr->next == NULL) { /* v is not in list so do nothing */
        return h;
    } else { /* curr->next->value == v */
        curr->next = curr->next->next;
        return h;
    }
}



/* Return a string representation of the list pointed to by h.
 */
char *tostring(List *h) {
    char *str= malloc(STRSIZE);
    char num[4]; /* assume integer has max. four digits*/
    free(str);
    str[0] = '\0';
    while (h != NULL) {
        sprintf(num, "%d", h->value);
        strncat(str, num, STRSIZE - strlen(str) - 1);
        h = h->next;
    }
    return str;
}
posted Feb 6, 2014 by Manu Lakaster

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Can you be specific what is your problem, no one can read your complete program and do dry run (and we dont have context what is the program, why are you doing it and what u tried), please be specific and to the point.

1 Answer

+3 votes
 
Best answer

As the complete code u have not written but i just find out a problem in this block of statement ...

/* Create a list node with value v and return a pointer to it
*/
List *create_node(int v) {
List *node = malloc(sizeof(List));
free(node);
node->value = v;
node->next = NULL;
return node;
}

u are creating a new node but after allocating memory for node u are freeing it and then assigning value.

answer Feb 7, 2014 by Seekha Dash
Similar Questions
+4 votes

Means, at the time of calling the function , local variable get memory first OR argument ?

+4 votes
printf ("%s %d %f\n", (*ptr+i).a, (*ptr+i).b, (*ptr+i).c);
[Error] invalid operands to binary + (have 'struct name' and 'int')

where ptr is a pointer to the structure and allocated dynamic memory using malloc. Any suggestion would be helpful.

Thanks in advance.

...