0

iam trying to use a simple linked list in C but I have some trouble with it. I created a struct node

struct node{
    int value;
    struct node *next;
};

and wrote in the main following code

struct node *root;
struct node *conductor;

root = (struct node *)malloc(sizeof(struct node));
root->next = 0;
conductor = root;       

root->value = 1;

if ((root->value) == 1) 
    LED_GREEN = 1;
//LED_GREEN = 1;

I'm running it on an embedded system and just compare the value of the root node. I would expect, the LED is going on, but it isn't. Anyone got an idea, why this is not working as expected?

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
xy36
  • 315
  • 1
  • 11
  • 1
    What is `LED_GREEN`? Also [read this](http://stackoverflow.com/a/605858/1983495). And what happens if you don't use the `if`? – Iharob Al Asimi Jan 20 '16 at 11:53
  • root->next = 0; is wrong. It should point to valid memory location – GorvGoyl Jan 20 '16 at 11:55
  • 1
    @JerryGoyal How is it wrong? That's how you initialize the first node a linked list. – Lundin Jan 20 '16 at 11:56
  • 4
    Agree with the above... And two other things. Firstly, don't use 0 as a substitute for null. Use null. Secondly, don't cast the return from Malloc. It's (void *) for a reason! You don't need to cast it and casting it can conceal errors. Coupled with this, you should absolutely verify that Malloc successfully returned memory to you before you start assigning things into it. :) – David Hoelzer Jan 20 '16 at 11:56
  • LED_GREEN is mapped to a outputpin of the controller. The command "LED_GREEN = 1" works fine. I tested it without using the if-statement. – xy36 Jan 20 '16 at 11:56
  • 4
    @xy36 Then the problem cannot be reproduced with the code posted. – Lundin Jan 20 '16 at 11:57
  • don't cast the result of malloc: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Viktor Simkó Jan 20 '16 at 12:04
  • well, but the code is not working as expected. – xy36 Jan 20 '16 at 12:23
  • I checked the return value of malloc, and it seems to be 0... – xy36 Jan 20 '16 at 12:26
  • 1
    @xy36 then that's your problem, it means malloc failed. Do you #include ? Does the compiler/platform you're using support dynamic memory allocation at all ? – nos Jan 20 '16 at 12:29
  • Yeah it should be. I already tried other datatypes as struct node.... its always returning a 0. It seems theres a problem with my compiler/platform... – xy36 Jan 20 '16 at 12:33
  • is included – xy36 Jan 20 '16 at 12:33

2 Answers2

2

I solved the problem myself, but maybe its helpful for similar problems, when i post the solution. The problem was solved by change the size of the heap size in the project options of my IDE. The size was set to 0, hence malloc couldnt allocate memory.

xy36
  • 315
  • 1
  • 11
  • And this should make you remember ALWAYS to check the return value of `malloc` - I bet it returned NULL when the heap size was 0 ? – Morten Jensen Jan 20 '16 at 15:31
  • But I got some help from the Support of the IDE. Usually there should be an linking error cause of the missing heap size but it wasn't. Anyway, thanks for the help! – xy36 Jan 20 '16 at 15:48
0

@xy36 is right, and this error can`t be reproduced with the code posted. But, if you need a list, you can use the code bellow. I just improved a little your code. About the var LED_GREEN, if you want to change it's value, I sugget you to change the code inside the function addNode. If you are using an embbed board, like arduino, just check your wires connections and don't forget to use the command "digitalWrite(pin, value);" to change you led value.

Good luck.

    #include <stdio.h>
#include <stdlib.h>

struct node{
    int ID;
    int value;
    struct node *next;
};

int LED_GREEN = 0;

struct node * addNode(struct node *conductor, int value){
    struct node * newNode;
    newNode = (struct node *)malloc(sizeof(struct node));
    newNode->value = value;
    newNode->ID = conductor->ID + 1;
    conductor->next = newNode;
    newNode->next = NULL;     
    printf("Node added.\n");
    return newNode;
}

void printList(struct node *root){
    struct node *conductor = NULL;
    conductor = root;

    while(conductor){
        printf("Node[%d] value: %d. \n",conductor->ID, conductor->value);
        conductor = conductor->next;
    }
    return;
}

int main()
{
    struct node *root =NULL;
    struct node *conductor = NULL;

    if(!root){
        root = (struct node *)malloc(sizeof(struct node));
        root->next = 0;
        conductor = root;     
        root->value = 1;
        root->ID = 0;
    }

    conductor = addNode(conductor, 3);
    conductor = addNode(conductor, 5);
    conductor = addNode(conductor, 7);
    conductor = addNode(conductor, 11);
    conductor = addNode(conductor, 13);

    printList(root);
    return 0;
}
FViel
  • 1
  • 1