Segmentation fault (core dumped)????

This is a discussion on Segmentation fault (core dumped)???? within the C++ Programming forums, part of the General Programming Boards category; hello i have a problem Segmentation fault (core dumped)???? when i define t or num more than 400 i get ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    1

    Segmentation fault (core dumped)????

    hello

    i have a problem

    Segmentation fault (core dumped)????

    when i define t or num more than 400 i get Segmentation fault (core dumped)????

    what can i do

    the program is
    Code:
    #include<iostream>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    
    using namespace std;
    #define limit 2 //varibale
    #define num 200// number of particale 
    #define t 200 // number of steps
    FILE *fout;
    
    
    class particale
    {
    public:
    		double X[limit][t]; //possion LIMIT FOR VARIBALES T FOR NUMBER OF steps
    		double V[limit][t]; //velocity
    		double PPB[limit][t]; // particale personal  best
    		double y_p_b[t];      //solution for ppb
    
    };
    
    class gbest
    {
    public:
    		int particale_num;
    		int step;
    		double X[limit];
    		double y_s_gb;
    };
    
    void rand1(double &y)  //RANDOM number between 0-1 for the exp 
    {
    	y=rand()/float (RAND_MAX);	
    }
    void rand(double &x)  //jump between 5-5   
    {	
    	x=(rand()/float(RAND_MAX))*10;		
    }
    
    void y_soloution(double x1,double x2,double &y)
    //solution for the equation.
    {
    	double a,b,c,d,e,f,g,o,p,q,r,s,t1;
    	double dd,ee,ff,gg,oo,pp,qq,ss,rr,tt;
    
    	a =pow(( ( x1 - 5 ) / 0.94 ),2);
    	b =pow( ( (x2  - 5 ) / 0.94 ),2);
    	c = ( a + b );
    
    	if  (c <= 50)
    		y= 50;
    	if (c>50)
    		y=c;
    
    	d =pow( ( ( x1 - 3 ) / 0.05 ),2);
    	e =pow( ( ( x2 - 3 ) / 0.05 ),2);
    	f =pow( ( ( x1 - 5 ) / 0.05 ),2);
    	g =pow( ( ( x2 - 5 ) / 0.05),2);
    	o =pow( ( ( x1 - 1 ) / 0.05 ),2);
    	p =pow( ( ( x2 - 1) / 0.05 ),2);
    	q = pow(( ( x1 - 7 ) / 0.05 ),2);
    	r = pow(( ( x2 - 7 ) / 0.05 ),2);
    	s = pow(( ( x1 - 9 ) / 0.05 ),2);
    	t1 =pow( ( ( x2 - 9 ) / 0.05 ),2);
    	
    	dd =pow( ( ( x1 - 2 ) / 0.05 ),2);
    	ee =pow( ( ( x2 - 2 ) / 0.05 ),2);
    	ff =pow( ( ( x1 - 4 ) / 0.05 ),2);
    	gg =pow( ( ( x2 - 4 ) / 0.05),2);
    	oo =pow( ( ( x1 - 6 ) / 0.05 ),2);
    	pp =pow( ( ( x2 - 6) / 0.05 ),2);
    	qq = pow(( ( x1 - 8 ) / 0.05 ),2);
    	rr = pow(( ( x2 - 8 ) / 0.05 ),2);
    	ss = pow(( ( x1 - 10 ) / 0.05 ),2);
    	tt =pow( ( ( x2 - 10) / 0.05 ),2);
    
    	if( (  d + e+10 ) < 50)
    		y = d + e+10 ;
    	if( ( f + g+2 ) < 50)
    		y = f + g+2;
    	if( ( o + p+3 ) < 50 )
    		y =  o + p+3;
    	if( ( q + r+4 ) < 50 )
    		y = q + r+4 ;
    	if( (  s + t1+5) < 50 )
    		y =  s + t1+5;
    
    	if( (  dd + ee+6 ) < 50)
    		y = dd + ee+6 ;
    	if( ( ff + gg+7 ) < 50)
    		y = ff + gg+7;
    	if( ( oo + pp+8 ) < 50 )
    		y =  oo + pp+8;
    	if( ( qq + rr +9) < 50 )
    		y = qq + rr+9 ;
    	if( (  ss + tt+10 ) < 50 )
    		y =  ss + tt+10;
    
    }
    
    int main()
    {
    	int i,j,p;
    	double y1,y_p_b,r1,r2;
    	particale a[num];
    	gbest g_b;
    	
    	srand(time(0));
    	
    	for (i=0;i<num;i++) // calibration first
    	{
    		rand(a[i].X[0][1]);  //GET SEP 1 POSSION X1
    		rand(a[i].X[1][1]);	 //GET STEP 1 POSSION X2
    		rand(a[i].V[0][1]);  // GET STEP 1 velocity X1
    		rand(a[i].V[1][1]);  //GET STEP 1 velocity X2
    		a[i].PPB[0][0]=a[i].X[0][1]; //PPB STEP 0 X1
    		a[i].PPB[1][0]=a[i].X[1][1]; //PPB STEP 0 X2
    		
    		g_b.particale_num=i;    // global best 
    		g_b.X[1]=a[i].PPB[1][0]; //x1 global best
    		g_b.X[0]=a[i].PPB[0][0]; //x2 global best
    		y_soloution(g_b.X[0],g_b.X[1],g_b.y_s_gb); //ysoloution global best
    		g_b.step=0;                                //number of step global best
    
    	for (i=1;i<t;i++) //main loop start a step 1  (step 0 before)
    	{
    		for (j=0;j<num;j++)  //loop to check for every particle ppb gb ,num of dogs birds  
    		{
    			y_soloution(a[j].PPB[0][i-1],a[j].PPB[1][i-1],a[j].y_p_b[i-1]); //SOLOUTION PPB STEP I
    			y_soloution(a[j].X[0][i],a[j].X[1][i],y1);   //SOLOUTION X1 X2 STEP I+1
    			if 	(y1>a[j].y_p_b[i-1])  
    			//if ppb for the previous step is lower E than the ppb for the next step will be him
    			{
    				a[j].PPB[0][i]=a[j].PPB[0][i-1];
    				a[j].PPB[1][i]=a[j].PPB[1][i-1];
    				a[j].y_p_b[i]=a[j].y_p_b[i-1];
    			}
    			else
    			/*if the x1 x2 of this step is lower in E than the ppb of the previous step than
    			  the x1 x2 ==>y will be the step ppb.*/
    			{
    				a[j].PPB[0][i]=a[j].X[0][i];
    				a[j].PPB[1][i]=a[j].X[1][i];
    				a[j].y_p_b[i]=y1;
    			}
    			if (a[j].y_p_b[i]< g_b.y_s_gb)  //changing global best if needed
    			{
    				g_b.particale_num=j;    // global best 
    				g_b.X[0]=a[j].PPB[0][i];
    				g_b.X[1]=a[j].PPB[1][i];
    				y_soloution(g_b.X[0],g_b.X[1],g_b.y_s_gb);
    				g_b.step=i;
    			}	
    
    		}
    		for (j=0;j<num;j++) 
    		/*number of dogs loop for changing the next step velocity 
    		and after that the possion*/
    		{
    			for (p=0;p<limit;p++) //number of dimendional of every dog
    			{
    				rand1(r1);
    				rand1(r2);
    				a[j].V[p][i+1]=a[j].V[p][i]*0.65+2*r1*(a[j].PPB[p][i]-a[j].X[p][i])+2*r2*(g_b.X[p]-a[j].X[p][i]);
    				a[j].X[p][i+1]=a[j].X[p][i]+a[j].V[p][i+1];
    			}
    		}
    	cout<<"interact "<<t<<" particale number " <<g_b.particale_num<<" X1/2 POSSION " <<g_b.X[0]<<" "<< g_b.X[1]<<" Y= "<<g_b.y_s_gb<<" step "<<g_b.step<<endl;
    	}
    	cout<<"final"<<" particale number " <<g_b.particale_num<<" X1/2 POSSION " <<g_b.X[0]<<" "<< g_b.X[1]<<" Y= "<<g_b.y_s_gb<<" step "<<g_b.step<<endl;
    	for (i=0;i<num;i++) //write to screen
    	{
    		cout<<" particale number "<<i<<" personal best possion "<< a[i].PPB[0][t-1]<<" "<<a[i].PPB[1][t-1]<<endl;
    	}
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    You have 1 huge variable taking over 2MB of stack.
    Code:
    #include<iostream>
    
    using namespace std;
    #define limit 2 //varibale
    #define num 200// number of particale
    #define t 200 // number of steps
    
    class particale
    {
    public:
            double X[limit][t]; //possion LIMIT FOR VARIBALES T FOR NUMBER OF steps
            double V[limit][t]; //velocity
            double PPB[limit][t]; // particale personal  best
            double y_p_b[t];      //solution for ppb
    
    };
    
    int main()
    {
        cout << sizeof(particale)*num << endl;
    }
    
    $ g++ foo.cpp
    $ ./a.out 
    2240000
    $
    A quick hack would be to declare
    static particale a[num];


    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    Also, replace these with the C++ namespace versions
    #include <cmath>
    #include <cstdlib>
    #include <ctime>
    #include <cstdio>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Replace your #define's with const int variables. Also, I really suggest you change your C arrays to std::array or std::vector for safety purposes (security and less bugs).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 'Segmentation fault (core dumped)' error?
    By Von Fuzzball in forum C Programming
    Replies: 12
    Last Post: 05-03-2011, 03:29 PM
  2. Replies: 7
    Last Post: 03-17-2011, 11:55 AM
  3. Replies: 3
    Last Post: 06-05-2010, 03:58 AM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 04:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21