segmenatation fault while allocating memory

Discussion in 'C++' 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. Sameer wrote:
    > Hello Group,


    Hi, how is Bangalore, or New Delhi ?

    >
    > This is one problem in programming that is troubling me.


    Join the club.

    >
    > 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.


    This code does not create a working program.

    try posting a program that compiles.

    >
    >
    > 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;
    > }
    >
    > }


    It's likely your problem is elsewhere.
    Gianni Mariani, Nov 3, 2003
    #2
    1. Advertising

  3. Sameer

    David White Guest

    Sameer <> wrote in message
    news:bo4m2r$17egee$-berlin.de...
    > 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)


    What are you passing in as 'q'?

    > {
    > struct node *temp;
    > temp=*q;


    You aren't using this value of temp.

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


    There's no crash evident here. You need to post more code.

    DW
    David White, Nov 3, 2003
    #3
    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:
    442
  2. soni29
    Replies:
    6
    Views:
    408
    Kevin Goodsell
    Sep 5, 2003
  3. Axel
    Replies:
    1
    Views:
    731
    stephan beal
    Oct 27, 2003
  4. Sameer

    segmenatation fault while allocating memory

    Sameer, Nov 3, 2003, in forum: C Programming
    Replies:
    4
    Views:
    428
    Al Bowers
    Nov 3, 2003
  5. Rakesh Kumar
    Replies:
    5
    Views:
    681
    James Kanze
    Dec 21, 2007
Loading...

Share This Page