Error: “Access not within mapped region at address” (Valgrind)









up vote
0
down vote

favorite












i am having a probem with valgrind givin me an error saying "Access not within mapped region at address 0x8". It then says "at 0x400606: append_linked_list (testing2.c:64) by 0x400563: main (testing2.c:32)". Line 64 is list->tail->next = newNode;, and line 32 is just calling the function which line 64 is in append_linked_list(list, (void *) argv[i]);. When i run the program i just run it as "./testing this is a fairly short test string.". Does anyone know why valgrind is giving me this error?



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

typedef struct Node

void *data;
struct Node *next;
struct Node *prev;
Node;

typedef struct LinkedList

Node *head;
Node *tail;
LinkedList;

Node *initialise_node(void);
LinkedList *initialise_linked_list(void);
Node *append_linked_list(LinkedList *list, void *data);

int main(int argc, char **argv)

LinkedList *list;
int i;

list = initialise_linked_list();


for(i = 1; i < argc; i++)

append_linked_list(list, (void *) argv[i]);

return 0;


Node *initialise_node(void)

Node *node;

node = (Node *) malloc(sizeof(Node));

return node;



LinkedList *initialise_linked_list(void)

LinkedList *list;

list = (LinkedList *) malloc(sizeof(LinkedList));
list->head = NULL;
list->tail = NULL;

return list;


Node *append_linked_list(LinkedList *list, void *data)

Node *newNode = initialise_node();

newNode->data = data;
newNode->prev = list->tail;
list->tail->next = newNode;
newNode->next = NULL;

return newNode;











share|improve this question





















  • Address 0x8 is suspiciously low. From that part of Valgrind's message alone, it seems likely that the program is dereferencing an invalid pointer.
    – John Bollinger
    Nov 10 at 16:52







  • 1




    you have initialized list->tail = NULL; and you are trying to dereference it list->tail->next = newNode; You probably need to learn how to insert node at the end using tail. Lots of code missing in append_linked_list.
    – kiran Biradar
    Nov 10 at 16:57















up vote
0
down vote

favorite












i am having a probem with valgrind givin me an error saying "Access not within mapped region at address 0x8". It then says "at 0x400606: append_linked_list (testing2.c:64) by 0x400563: main (testing2.c:32)". Line 64 is list->tail->next = newNode;, and line 32 is just calling the function which line 64 is in append_linked_list(list, (void *) argv[i]);. When i run the program i just run it as "./testing this is a fairly short test string.". Does anyone know why valgrind is giving me this error?



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

typedef struct Node

void *data;
struct Node *next;
struct Node *prev;
Node;

typedef struct LinkedList

Node *head;
Node *tail;
LinkedList;

Node *initialise_node(void);
LinkedList *initialise_linked_list(void);
Node *append_linked_list(LinkedList *list, void *data);

int main(int argc, char **argv)

LinkedList *list;
int i;

list = initialise_linked_list();


for(i = 1; i < argc; i++)

append_linked_list(list, (void *) argv[i]);

return 0;


Node *initialise_node(void)

Node *node;

node = (Node *) malloc(sizeof(Node));

return node;



LinkedList *initialise_linked_list(void)

LinkedList *list;

list = (LinkedList *) malloc(sizeof(LinkedList));
list->head = NULL;
list->tail = NULL;

return list;


Node *append_linked_list(LinkedList *list, void *data)

Node *newNode = initialise_node();

newNode->data = data;
newNode->prev = list->tail;
list->tail->next = newNode;
newNode->next = NULL;

return newNode;











share|improve this question





















  • Address 0x8 is suspiciously low. From that part of Valgrind's message alone, it seems likely that the program is dereferencing an invalid pointer.
    – John Bollinger
    Nov 10 at 16:52







  • 1




    you have initialized list->tail = NULL; and you are trying to dereference it list->tail->next = newNode; You probably need to learn how to insert node at the end using tail. Lots of code missing in append_linked_list.
    – kiran Biradar
    Nov 10 at 16:57













up vote
0
down vote

favorite









up vote
0
down vote

favorite











i am having a probem with valgrind givin me an error saying "Access not within mapped region at address 0x8". It then says "at 0x400606: append_linked_list (testing2.c:64) by 0x400563: main (testing2.c:32)". Line 64 is list->tail->next = newNode;, and line 32 is just calling the function which line 64 is in append_linked_list(list, (void *) argv[i]);. When i run the program i just run it as "./testing this is a fairly short test string.". Does anyone know why valgrind is giving me this error?



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

typedef struct Node

void *data;
struct Node *next;
struct Node *prev;
Node;

typedef struct LinkedList

Node *head;
Node *tail;
LinkedList;

Node *initialise_node(void);
LinkedList *initialise_linked_list(void);
Node *append_linked_list(LinkedList *list, void *data);

int main(int argc, char **argv)

LinkedList *list;
int i;

list = initialise_linked_list();


for(i = 1; i < argc; i++)

append_linked_list(list, (void *) argv[i]);

return 0;


Node *initialise_node(void)

Node *node;

node = (Node *) malloc(sizeof(Node));

return node;



LinkedList *initialise_linked_list(void)

LinkedList *list;

list = (LinkedList *) malloc(sizeof(LinkedList));
list->head = NULL;
list->tail = NULL;

return list;


Node *append_linked_list(LinkedList *list, void *data)

Node *newNode = initialise_node();

newNode->data = data;
newNode->prev = list->tail;
list->tail->next = newNode;
newNode->next = NULL;

return newNode;











share|improve this question













i am having a probem with valgrind givin me an error saying "Access not within mapped region at address 0x8". It then says "at 0x400606: append_linked_list (testing2.c:64) by 0x400563: main (testing2.c:32)". Line 64 is list->tail->next = newNode;, and line 32 is just calling the function which line 64 is in append_linked_list(list, (void *) argv[i]);. When i run the program i just run it as "./testing this is a fairly short test string.". Does anyone know why valgrind is giving me this error?



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

typedef struct Node

void *data;
struct Node *next;
struct Node *prev;
Node;

typedef struct LinkedList

Node *head;
Node *tail;
LinkedList;

Node *initialise_node(void);
LinkedList *initialise_linked_list(void);
Node *append_linked_list(LinkedList *list, void *data);

int main(int argc, char **argv)

LinkedList *list;
int i;

list = initialise_linked_list();


for(i = 1; i < argc; i++)

append_linked_list(list, (void *) argv[i]);

return 0;


Node *initialise_node(void)

Node *node;

node = (Node *) malloc(sizeof(Node));

return node;



LinkedList *initialise_linked_list(void)

LinkedList *list;

list = (LinkedList *) malloc(sizeof(LinkedList));
list->head = NULL;
list->tail = NULL;

return list;


Node *append_linked_list(LinkedList *list, void *data)

Node *newNode = initialise_node();

newNode->data = data;
newNode->prev = list->tail;
list->tail->next = newNode;
newNode->next = NULL;

return newNode;








c valgrind






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 16:49









Eskil

1




1











  • Address 0x8 is suspiciously low. From that part of Valgrind's message alone, it seems likely that the program is dereferencing an invalid pointer.
    – John Bollinger
    Nov 10 at 16:52







  • 1




    you have initialized list->tail = NULL; and you are trying to dereference it list->tail->next = newNode; You probably need to learn how to insert node at the end using tail. Lots of code missing in append_linked_list.
    – kiran Biradar
    Nov 10 at 16:57

















  • Address 0x8 is suspiciously low. From that part of Valgrind's message alone, it seems likely that the program is dereferencing an invalid pointer.
    – John Bollinger
    Nov 10 at 16:52







  • 1




    you have initialized list->tail = NULL; and you are trying to dereference it list->tail->next = newNode; You probably need to learn how to insert node at the end using tail. Lots of code missing in append_linked_list.
    – kiran Biradar
    Nov 10 at 16:57
















Address 0x8 is suspiciously low. From that part of Valgrind's message alone, it seems likely that the program is dereferencing an invalid pointer.
– John Bollinger
Nov 10 at 16:52





Address 0x8 is suspiciously low. From that part of Valgrind's message alone, it seems likely that the program is dereferencing an invalid pointer.
– John Bollinger
Nov 10 at 16:52





1




1




you have initialized list->tail = NULL; and you are trying to dereference it list->tail->next = newNode; You probably need to learn how to insert node at the end using tail. Lots of code missing in append_linked_list.
– kiran Biradar
Nov 10 at 16:57





you have initialized list->tail = NULL; and you are trying to dereference it list->tail->next = newNode; You probably need to learn how to insert node at the end using tail. Lots of code missing in append_linked_list.
– kiran Biradar
Nov 10 at 16:57













2 Answers
2






active

oldest

votes

















up vote
0
down vote














"Access not within mapped region at address 0x8". It then says "at 0x400606: append_linked_list (testing2.c:64) by 0x400563: main (testing2.c:32)". Line 64 is list->tail->next = newNode; [in function append_linked_list]




Your append_linked_list() function is broken for the case in which the list is empty, and in particular, the line identified by Valgrind performs an invalid memory access, just as Valgrind says. That not being evident to you, you could nevertheless have discovered it for yourself by studying the program's behavior in a debugger.



Specifically, when the list is empty, its list->tail pointer is NULL, yet in that circumstance the program attempts to assign to list->tail->next. This is invalid. Moreover, you fail to ever assign a non-null value to list->head, which will cause you trouble later, when you try to traverse the list.



The simplest thing for you to do would be to write a special case for the needed behavior when the list is empty:



if (list->tail) 
list->tail->next = newNode;
else
// Special case: empty list
list->head = list->tail = newNode;



There are alternatives that avoid the need for such special cases by making the data structure a little more complex, but that would be the subject of a different answer.






share|improve this answer



























    up vote
    0
    down vote













    Access not within mapped region at address 0x8


    Means (as expected) that you access the address 0x8 which is not in a mapped segment. This usually means you access a structure pointer which is NULL.



    In you recompile with debug info (flas -ggdb for example)



    you get:



    ==7797== Process terminating with default action of signal 11 (SIGSEGV): dumping core
    ==7797== Access not within mapped region at address 0x8
    ==7797== at 0x4005CF: append_linked_list (delme.c:66)
    ==7797== by 0x40052C: main (delme.c:33)


    which directly gives you the lines that causes the error, which is (as pointed in comments):



    list->tail->next = newNode;


    the 0x8 value comes from the fact that next is 8 bytes inside the structure Node, thus &((Node *)NULL)->next is 0x8






    share|improve this answer




















      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',
      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%2f53241180%2ferror-access-not-within-mapped-region-at-address-valgrind%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








      up vote
      0
      down vote














      "Access not within mapped region at address 0x8". It then says "at 0x400606: append_linked_list (testing2.c:64) by 0x400563: main (testing2.c:32)". Line 64 is list->tail->next = newNode; [in function append_linked_list]




      Your append_linked_list() function is broken for the case in which the list is empty, and in particular, the line identified by Valgrind performs an invalid memory access, just as Valgrind says. That not being evident to you, you could nevertheless have discovered it for yourself by studying the program's behavior in a debugger.



      Specifically, when the list is empty, its list->tail pointer is NULL, yet in that circumstance the program attempts to assign to list->tail->next. This is invalid. Moreover, you fail to ever assign a non-null value to list->head, which will cause you trouble later, when you try to traverse the list.



      The simplest thing for you to do would be to write a special case for the needed behavior when the list is empty:



      if (list->tail) 
      list->tail->next = newNode;
      else
      // Special case: empty list
      list->head = list->tail = newNode;



      There are alternatives that avoid the need for such special cases by making the data structure a little more complex, but that would be the subject of a different answer.






      share|improve this answer
























        up vote
        0
        down vote














        "Access not within mapped region at address 0x8". It then says "at 0x400606: append_linked_list (testing2.c:64) by 0x400563: main (testing2.c:32)". Line 64 is list->tail->next = newNode; [in function append_linked_list]




        Your append_linked_list() function is broken for the case in which the list is empty, and in particular, the line identified by Valgrind performs an invalid memory access, just as Valgrind says. That not being evident to you, you could nevertheless have discovered it for yourself by studying the program's behavior in a debugger.



        Specifically, when the list is empty, its list->tail pointer is NULL, yet in that circumstance the program attempts to assign to list->tail->next. This is invalid. Moreover, you fail to ever assign a non-null value to list->head, which will cause you trouble later, when you try to traverse the list.



        The simplest thing for you to do would be to write a special case for the needed behavior when the list is empty:



        if (list->tail) 
        list->tail->next = newNode;
        else
        // Special case: empty list
        list->head = list->tail = newNode;



        There are alternatives that avoid the need for such special cases by making the data structure a little more complex, but that would be the subject of a different answer.






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote










          "Access not within mapped region at address 0x8". It then says "at 0x400606: append_linked_list (testing2.c:64) by 0x400563: main (testing2.c:32)". Line 64 is list->tail->next = newNode; [in function append_linked_list]




          Your append_linked_list() function is broken for the case in which the list is empty, and in particular, the line identified by Valgrind performs an invalid memory access, just as Valgrind says. That not being evident to you, you could nevertheless have discovered it for yourself by studying the program's behavior in a debugger.



          Specifically, when the list is empty, its list->tail pointer is NULL, yet in that circumstance the program attempts to assign to list->tail->next. This is invalid. Moreover, you fail to ever assign a non-null value to list->head, which will cause you trouble later, when you try to traverse the list.



          The simplest thing for you to do would be to write a special case for the needed behavior when the list is empty:



          if (list->tail) 
          list->tail->next = newNode;
          else
          // Special case: empty list
          list->head = list->tail = newNode;



          There are alternatives that avoid the need for such special cases by making the data structure a little more complex, but that would be the subject of a different answer.






          share|improve this answer













          "Access not within mapped region at address 0x8". It then says "at 0x400606: append_linked_list (testing2.c:64) by 0x400563: main (testing2.c:32)". Line 64 is list->tail->next = newNode; [in function append_linked_list]




          Your append_linked_list() function is broken for the case in which the list is empty, and in particular, the line identified by Valgrind performs an invalid memory access, just as Valgrind says. That not being evident to you, you could nevertheless have discovered it for yourself by studying the program's behavior in a debugger.



          Specifically, when the list is empty, its list->tail pointer is NULL, yet in that circumstance the program attempts to assign to list->tail->next. This is invalid. Moreover, you fail to ever assign a non-null value to list->head, which will cause you trouble later, when you try to traverse the list.



          The simplest thing for you to do would be to write a special case for the needed behavior when the list is empty:



          if (list->tail) 
          list->tail->next = newNode;
          else
          // Special case: empty list
          list->head = list->tail = newNode;



          There are alternatives that avoid the need for such special cases by making the data structure a little more complex, but that would be the subject of a different answer.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 17:07









          John Bollinger

          76.3k63771




          76.3k63771






















              up vote
              0
              down vote













              Access not within mapped region at address 0x8


              Means (as expected) that you access the address 0x8 which is not in a mapped segment. This usually means you access a structure pointer which is NULL.



              In you recompile with debug info (flas -ggdb for example)



              you get:



              ==7797== Process terminating with default action of signal 11 (SIGSEGV): dumping core
              ==7797== Access not within mapped region at address 0x8
              ==7797== at 0x4005CF: append_linked_list (delme.c:66)
              ==7797== by 0x40052C: main (delme.c:33)


              which directly gives you the lines that causes the error, which is (as pointed in comments):



              list->tail->next = newNode;


              the 0x8 value comes from the fact that next is 8 bytes inside the structure Node, thus &((Node *)NULL)->next is 0x8






              share|improve this answer
























                up vote
                0
                down vote













                Access not within mapped region at address 0x8


                Means (as expected) that you access the address 0x8 which is not in a mapped segment. This usually means you access a structure pointer which is NULL.



                In you recompile with debug info (flas -ggdb for example)



                you get:



                ==7797== Process terminating with default action of signal 11 (SIGSEGV): dumping core
                ==7797== Access not within mapped region at address 0x8
                ==7797== at 0x4005CF: append_linked_list (delme.c:66)
                ==7797== by 0x40052C: main (delme.c:33)


                which directly gives you the lines that causes the error, which is (as pointed in comments):



                list->tail->next = newNode;


                the 0x8 value comes from the fact that next is 8 bytes inside the structure Node, thus &((Node *)NULL)->next is 0x8






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Access not within mapped region at address 0x8


                  Means (as expected) that you access the address 0x8 which is not in a mapped segment. This usually means you access a structure pointer which is NULL.



                  In you recompile with debug info (flas -ggdb for example)



                  you get:



                  ==7797== Process terminating with default action of signal 11 (SIGSEGV): dumping core
                  ==7797== Access not within mapped region at address 0x8
                  ==7797== at 0x4005CF: append_linked_list (delme.c:66)
                  ==7797== by 0x40052C: main (delme.c:33)


                  which directly gives you the lines that causes the error, which is (as pointed in comments):



                  list->tail->next = newNode;


                  the 0x8 value comes from the fact that next is 8 bytes inside the structure Node, thus &((Node *)NULL)->next is 0x8






                  share|improve this answer












                  Access not within mapped region at address 0x8


                  Means (as expected) that you access the address 0x8 which is not in a mapped segment. This usually means you access a structure pointer which is NULL.



                  In you recompile with debug info (flas -ggdb for example)



                  you get:



                  ==7797== Process terminating with default action of signal 11 (SIGSEGV): dumping core
                  ==7797== Access not within mapped region at address 0x8
                  ==7797== at 0x4005CF: append_linked_list (delme.c:66)
                  ==7797== by 0x40052C: main (delme.c:33)


                  which directly gives you the lines that causes the error, which is (as pointed in comments):



                  list->tail->next = newNode;


                  the 0x8 value comes from the fact that next is 8 bytes inside the structure Node, thus &((Node *)NULL)->next is 0x8







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 17:09









                  OznOg

                  2,22711324




                  2,22711324



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241180%2ferror-access-not-within-mapped-region-at-address-valgrind%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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto