beginners problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lars

    beginners problem

    Hi all
    If this is the wrong list for beginners trouble I apologize.
    please refer to me the correct list in such case.

    I am trying to create a simple linked list but I keep getting segmentation
    errors and I dont know why. I realize that I do not free the allocated
    memory but the segfault happpens (it seems) with the insert function.
    Can any one explain ?

    Regards Lars

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

    typedef struct node {
      struct node * next;
      int data;
    } Node;


    Node * alloc_node(int n);

    void insert(Node * head, int n);

    int main(){
      Node * nod = NULL;
      insert(nod,4);
      printf("%d\n",n od->data);
      printf("%10p\n" , nod);
      printf("%10p\n" , nod->next);
      return 0;
    }

    Node * alloc_node(int n){
      Node * tmp;
      tmp = (Node *) malloc(sizeof(N ode));
      tmp->data = n;
      tmp->next = NULL;
      return tmp;
    }

    void insert(Node * head, int n) {
      Node * tmp;
      if (head==NULL) {
        head = alloc_node(n);
      }
      else {  
        tmp = alloc_node(n);
        head->next = tmp;
      }
    }
  • Ian Collins

    #2
    Re: beginners problem

    Lars wrote:
    Hi all
    If this is the wrong list for beginners trouble I apologize.
    please refer to me the correct list in such case.
    >
    I am trying to create a simple linked list but I keep getting segmentation
    errors and I dont know why. I realize that I do not free the allocated
    memory but the segfault happpens (it seems) with the insert function.
    Can any one explain ?
    >
    Regards Lars
    >
    #include <stdio.h>
    #include <stdlib.h>
    >
    typedef struct node {
    struct node * next;
    int data;
    } Node;
    >
    >
    Node * alloc_node(int n);
    >
    void insert(Node * head, int n);
    >
    int main(){
    Node * nod = NULL;
    insert(nod,4);
    printf("%d\n",n od->data);
    There's your problem, nod is NULL.
    Node * alloc_node(int n){
    Node * tmp;
    tmp = (Node *) malloc(sizeof(N ode));
    You don't have to (and shouldn't) cast the return value of malloc.

    tmp = malloc(sizeof *tmp);

    --
    Ian Collins.

    Comment

    • Lars

      #3
      Re: beginners problem

      Ian Collins wrote:
      Lars wrote:
      >Hi all
      >If this is the wrong list for beginners trouble I apologize.
      >please refer to me the correct list in such case.
      >>
      >I am trying to create a simple linked list but I keep getting
      >segmentation errors and I dont know why. I realize that I do not free the
      >allocated memory but the segfault happpens (it seems) with the insert
      >function. Can any one explain ?
      >>
      >Regards Lars
      >>
      >#include <stdio.h>
      >#include <stdlib.h>
      >>
      >typedef struct node {
      > struct node * next;
      > int data;
      >} Node;
      >>
      >>
      >Node * alloc_node(int n);
      >>
      >void insert(Node * head, int n);
      >>
      >int main(){
      > Node * nod = NULL;
      > insert(nod,4);
      > printf("%d\n",n od->data);
      >
      There's your problem, nod is NULL.
      >
      >Node * alloc_node(int n){
      > Node * tmp;
      > tmp = (Node *) malloc(sizeof(N ode));
      >
      You don't have to (and shouldn't) cast the return value of malloc.
      >
      tmp = malloc(sizeof *tmp);
      >
      Thanks for your reply. But is this not handled by the the insert functio
      through this ?
      if (head==NULL) {
          head = alloc_node(n);
        }

      Regards
      Lars

      Comment

      • Ian Collins

        #4
        Re: beginners problem

        Lars wrote:
        Ian Collins wrote:
        >>>
        >>Node * alloc_node(int n);
        >>>
        >>void insert(Node * head, int n);
        >>>
        >>int main(){
        >> Node * nod = NULL;
        >> insert(nod,4);
        >> printf("%d\n",n od->data);
        >There's your problem, nod is NULL.
        >>
        >
        Thanks for your reply. But is this not handled by the the insert functio
        through this ?
        if (head==NULL) {
        head = alloc_node(n);
        }
        >
        No, ant changes to head are local to the function, heat is a pointer, if
        you wanted the change to be visible to the caller, it would have to be a
        pointer to a pointer.

        void insert(Node** head, int n);

        --
        Ian Collins.

        Comment

        • Lars

          #5
          Re: beginners problem

          Ian Collins wrote:
          Lars wrote:
          >Ian Collins wrote:
          >>>>
          >>>Node * alloc_node(int n);
          >>>>
          >>>void insert(Node * head, int n);
          >>>>
          >>>int main(){
          >>> Node * nod = NULL;
          >>> insert(nod,4);
          >>> printf("%d\n",n od->data);
          >>There's your problem, nod is NULL.
          >>>
          >>
          >Thanks for your reply. But is this not handled by the the insert functio
          >through this ?
          > if (head==NULL) {
          > head = alloc_node(n);
          > }
          >>
          No, ant changes to head are local to the function, heat is a pointer, if
          you wanted the change to be visible to the caller, it would have to be a
          pointer to a pointer.
          >
          void insert(Node** head, int n);
          >
          Thats it!!!
          I completely overlooked that.
          Thank you very much.
          Best regards
          Lars

          Comment

          • Ian Collins

            #6
            Re: beginners problem

            Richard Bos wrote:
            Lars <a@nondisclosur e.dkwrote:
            >
            >If this is the wrong list for beginners trouble I apologize.
            >
            This is not a list, it's a newsgroup. And if you post questions about
            ISO C, not about, oh, I dunno, M$VC#++4.6 or Ganuck 4.77.35.88.55-alpha,
            you should be fine here whether you're a beginner or not. Your question
            is perfectly on-topic. However...
            >
            >#include <stdio.h>
            >#include <stdlib.h>
            >>
            >typedef struct node {
            >Â struct node * next;
            >Â int data;
            >
            ....you might want to check what is inserting these weird characters into
            your code, and...
            >
            Your client? They don't appear to be in the message source.

            --
            Ian Collins.

            Comment

            • Randy Howard

              #7
              Re: beginners problem

              On Mon, 11 Feb 2008 02:47:11 -0600, Ian Collins wrote
              (in article <61agcgF1ua3ccU 9@mid.individua l.net>):
              Richard Bos wrote:
              >Lars <a@nondisclosur e.dkwrote:
              >>
              >>If this is the wrong list for beginners trouble I apologize.
              >>
              >This is not a list, it's a newsgroup. And if you post questions about
              >ISO C, not about, oh, I dunno, M$VC#++4.6 or Ganuck 4.77.35.88.55-alpha,
              >you should be fine here whether you're a beginner or not. Your question
              >is perfectly on-topic. However...
              >>
              >>#include <stdio.h>
              >>#include <stdlib.h>
              >>>
              >>typedef struct node {
              >>Â struct node * next;
              >>Â int data;
              >>
              >....you might want to check what is inserting these weird characters into
              >your code, and...
              >>
              Your client? They don't appear to be in the message source.
              I saw them as well. I'm pretty sure it's some UTF/Unicode weirdness
              with tab expansion or perhaps newline encoding somewhere along the way,
              and we're both using different clients, on different operating systems.
              Being that the OP is apparently in Denmark, it's a safe bet that it
              something along those lines.


              --
              Randy Howard (2reply remove FOOBAR)
              "The power of accurate observation is called cynicism by those
              who have not got it." - George Bernard Shaw





              Comment

              Working...