0

I am new to C programming. I am working on a project for school which requires me to make use of malloc.

My question is this: is it better to call malloc before or after you assign something to the pointer

for example:

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

    int main(int argc, char** argv){

        struct Node* mylist = //something

        //which of the following is the correct way to do this? 
        struct Node* node = mylist; 
        node = (struct Node*) malloc(1 * sizeof(struct Node)); 

        //or

        struct Node* node = (struct Node*) malloc(1 * sizeof(struct Node));
        node = mylist;
    }
9
  • 3
    Both ways are wrong. You are reading uninitialized variables (mylist) and the second alternative also leaks the memory pointed to by node by overwriting the node pointer. Commented Sep 25, 2018 at 14:44
  • 2
    mylist = malloc(sizeof *mylist);, followed by assignment of its members, is sufficient. Commented Sep 25, 2018 at 14:47
  • 1
    Detail: It is sizeof *mylist, not sizeof(mylist). As to what allocations are needed, you really should post more of how mylist is used. Commented Sep 25, 2018 at 14:49
  • 1
    Try the 2nd and use *node = *mylist;. But this is still a guess as you do not tell us what you actually are trying to achieve. Commented Sep 25, 2018 at 15:05
  • 2
    @Jabberwocky both implementations are more like fueling up the car then getting into the trashcan next to the pump and trying to drive that Commented Sep 25, 2018 at 18:56

2 Answers 2

3

You want something like this:

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

int main(int argc, char** argv){

    struct Node* mylist;  // declare the mylist pointer, it points nowhere right now

    mylist = malloc(1 * sizeof *mylist);   // roughly the same as "1 * sizeof(struct Node)"
                                           // now mylist points to a memory region of 
                                           // size (1 * sizeof(struct Node))
    // do something
    mylist->value = 1234;
    .....

    // once you're done 
    free(mylist); 
    // now mylist points nowhere again
}

I suggest you read the chapter dealing with pointers and dynamic memory allocation in your C text book.

Sign up to request clarification or add additional context in comments.

Comments

1

A linked list is a set of dynamically allocated nodes, arranged in such a way that each node contains one value and one pointer. The pointer always points to the next member of the list. If the pointer is NULL, then it is the last node in the list.

A linked list is held using a local pointer variable which points to the first item of the list. If that pointer is also NULL, then the list is considered to be empty.

Let's create a local variable which points to the first item of the list:

struct Node* mylist = NULL;
mylist = malloc(sizeof(struct Node));
mylist ->value = 1;
mylist ->next  = NULL;

We've just created the first variable in the list. We must set the value, and the next item to be empty, if we want to finish populating the list.

To add a variable to the end of the list, we can just continue advancing to the next pointer:

mylist ->next = malloc(sizeof(struct Node));
mylist ->next ->value = 2;
mylist ->next ->next  = NULL;

Check this link for more information : https://www.learn-c.org/en/Linked_lists

1 Comment

'To add a variable to the end of the list ...' -- your code is only correct if the list consists of one element only. Otherwise you just overwrite #2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.