Why won't this C program let me access memory?












0















I'm trying to do a linked list database and for some reason the program won't let me access the memory, or at least that's what the debugger says. I'm using the gdb debugger, codeblocks.



I've already compiled the program on another machine, with the same operating system (windows). It worked flawlessly, BUT the debugger showed the same error.



void print_ListEl(ListEl* head)
{
ListEl* current = malloc(sizeof(ListEl));
current = head;
if (head==NULL) exit(EXIT_FAILURE);
if (current->next==NULL)
{
puts("No elements");
return;
}
else
{
int i=1;
while(current->next!=NULL)
{
printf("%d.%sn", i, current->name);
current=current->next;
++i;
}
}
free(current);
}


It's used like that:



ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);


This is the function that seems to cause the problem according to the debugger. When I watch the "current->next" variable, the debugger says "Cannot access memory at address". If I change current->next to current, the debugger still shows the same function as causing the problem. The ListEl structure is just a regular single linked list with a char type data.



 struct ListEl
{
char name[MAX_CHAR];
struct ListEl* next;
};


I also use



typedef struct ListEl ListEl


Headers are safe with ifndef and endif and are all included, I've checked.



This function, instead of putting "No elements" when there's no elements in the list, spews out some random character and crashes the program.










share|improve this question




















  • 1





    Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what is head, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.

    – Sami Kuhmonen
    Jan 1 at 19:23











  • You should update the code to show what the function parameters are. Clearly you are using malloc and free but then comment elsewhere that you didn't mean to use it in that function. Is head passed into the function. Also malloc doesn't clear the data it points to. In this case element still needs to be initialized so that name and next have meaningful values.

    – Fred
    Jan 1 at 19:24











  • @user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.

    – Mihu
    Jan 1 at 20:00











  • @Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.

    – user10605163
    Jan 1 at 20:14
















0















I'm trying to do a linked list database and for some reason the program won't let me access the memory, or at least that's what the debugger says. I'm using the gdb debugger, codeblocks.



I've already compiled the program on another machine, with the same operating system (windows). It worked flawlessly, BUT the debugger showed the same error.



void print_ListEl(ListEl* head)
{
ListEl* current = malloc(sizeof(ListEl));
current = head;
if (head==NULL) exit(EXIT_FAILURE);
if (current->next==NULL)
{
puts("No elements");
return;
}
else
{
int i=1;
while(current->next!=NULL)
{
printf("%d.%sn", i, current->name);
current=current->next;
++i;
}
}
free(current);
}


It's used like that:



ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);


This is the function that seems to cause the problem according to the debugger. When I watch the "current->next" variable, the debugger says "Cannot access memory at address". If I change current->next to current, the debugger still shows the same function as causing the problem. The ListEl structure is just a regular single linked list with a char type data.



 struct ListEl
{
char name[MAX_CHAR];
struct ListEl* next;
};


I also use



typedef struct ListEl ListEl


Headers are safe with ifndef and endif and are all included, I've checked.



This function, instead of putting "No elements" when there's no elements in the list, spews out some random character and crashes the program.










share|improve this question




















  • 1





    Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what is head, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.

    – Sami Kuhmonen
    Jan 1 at 19:23











  • You should update the code to show what the function parameters are. Clearly you are using malloc and free but then comment elsewhere that you didn't mean to use it in that function. Is head passed into the function. Also malloc doesn't clear the data it points to. In this case element still needs to be initialized so that name and next have meaningful values.

    – Fred
    Jan 1 at 19:24











  • @user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.

    – Mihu
    Jan 1 at 20:00











  • @Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.

    – user10605163
    Jan 1 at 20:14














0












0








0








I'm trying to do a linked list database and for some reason the program won't let me access the memory, or at least that's what the debugger says. I'm using the gdb debugger, codeblocks.



I've already compiled the program on another machine, with the same operating system (windows). It worked flawlessly, BUT the debugger showed the same error.



void print_ListEl(ListEl* head)
{
ListEl* current = malloc(sizeof(ListEl));
current = head;
if (head==NULL) exit(EXIT_FAILURE);
if (current->next==NULL)
{
puts("No elements");
return;
}
else
{
int i=1;
while(current->next!=NULL)
{
printf("%d.%sn", i, current->name);
current=current->next;
++i;
}
}
free(current);
}


It's used like that:



ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);


This is the function that seems to cause the problem according to the debugger. When I watch the "current->next" variable, the debugger says "Cannot access memory at address". If I change current->next to current, the debugger still shows the same function as causing the problem. The ListEl structure is just a regular single linked list with a char type data.



 struct ListEl
{
char name[MAX_CHAR];
struct ListEl* next;
};


I also use



typedef struct ListEl ListEl


Headers are safe with ifndef and endif and are all included, I've checked.



This function, instead of putting "No elements" when there's no elements in the list, spews out some random character and crashes the program.










share|improve this question
















I'm trying to do a linked list database and for some reason the program won't let me access the memory, or at least that's what the debugger says. I'm using the gdb debugger, codeblocks.



I've already compiled the program on another machine, with the same operating system (windows). It worked flawlessly, BUT the debugger showed the same error.



void print_ListEl(ListEl* head)
{
ListEl* current = malloc(sizeof(ListEl));
current = head;
if (head==NULL) exit(EXIT_FAILURE);
if (current->next==NULL)
{
puts("No elements");
return;
}
else
{
int i=1;
while(current->next!=NULL)
{
printf("%d.%sn", i, current->name);
current=current->next;
++i;
}
}
free(current);
}


It's used like that:



ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);


This is the function that seems to cause the problem according to the debugger. When I watch the "current->next" variable, the debugger says "Cannot access memory at address". If I change current->next to current, the debugger still shows the same function as causing the problem. The ListEl structure is just a regular single linked list with a char type data.



 struct ListEl
{
char name[MAX_CHAR];
struct ListEl* next;
};


I also use



typedef struct ListEl ListEl


Headers are safe with ifndef and endif and are all included, I've checked.



This function, instead of putting "No elements" when there's no elements in the list, spews out some random character and crashes the program.







c debugging struct segmentation-fault dynamic-memory-allocation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 19:53







Mihu

















asked Jan 1 at 19:08









MihuMihu

395




395








  • 1





    Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what is head, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.

    – Sami Kuhmonen
    Jan 1 at 19:23











  • You should update the code to show what the function parameters are. Clearly you are using malloc and free but then comment elsewhere that you didn't mean to use it in that function. Is head passed into the function. Also malloc doesn't clear the data it points to. In this case element still needs to be initialized so that name and next have meaningful values.

    – Fred
    Jan 1 at 19:24











  • @user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.

    – Mihu
    Jan 1 at 20:00











  • @Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.

    – user10605163
    Jan 1 at 20:14














  • 1





    Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what is head, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.

    – Sami Kuhmonen
    Jan 1 at 19:23











  • You should update the code to show what the function parameters are. Clearly you are using malloc and free but then comment elsewhere that you didn't mean to use it in that function. Is head passed into the function. Also malloc doesn't clear the data it points to. In this case element still needs to be initialized so that name and next have meaningful values.

    – Fred
    Jan 1 at 19:24











  • @user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.

    – Mihu
    Jan 1 at 20:00











  • @Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.

    – user10605163
    Jan 1 at 20:14








1




1





Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what is head, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.

– Sami Kuhmonen
Jan 1 at 19:23





Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what is head, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.

– Sami Kuhmonen
Jan 1 at 19:23













You should update the code to show what the function parameters are. Clearly you are using malloc and free but then comment elsewhere that you didn't mean to use it in that function. Is head passed into the function. Also malloc doesn't clear the data it points to. In this case element still needs to be initialized so that name and next have meaningful values.

– Fred
Jan 1 at 19:24





You should update the code to show what the function parameters are. Clearly you are using malloc and free but then comment elsewhere that you didn't mean to use it in that function. Is head passed into the function. Also malloc doesn't clear the data it points to. In this case element still needs to be initialized so that name and next have meaningful values.

– Fred
Jan 1 at 19:24













@user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.

– Mihu
Jan 1 at 20:00





@user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.

– Mihu
Jan 1 at 20:00













@Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.

– user10605163
Jan 1 at 20:14





@Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.

– user10605163
Jan 1 at 20:14












2 Answers
2






active

oldest

votes


















1















  1. You are not supposed to malloc for current. You are replacing the pointer obtained from it immediately with head and loose the allocation. The later free will free the last element of the list, but will not set the pointer of the previous element to NULL, causing undefined behavior if the list is used later.



  2. You are allocating memory for a node here:



    ListEl* element = malloc(sizeof(ListEl));


    but you never set any of its values before calling print_listEl(element);. Therefore the pointer element->next will be indeterminate and comparison against NULL will not be useful. You dereference this pointer later in print_listEl causing undefined behavior. Always initialize after malloc:



    ListEl* element = malloc(sizeof(ListEl));
    element->next = NULL;
    memset(element->name, '', sizeof(element->name));







share|improve this answer

































    2














    may be



    ListEl* current = malloc(sizeof(ListEl));
    current = head;


    must be



    ListEl* current = malloc(sizeof(ListEl));
    head = current;


    else why to do the malloc and lost it immediately ?





    I hope you have additional code before



    if (current->next==NULL)


    because current is only allocated, not initialized





    When I watch the "current->next" variable, the debugger says "Cannot access memory at address".



    That means current was broken because for instance it was deleted, or you just never initialized the field next. the problem is you do not give Minimal, Complete, and Verifiable example



    If you code is really that :



    ListEl* element = malloc(sizeof(ListEl));
    print_listEl(element);


    element->name nor element->next are not initialized, so in print_listEl it is the same






    share|improve this answer


























    • Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.

      – Mihu
      Jan 1 at 19:15






    • 2





      @Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells

      – bruno
      Jan 1 at 19:22













    • True, the source is incomplete. @Mihu you need to use malloc or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.

      – Dan Farrell
      Jan 1 at 19:23











    • I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?

      – Mihu
      Jan 1 at 19:44











    • By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.

      – Mihu
      Jan 1 at 19:47











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998192%2fwhy-wont-this-c-program-let-me-access-memory%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1















    1. You are not supposed to malloc for current. You are replacing the pointer obtained from it immediately with head and loose the allocation. The later free will free the last element of the list, but will not set the pointer of the previous element to NULL, causing undefined behavior if the list is used later.



    2. You are allocating memory for a node here:



      ListEl* element = malloc(sizeof(ListEl));


      but you never set any of its values before calling print_listEl(element);. Therefore the pointer element->next will be indeterminate and comparison against NULL will not be useful. You dereference this pointer later in print_listEl causing undefined behavior. Always initialize after malloc:



      ListEl* element = malloc(sizeof(ListEl));
      element->next = NULL;
      memset(element->name, '', sizeof(element->name));







    share|improve this answer






























      1















      1. You are not supposed to malloc for current. You are replacing the pointer obtained from it immediately with head and loose the allocation. The later free will free the last element of the list, but will not set the pointer of the previous element to NULL, causing undefined behavior if the list is used later.



      2. You are allocating memory for a node here:



        ListEl* element = malloc(sizeof(ListEl));


        but you never set any of its values before calling print_listEl(element);. Therefore the pointer element->next will be indeterminate and comparison against NULL will not be useful. You dereference this pointer later in print_listEl causing undefined behavior. Always initialize after malloc:



        ListEl* element = malloc(sizeof(ListEl));
        element->next = NULL;
        memset(element->name, '', sizeof(element->name));







      share|improve this answer




























        1












        1








        1








        1. You are not supposed to malloc for current. You are replacing the pointer obtained from it immediately with head and loose the allocation. The later free will free the last element of the list, but will not set the pointer of the previous element to NULL, causing undefined behavior if the list is used later.



        2. You are allocating memory for a node here:



          ListEl* element = malloc(sizeof(ListEl));


          but you never set any of its values before calling print_listEl(element);. Therefore the pointer element->next will be indeterminate and comparison against NULL will not be useful. You dereference this pointer later in print_listEl causing undefined behavior. Always initialize after malloc:



          ListEl* element = malloc(sizeof(ListEl));
          element->next = NULL;
          memset(element->name, '', sizeof(element->name));







        share|improve this answer
















        1. You are not supposed to malloc for current. You are replacing the pointer obtained from it immediately with head and loose the allocation. The later free will free the last element of the list, but will not set the pointer of the previous element to NULL, causing undefined behavior if the list is used later.



        2. You are allocating memory for a node here:



          ListEl* element = malloc(sizeof(ListEl));


          but you never set any of its values before calling print_listEl(element);. Therefore the pointer element->next will be indeterminate and comparison against NULL will not be useful. You dereference this pointer later in print_listEl causing undefined behavior. Always initialize after malloc:



          ListEl* element = malloc(sizeof(ListEl));
          element->next = NULL;
          memset(element->name, '', sizeof(element->name));








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 1 at 20:07

























        answered Jan 1 at 20:01









        user10605163user10605163

        2,868624




        2,868624

























            2














            may be



            ListEl* current = malloc(sizeof(ListEl));
            current = head;


            must be



            ListEl* current = malloc(sizeof(ListEl));
            head = current;


            else why to do the malloc and lost it immediately ?





            I hope you have additional code before



            if (current->next==NULL)


            because current is only allocated, not initialized





            When I watch the "current->next" variable, the debugger says "Cannot access memory at address".



            That means current was broken because for instance it was deleted, or you just never initialized the field next. the problem is you do not give Minimal, Complete, and Verifiable example



            If you code is really that :



            ListEl* element = malloc(sizeof(ListEl));
            print_listEl(element);


            element->name nor element->next are not initialized, so in print_listEl it is the same






            share|improve this answer


























            • Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.

              – Mihu
              Jan 1 at 19:15






            • 2





              @Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells

              – bruno
              Jan 1 at 19:22













            • True, the source is incomplete. @Mihu you need to use malloc or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.

              – Dan Farrell
              Jan 1 at 19:23











            • I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?

              – Mihu
              Jan 1 at 19:44











            • By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.

              – Mihu
              Jan 1 at 19:47
















            2














            may be



            ListEl* current = malloc(sizeof(ListEl));
            current = head;


            must be



            ListEl* current = malloc(sizeof(ListEl));
            head = current;


            else why to do the malloc and lost it immediately ?





            I hope you have additional code before



            if (current->next==NULL)


            because current is only allocated, not initialized





            When I watch the "current->next" variable, the debugger says "Cannot access memory at address".



            That means current was broken because for instance it was deleted, or you just never initialized the field next. the problem is you do not give Minimal, Complete, and Verifiable example



            If you code is really that :



            ListEl* element = malloc(sizeof(ListEl));
            print_listEl(element);


            element->name nor element->next are not initialized, so in print_listEl it is the same






            share|improve this answer


























            • Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.

              – Mihu
              Jan 1 at 19:15






            • 2





              @Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells

              – bruno
              Jan 1 at 19:22













            • True, the source is incomplete. @Mihu you need to use malloc or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.

              – Dan Farrell
              Jan 1 at 19:23











            • I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?

              – Mihu
              Jan 1 at 19:44











            • By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.

              – Mihu
              Jan 1 at 19:47














            2












            2








            2







            may be



            ListEl* current = malloc(sizeof(ListEl));
            current = head;


            must be



            ListEl* current = malloc(sizeof(ListEl));
            head = current;


            else why to do the malloc and lost it immediately ?





            I hope you have additional code before



            if (current->next==NULL)


            because current is only allocated, not initialized





            When I watch the "current->next" variable, the debugger says "Cannot access memory at address".



            That means current was broken because for instance it was deleted, or you just never initialized the field next. the problem is you do not give Minimal, Complete, and Verifiable example



            If you code is really that :



            ListEl* element = malloc(sizeof(ListEl));
            print_listEl(element);


            element->name nor element->next are not initialized, so in print_listEl it is the same






            share|improve this answer















            may be



            ListEl* current = malloc(sizeof(ListEl));
            current = head;


            must be



            ListEl* current = malloc(sizeof(ListEl));
            head = current;


            else why to do the malloc and lost it immediately ?





            I hope you have additional code before



            if (current->next==NULL)


            because current is only allocated, not initialized





            When I watch the "current->next" variable, the debugger says "Cannot access memory at address".



            That means current was broken because for instance it was deleted, or you just never initialized the field next. the problem is you do not give Minimal, Complete, and Verifiable example



            If you code is really that :



            ListEl* element = malloc(sizeof(ListEl));
            print_listEl(element);


            element->name nor element->next are not initialized, so in print_listEl it is the same







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 1 at 20:05

























            answered Jan 1 at 19:13









            brunobruno

            10.4k21126




            10.4k21126













            • Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.

              – Mihu
              Jan 1 at 19:15






            • 2





              @Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells

              – bruno
              Jan 1 at 19:22













            • True, the source is incomplete. @Mihu you need to use malloc or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.

              – Dan Farrell
              Jan 1 at 19:23











            • I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?

              – Mihu
              Jan 1 at 19:44











            • By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.

              – Mihu
              Jan 1 at 19:47



















            • Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.

              – Mihu
              Jan 1 at 19:15






            • 2





              @Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells

              – bruno
              Jan 1 at 19:22













            • True, the source is incomplete. @Mihu you need to use malloc or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.

              – Dan Farrell
              Jan 1 at 19:23











            • I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?

              – Mihu
              Jan 1 at 19:44











            • By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.

              – Mihu
              Jan 1 at 19:47

















            Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.

            – Mihu
            Jan 1 at 19:15





            Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.

            – Mihu
            Jan 1 at 19:15




            2




            2





            @Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells

            – bruno
            Jan 1 at 19:22







            @Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells

            – bruno
            Jan 1 at 19:22















            True, the source is incomplete. @Mihu you need to use malloc or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.

            – Dan Farrell
            Jan 1 at 19:23





            True, the source is incomplete. @Mihu you need to use malloc or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.

            – Dan Farrell
            Jan 1 at 19:23













            I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?

            – Mihu
            Jan 1 at 19:44





            I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?

            – Mihu
            Jan 1 at 19:44













            By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.

            – Mihu
            Jan 1 at 19:47





            By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.

            – Mihu
            Jan 1 at 19:47


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998192%2fwhy-wont-this-c-program-let-me-access-memory%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith