GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jun-2008, 07:57
fyxx fyxx is offline
New Member
 
Join Date: Jun 2008
Posts: 3
fyxx is on a distinguished road
Question

Baffling Segmentation Fault...


So I am just working on a MD program but either way I am having a not so fun time making it work (properly).

So it starts out normally and I call in several modules.

CPP / C++ / C Code:
int main() {

    // Initializsierung entsprechend setup.h - Initializing acorrding to setup.h
    int npart = npart_def;
    double tnet = tnet_def;			
    double dt = dt_def;

So the values npart_def etc. come from the setup.h and they are defined properly and work, no issues here..

Then these variables are passed to the another module, from the main routine it looks like..

CPP / C++ / C Code:
	Getinitials(npart, tnet, dt);

Then in the Getinitials function it looks like..

CPP / C++ / C Code:
// Subroutines
template<typename T> void query(char text[],T& input, T min,T max) {

	do {
		cout<<"# "<<text<<" <"<<input<<"> : ";
		if (cin.peek()!='\n') {cin>>input;}
		while(getchar()!='\n');		//clear buffer

	} while (!((input>=min) && (input<=max)));
}


// Mainroutine
void Getinitials(int& npart, double& tnet, double& dt)
{

	makechanges: 						// Label fur wiederholtes Einlesen (Korrektur)

	// Einlesen der Systemparameter
	query("Number of Particles ", npart, 1, 1000000);
	query("Run Time ", tnet, 0.1, MAX_tnet);
	query("Time Steps ", dt, 0.0, tnet/1000.0);

	// Kontrollausgabe der eingegebenen Werte
	cout<<"#"<<endl;
	cout<<"# N = "<<npart<<endl;
	cout<<"# tnet = "<<tnet<<endl;
	cout<<"# dt= "<<dt<<endl;
	cout<<"#"<<endl;

	// Start oder Korrenktur?
	for(char ch;;) {
		cout<<"# [A]ccept [D]ecline <A> : "; cin.get(ch);
		if (ch=='\n') break;
		else while(getchar()!='\n');

		if (ch=='A' || ch=='a') break;
		else if (ch=='D' || ch=='d') {goto makechanges;} 	// Anderungen
	}

}


and this works fine also.. then in the main routine (**Placeholder**)

CPP / C++ / C Code:
	// Set sizes of dynamic arrays based on "Get initials"
	x = new double[npart];
	y = new double[npart];
	z = new double[npart];
	v_x = new double[npart];
	v_y = new double[npart];
	v_z = new double[npart];
	/*dxdt = new double[npart];
	dydt = new double[npart];
	dzdt = new double[npart];*/
	dv_xdt = new double[npart];
	dv_ydt = new double[npart];
	dv_zdt = new double[npart];

	cout<<"Energies"<<endl;
	A = 0.0;
	U = 0.0;
	K = new double[npart];
	//E = new double[npart];
	N = 0.0;

	// Sets the n-particles at their respective origins
	// However for simplification I am setting both to a/2
	cout<<"Coords"<<endl;
	x[1]=-0.5;
	x[2]= 0.5;
	y[1]= 0;
	y[2]= 0;
	z[1]= 0;
	z[2]= 0;

	Initout(npart);

And Initout function looks like

CPP / C++ / C Code:
void Initout(int npart)
{
	// Header format
	cout<<"Opening file"<<endl;
	ofstream handler("log.dat", ios::out);	// Offnen/Opens after making the data file
	cout<<"Opened"<<endl;

	handler<<"# MD -- VERSION: "<<VERSION<<endl;
	cout<<"Printing N"<<endl;
	handler<<"# N = "<<npart<<endl;
		
	// Creates columns
	cout<<"Headers"<<endl;
	handler<<"#";	
	for (int k=1; k<=npart; k++) {
		handler<<"\t\t n = "<<k<<"\t\t\t\t";
	}
	handler<<endl<<"#";
	for (int k=1; k<=npart; k++) {
		handler<<"   x\t\t  y\t\t  z\t\tKinetic\t\t";
	}
	handler<<"External, Potential, Total Energy";
	handler<<endl;
	handler.close();
}


What happens though, is when the Getinitials is called, if I leave all the values and just hit enter or enter the same number, the program runs fine. However, if I change any of the numbers (I don't change npart, because it isn't really designed to be changed yet) then I get a segmentation fault when it tries to open the log.dat file.. So it seems weird that it only happens when I change the values otherwise it works, baffling..
Also, if I change the Initout function to be where I said **Placeholder** then I don't get a segmentation fault, but later on when I print values they all show up as nan.

Any help is appreciated... I am no professional programmer, and C++ was never my game.. I shoulda been a plumber
  #2  
Old 03-Jun-2008, 09:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Baffling Segmentation Fault...


Quote:
Originally Posted by fyxx
.... C++ was never...


Are you, perhaps, using npart = 2? If so, then the following leads to undefined behavior since, in C and C++, array index values start at zero, not one.
CPP / C++ / C Code:
	x = new double[npart];
.
.
.
	x[1]=-0.5;
	x[2]= 0.5;

Same for y and z.

Undefined behavior can manifest itself in different ways, and sometimes it depends on various conditions at run time. (Like sometimes the program crashes and sometimes not. Stuff like that.)



Regards,

Dave
  #3  
Old 03-Jun-2008, 09:29
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Baffling Segmentation Fault...


One-off problems like davekw7x mentions are very common, & can lead to nasty, but subtle results.

Likewise, I see no error checking on a whole lot of heapspace allocations. What kind of heapspace requirements do you have & does the platform used support these needs?
  #4  
Old 03-Jun-2008, 09:30
fyxx fyxx is offline
New Member
 
Join Date: Jun 2008
Posts: 3
fyxx is on a distinguished road
Cool

Re: Baffling Segmentation Fault...


Hey, thanks, that worked.. I think. Just got to see if I can get it working again after I mashed it up something fierce. How i had it set up is the loops for 1-2 not 0-1.. If I use:

x = new double[2]

does that define x[0] and x[1] as in 2 spots, or does it define x[0], x[1], x[2] as in 0 to 2? Thanks again for your time, I appreciate it
  #5  
Old 03-Jun-2008, 09:42
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Baffling Segmentation Fault...


Quote:
Originally Posted by fyxx
If I use:

x = new double[2]

does that define x[0] and x[1] as in 2 spots, or does it define x[0], x[1], x[2] as in 0 to 2?
The above statement allocates space from the heap for two contiguous double's -- so the array indexing is as expected -- 0, & 1.
  #6  
Old 04-Jun-2008, 02:43
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Baffling Segmentation Fault...


This is basic operation in C/C++ and often lead to common bug in program.
  #7  
Old 04-Jun-2008, 03:16
fyxx fyxx is offline
New Member
 
Join Date: Jun 2008
Posts: 3
fyxx is on a distinguished road
Smile

Re: Baffling Segmentation Fault...


Yea totally, I managed to fix most of my damage last night; thanks a bunch guys you saved me a week of banging my head on the table!
  #8  
Old 04-Jun-2008, 08:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Baffling Segmentation Fault...


Quote:
Originally Posted by fyxx
...banging my head on the table...

I used to do this all of the time, and my mom was afraid that I was going to damage the table, so she came up with a Better Plan. See attachment.

I taped this onto the outside wall of the little brick "house behind the house" and she was much happier. (Not quite so happy, of course, when she was inside the little house, but it still saved lots of wear and tear on the table.)

Regards,

Dave
Attached Images
File Type: pdf stress.pdf (29.9 KB, 6 views)
Last edited by davekw7x : 04-Jun-2008 at 09:56.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I have a segmentation fault in my project takachi C++ Forum 6 09-Mar-2008 23:30
Find the Segmentation Fault jlwelch C Programming Language 3 10-Oct-2006 11:16
My Bane - The Segmentation Fault DCOM C Programming Language 6 08-Feb-2005 23:58
segmentation fault in c++ rushman8282 C++ Forum 2 26-Jan-2005 04:38
child pid xxx exit signal Segmentation fault (11) bezak Apache Web Server Forum 1 24-Nov-2003 10:18

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:34.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.