segmenatation fault while allocating memory

S

Sameer

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

}
 
G

Gianni Mariani

Sameer said:
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.
 
D

David White

Sameer said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top