segmenatation fault while allocating memory

Discussion in 'C Programming' started by Sameer, Nov 3, 2003.

  1. Sameer

    Sameer Guest

    Hello Group,

    This is one problem in programming that is troubling me.

    there is a segmentation fault just before creating memory to
    a structure ..i.e, just after the "allocating memory " statement.

    This happens for some inputs and not all.

    What can be the reason for such fault ?

    Kindly reply soon,
    Sameer.


    void add(struct node **q)
    {
    struct node *temp;
    temp=*q;

    if(*q==NULL)
    {
    *q=new node;
    temp=*q;
    }

    else
    {
    cout << "allocating memory";
    temp = new node;
    cout << "memory allocated";
    temp->next=*q;
    }

    }
     
    Sameer, Nov 3, 2003
    #1
    1. Advertising

  2. In article <bo4q7g$179s5a$-berlin.de>,
    "Sameer" <> wrote:

    > cout << "allocating memory";



    cout << "You are posting in the wrong newsgroup";
    cout << "comp.lang.c++ is next door. ";
     
    Christian Bau, Nov 3, 2003
    #2
    1. Advertising

  3. In article <bo4q7g$179s5a$-berlin.de>, Sameer wrote:
    > Hello Group,
    >
    > This is one problem in programming that is troubling me.
    >
    > there is a segmentation fault just before creating memory to
    > a structure ..i.e, just after the "allocating memory " statement.
    >
    > This happens for some inputs and not all.
    >
    > What can be the reason for such fault ?
    >
    > Kindly reply soon,
    > Sameer.
    >
    >


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

    > void add(struct node **q)
    > {
    > struct node *temp;


    if (q == NULL) {
    /* take care of error */
    }

    > temp=*q;


    Remove the line above (unneeded).

    > if(*q==NULL)
    > {
    > *q=new node;


    replace with:

    *q = malloc(sizeof **q);
    if (*q == NULL) {
    /* take care of error */
    }

    > temp=*q;
    > }
    >
    > else
    > {
    > cout << "allocating memory";


    replace with:

    printf("Allocating memory\n");

    > temp = new node;


    replace with:

    temp = malloc(sizeof *temp);
    if (temp == NULL) {
    /* take care of error */
    }

    > cout << "memory allocated";


    replace with:

    printf("Memory allocated\n");

    > temp->next=*q;
    > }
    >
    > }



    There was an awful lot of C++ -like code in there... and the C++
    iostream classes doesn't flush output unless a cout object is
    passed an endl or flush object. The error might therefore occur
    somewhere else entierly. Switch to proper C syntax and make
    sure you flush your output in all your debug output statements.


    --
    Andreas Kähäri
     
    Andreas Kahari, Nov 3, 2003
    #3
  4. Sameer wrote:

    > Hello Group,
    >
    > This is one problem in programming that is troubling me.
    >
    > there is a segmentation fault just before creating memory to
    > a structure ..i.e, just after the "allocating memory " statement.
    >
    > This happens for some inputs and not all.
    >
    > What can be the reason for such fault ?
    >
    > Kindly reply soon,
    > Sameer.
    >
    >
    > void add(struct node **q)
    > {
    > struct node *temp;
    > temp=*q;
    >
    > if(*q==NULL)
    > {
    > *q=new node;
    > temp=*q;
    > }
    >
    > else
    > {
    > cout << "allocating memory";
    > temp = new node;
    > cout << "memory allocated";
    > temp->next=*q;
    > }
    >
    > }


    For your code to be topical in comp.lang.c, it would look something like
    the following (no guarantees about the correctness of the code):

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

    struct node
    {
    struct node *next;
    };

    void add(struct node **q)
    {
    struct node *temp;
    temp = *q;

    if (!*q) {
    printf("allocating root\n");
    if ((*q = malloc(sizeof **q))) {
    printf("root allocated\n");
    (*q)->next = 0;
    }
    else {
    printf("allocation failed\n");
    abort();
    }
    }

    else {
    printf("allocating new node\n");
    if ((temp = malloc(sizeof **q))) {
    printf("node allocated\n");
    temp->next = 0;
    (*q)->next = temp;
    }
    else {
    printf("allocation failed\n");
    abort();
    }
    }

    }

    int main(void)
    {
    struct node *k = 0;
    add(&k);
    return 0;
    }





    --
    Martin Ambuhl
     
    Martin Ambuhl, Nov 3, 2003
    #4
  5. Sameer

    Al Bowers Guest

    Martin Ambuhl wrote:

    > For your code to be topical in comp.lang.c, it would look something like
    > the following (no guarantees about the correctness of the code):
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > struct node
    > {
    > struct node *next;
    > };
    >
    > void add(struct node **q)
    > {
    > struct node *temp;
    > temp = *q;
    >
    > if (!*q) {
    > printf("allocating root\n");
    > if ((*q = malloc(sizeof **q))) {
    > printf("root allocated\n");
    > (*q)->next = 0;
    > }
    > else {
    > printf("allocation failed\n");
    > abort();
    > }
    > }
    >
    > else {
    > printf("allocating new node\n");
    > if ((temp = malloc(sizeof **q))) {
    > printf("node allocated\n");
    > temp->next = 0;
    > (*q)->next = temp;


    Oops. You need to be very careful in using this
    function. It you try to link more than two nodes
    you will case a break in the link and resulting
    data loss and memory leak.


    > }
    > else {
    > printf("allocation failed\n");
    > abort();
    > }
    > }


    Here, hopefully, is a better example.

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

    struct node
    {
    char name[32];
    struct node *next;
    };

    int add(struct node **q, const char *name);

    int main(void)
    {
    struct node *k = 0;
    add(&k,"George Washington");
    add(&k, "Abe Lincoln");
    add(&k, "George Bush");
    while(k)
    {
    struct node *temp = k->next;
    printf("The name is %s\n",k->name);
    free(k);
    k = temp;
    }
    return 0;
    }

    int add(struct node **q, const char *name)
    {
    struct node *temp;

    if((temp = malloc(sizeof *temp)) == NULL) return 0;
    strcpy(temp->name,name);
    temp->next = *q;
    *q = temp;
    return 1;
    }

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Nov 3, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hall
    Replies:
    4
    Views:
    458
  2. soni29
    Replies:
    6
    Views:
    445
    Kevin Goodsell
    Sep 5, 2003
  3. Axel
    Replies:
    1
    Views:
    769
    stephan beal
    Oct 27, 2003
  4. Sameer
    Replies:
    2
    Views:
    293
    David White
    Nov 3, 2003
  5. Rakesh Kumar
    Replies:
    5
    Views:
    695
    James Kanze
    Dec 21, 2007
Loading...

Share This Page