C Question

mail@pastecode.io avatar
unknown
plain_text
3 years ago
4.7 kB
10
Indexable
Never
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

struct Node {
    int data;
    void (*print)(struct Node *node);
    struct Node *node;    
};

void print_node(struct Node *node)
{
    printf("%d \n", node->data);
    if (node->node != NULL)
        node->node->print(node->node);
}

void add_node_to_end(struct Node *node, int val)
{    
    while(node->node != NULL)
        node = node->node;

    node->node = malloc(sizeof(struct Node));
    node->node->data = val;
    node->node->print = &print_node;
}

void remove_node(struct Node *node, int val)
{
    struct Node *prev, *toRemove = NULL;
    while(node != NULL)
    {        
        if (node->data == val)
        {
            toRemove = node;
            break;
        }
        prev = node;
        node = node->node;
    }

    if (toRemove == NULL)    
        printf("Node with value %d not found. \n", val);
    else 
    {        
        if (toRemove->node == NULL)            
        {
            if (prev != NULL)
                prev->node = NULL;        
            else 
                node = NULL;
        }
        else         
            prev->node = toRemove->node;                    
        
        free(toRemove); 
    }
}

struct Node *init()
{
    struct Node *node = malloc(sizeof(struct Node));    
    node->data = 0;
    node->print = &print_node;
    return node;
}

void free_list(struct Node *node) 
{
    struct Node *tmp;
    while (node != NULL)
    {        
        tmp = node;
        node = node->node;
        free(tmp);
    }    
}

int main(int argc, char **argv)
{   
    struct Node *node = init();
    add_node_to_end(node, 10);
    add_node_to_end(node, 20);
    add_node_to_end(node, 30);
    node->print(node);
    free_list(node);
    return 0;
}
/* Valgrind errors:

==1664978== Memcheck, a memory error detector
==1664978== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1664978== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==1664978== Command: ./test
==1664978== 
==1664978== Conditional jump or move depends on uninitialised value(s)
==1664978==    at 0x1091CF: add_node_to_end (test.c:22)
==1664978==    by 0x109357: main (test.c:84)
==1664978==  Uninitialised value was created by a heap allocation
==1664978==    at 0x483A77F: malloc (vg_replace_malloc.c:307)
==1664978==    by 0x1092CD: init (test.c:64)
==1664978==    by 0x109342: main (test.c:83)
==1664978== 
==1664978== Conditional jump or move depends on uninitialised value(s)
==1664978==    at 0x1091CF: add_node_to_end (test.c:22)
==1664978==    by 0x109368: main (test.c:85)
==1664978==  Uninitialised value was created by a heap allocation
==1664978==    at 0x483A77F: malloc (vg_replace_malloc.c:307)
==1664978==    by 0x1091DA: add_node_to_end (test.c:25)
==1664978==    by 0x109357: main (test.c:84)
==1664978== 
==1664978== Conditional jump or move depends on uninitialised value(s)
==1664978==    at 0x1091CF: add_node_to_end (test.c:22)
==1664978==    by 0x109379: main (test.c:86)
==1664978==  Uninitialised value was created by a heap allocation
==1664978==    at 0x483A77F: malloc (vg_replace_malloc.c:307)
==1664978==    by 0x1091DA: add_node_to_end (test.c:25)
==1664978==    by 0x109368: main (test.c:85)
==1664978== 
==1664978== Conditional jump or move depends on uninitialised value(s)
==1664978==    at 0x109189: print_node (test.c:16)
==1664978==    by 0x1091A3: print_node (test.c:17)
==1664978==    by 0x1091A3: print_node (test.c:17)
==1664978==    by 0x1091A3: print_node (test.c:17)
==1664978==    by 0x10938A: main (test.c:87)
==1664978==  Uninitialised value was created by a heap allocation
==1664978==    at 0x483A77F: malloc (vg_replace_malloc.c:307)
==1664978==    by 0x1091DA: add_node_to_end (test.c:25)
==1664978==    by 0x109379: main (test.c:86)
==1664978== 
==1664978== Conditional jump or move depends on uninitialised value(s)
==1664978==    at 0x109324: free_list (test.c:73)
==1664978==    by 0x109396: main (test.c:88)
==1664978==  Uninitialised value was created by a heap allocation
==1664978==    at 0x483A77F: malloc (vg_replace_malloc.c:307)
==1664978==    by 0x1091DA: add_node_to_end (test.c:25)
==1664978==    by 0x109379: main (test.c:86)
==1664978== 
==1664978== 
==1664978== HEAP SUMMARY:
==1664978==     in use at exit: 0 bytes in 0 blocks
==1664978==   total heap usage: 5 allocs, 5 frees, 1,120 bytes allocated
==1664978== 
==1664978== All heap blocks were freed -- no leaks are possible
==1664978== 
==1664978== For lists of detected and suppressed errors, rerun with: -s
==1664978== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)


*/