473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

segmenatation fault while allocating memory

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

}

Jul 19 '05 #1
2 2868
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.

Jul 19 '05 #2
Sameer <ia*******@hotmail.com> wrote in message
news:bo*************@ID-160782.news.uni-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

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
9
by: Bin Lu | last post by:
I keep getting this malloc problem when my program tries to allocate memory for some pointer. The statement is like: rsv_cache = (rsvcache *) malloc (sizeof(rsvcache)); It goes through the...
4
by: Sameer | last post by:
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 "...
1
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can...
6
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
27
by: lovecreatesbea... | last post by:
This code snippet is an exercise on allocating two dimension array dynamically. Though this one is trivial, is it a correct one? Furthermore, when I tried to make these changes to the original...
20
by: Win Sock | last post by:
Hi All, somebody told me this morning that the following is leagal. char *a = "Hello wrold"; The memory is automatically allocated on the fly. Is this correct?
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.