Thread: Linked List and Dynamic Memory Allocation

  1. #1
    Registered User
    Join Date
    Feb 2009

    Unhappy Linked List and Dynamic Memory Allocation


    I'm a newbie to C programming and currently very confused. We are studying linked lists right now and our assignment is to create a dictionary program variables.c that stores and retrieves name-value pairs.

    I have been working on this for hours and I'm not even sure what I have is correct. Could someone please help me? Even if it's just to tell me the I'm on the right track.

    Here's what I've written so far:

    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <string.h>
    #include "variables.h"
    typedef struct Variable
        double value;
        char* name;
        struct Variable* next;
        struct Variable* prev;
    } Variable_t;
    void set_variable(const char* name, double value)
        Variable_t* new_variable_node(const char* name, double value)
            Variable_t* new_node = malloc(size of(Variable_t));
            assert(new_node != NULL);
            new_node->next = NULL;
            new_node->prev = NULL;
            new_node->value = value;
            new_node->name = malloc(strlen(name) + 1);
            assert(new_node->name != NULL);
            strcpy(new_node->name, name);
            return new_node;
    double variable_value(const char* name)
    The files my professor gave are

    #define MAX_VARIABLE_LENGTH 15
    /* Set the variable with the specified name to the specified value.
    * If there is not a variable with that name in the dictionary,
    * add one. */
    void set_variable (const char* name, double value);
    /* Return the value of the variable with the specified name.
    * If variable does not exist, add it to the dictionary
    * with a default value of 0.0, and return that value. */
    double variable_value(const char* name);
    /* Display the names and values of all variables in the dictionary. */
    void display_variables();
    #include <stdio.h>
    #include "variables.h"
    void verify (double result, double expected_value)
        if (result != expected_value)
            printf ("Incorrect result.  Should be %8.4f\n\n", expected_value);
    int main()
        double result;
        printf ("Starting dictionary test\n\n");
        set_variable("A1", 1.0);
        set_variable("A2", 2.0);
        set_variable("A3_3333", 3.3333);
        result = variable_value("A1");
        printf ("A1 is %8.4f.\n", result);
        verify(result, 1.0);
        result = variable_value("A2");
        printf ("A2 is %8.4f.\n", result);
        verify(result, 2.0);
        result = variable_value("A3_3333");
        printf ("A3_333 is %8.4f\n", result);
        verify(result, 3.3333);
        set_variable("A long variable", -1.0);
        result = variable_value("A long variable");
        printf ("A long variable is %8.4f\n", result);
        verify(result, -1.0);
        result = variable_value("Unset Variable ");
        printf ("Unset Variable  is %8.4f\n", result);
        verify(result, 0.0);
        printf ("\n");
        printf ("All variables:\n");
        printf ("Dictionary test complete\n");
        return 0;
    Any help will be GREATLY appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Did you mess up pasting the code you had written so far? You currently have a function within another function which isn't right. Also, you cut off the variable_value() function.

  3. #3
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    assert should be used to catch the programmers errors during debugging.

    malloc returning NULL is not a programmers error - it is possible condition indicating out of memory

    so it should be handled in the release version as well.

    in your case program will just crash in release
    To be or not to be == true

  4. #4
    Registered User
    Join Date
    Oct 2008
    Your professor sure leaves a lot of blanks to be filled in. As noted before you cannot have nested function definitions and you need a starting place for doubly-linked list. See the faq which has a very good writeup on linked lists.

  5. #5
    Registered User
    Join Date
    Oct 2008
    The way it's currently setup, the root node has to be a global variable unless the function declarations can be altered to accept the root node as an argument and return it as well (instead of void).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is dynamic memory allocation
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 06-08-2009, 08:11 AM
  2. Linked list probs
    By mouse163 in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 04:41 PM
  3. Help here with qsort!
    By xxxrugby in forum C Programming
    Replies: 11
    Last Post: 02-09-2005, 07:52 AM
  4. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 08:09 PM
  5. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM