in C++ segmenatation fault

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2006
Posts: 2
Reputation: bharat.bhooshan is an unknown quantity at this point 
Solved Threads: 0
bharat.bhooshan bharat.bhooshan is offline Offline
Newbie Poster

in C++ segmenatation fault

 
0
  #1
Aug 13th, 2006
I have a problem in C++(complier dev 4.9.9.2). My program have complie with no error but out put is wrong and not run show with massage “ some intermediated data is missing”. In debugging its show massage “segmentation Error” .

  1. #include<iostream>
#include<math.h>
#include<conio.h>
#include<fstream>
#include<dos.h>
#include<iomanip>
const int row=195;
const int col=235;
using namespace std;
float velocity (int,float,float,float,float);
int main()
{
 
ifstream in("data.txt");
ofstream out("vel.txt");
int **EL= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **el= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **SA= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **RL= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **LSD= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **ASP= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **SL= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **X= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
int **Y= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
float **FC= (float**)malloc(row*col*sizeof(float) + col*sizeof(float*));
double **vel_g=(double**)malloc(row*col*sizeof(double) + col*sizeof(double*));
 
int i,m,n ,j,x,y,t,Max,k,k1;
 
float cvel,vel1,vel2,vel3,vel4,vel5,vel6,vel8,vel7,vel;
 
 
for(i=0;i<row;i++)
 
for( j=0; j<col;j++)
{
in>>el[i][j];
EL[i][j]=el[i][j];
in>>RL[i][j];
in>>SA[i][j];
in>>SL[i][j];
in>>FC[i][j];
in>>LSD[i][j];
in>>ASP[i][j];
}
 
for( k=0; k<row ;k++)
for( k1=0; k1<col;k1++)
{
Max=-2;
 
for(int i=0; i<row;i++)
for(int j=0; j<col;j++)
 
if(el[i][j]>Max)
{
Max=el[i][j];
x=j;
y=i;
}
 
X[k][k1]=x;
Y[k][k1]=y;
el[y][x]=-21;
cout<<k<<"\t"<<k1<<"\t"<<X[k][k1]<<endl; 
 
}
for(int a=0;a<row;a++)
for(int b=0;b<col;b++)
vel_g[a][b]= 0 ;
for(int k=0; k<row;k++)
for(int k1=0;k1<col;k1++)
{ 
 
n=X[k][k1];
m=Y[k][k1];
if(1==LSD[m][n]) 
{
while(SA[m][n]>=25)
{
vel = velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]); 
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7))));
t=ASP[m][n]; 
switch(t)
{
case(1): {m= m-1;n=n; vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7))));
if(m<0||m>row||n<0||n>col) {cout<<"error1\n";} break;}
case(2): {m= m-1;n=n+1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7)))); 
if(m<0||m>row||n<0||n>col) {cout<<"error2\n"; }break;}
case(3): {m= m;n=n+1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7))));
if(m<0||m>row||n<0||n>col) {cout<<"error3\n"; }break;}
case(4): {m= m+1;n=n+1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7)))); 
if(m<0||m>row||n<0||n>col) {cout<<"error4\n"; }break;}
case(5): {m= m+1;n=n;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7)))); 
if(m<0||m>row||n<0||n>col) {cout<<"error5\n"; }break;}
case(6): {m= m+1;n=n-1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7))));
if(m<0||m>row||n<0||n>col) {cout<<"error6\n"; }break;}
case(7): {m= m;n=n-1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7)))); 
if(m<0||m>row||n<0||n>col) {cout<<"error7\n"; }break;}
case(8): {m= m+1;n=n-1;vel=velocity(RL[m][n],FC[m][n],SL[m][n],SA[m][n],vel_g[m][n]);
vel_g[m][n]= sqrt((vel_g[m][n]*vel_g[m][n])+(vel*vel)+(2*vel_g[m][n]*vel*cos(45*(22/7))));
if(m<0||m>row||n<0||n>col) {cout<<"error8\n"; }break;}
 
 
}
 
} } 
 
 
} 
 
 
for(int c=0;c<row ;c++)
for(int d=0;d<col;d++)
{ out<<c<<"\t"<<d<<"\t";
out<<vel_g[c][d]<<'\n';
}
 
return 0;}
 
 
 
 
 
float velocity( int RL,float fric,float slope, float slopeangle, float ve)
{
int heg;
float D,b;
heg=RL;
b=fric;
if(ve>1000)
{
cout<<"\t"<<"heg="<<heg<<"\tb="<<b<<"\tve"<<ve<<"\t";
getch();
} 
const double g =9.781;
slopeangle=((slopeangle*(22/7))/180);
float x = (2*g*(heg-(b*slope*cos(slopeangle))));
if(x<0) x=0;
D=slope*cos(slopeangle);
float y = sqrt(x+(ve*ve));
if(y>100000)
{
cout<<"\td="<<D<<"\tfc="<<b<<"\tg="<<g<<"\theg="<<heg<<"\nve="<<ve<<"\ty="<<y<<"\n";
 
}
return y;
}
Last edited by cscgal; Aug 13th, 2006 at 1:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 5
Reputation: psycho^ is an unknown quantity at this point 
Solved Threads: 0
psycho^ psycho^ is offline Offline
Newbie Poster

Re: in C++ segmenatation fault

 
0
  #2
Aug 14th, 2006
int **EL= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));

EL[0] - this is first pointer in array
EL[0][0] - this is first pointer in array POINTED BY EL[0]

since you didnt initialize it (EL[0]), you have random data in there, so you access random memory - segmentation fault

you have to use EL[i*row+j] (or EL[i*col+j])
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: in C++ segmenatation fault

 
0
  #3
Aug 14th, 2006
@Bharat
<math.h>//deprecated in C++, use <cmath>
<conio.h>//Non-Standard
<dos.h>//Non-Standard
Last edited by Grunt; Aug 14th, 2006 at 12:59 pm.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: in C++ segmenatation fault

 
1
  #4
Aug 14th, 2006
> int **EL= (int**)malloc(row*col*sizeof(int) + col*sizeof(int*));
1. You didn't include stdlib.h
2. Since this seems to be C++, you should be using new anyway.
3. Since your sizes seem to be constant, why are you dynamically allocating anyway?

I see that lot adds up to at least a couple of MB, so that probably explains why you gave up on
int EL[row][col];

There are several ways out, which don't involve a lot of mess.

1. Make them static, you get the scope without the stack space
static int EL[row][col];

2. Dynamically allocate in one go, since the minor size is constant.
int (*EL)[col] = new int[row][col];
Note the placement of the ( ), very important you get these right.

3. Since this is C++, use a vector
vector< vector<int> > EL;
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2
Reputation: bharat.bhooshan is an unknown quantity at this point 
Solved Threads: 0
bharat.bhooshan bharat.bhooshan is offline Offline
Newbie Poster

Re: in C++ segmenatation fault

 
0
  #5
Aug 15th, 2006
thankyoufor your suggestions. I used static array as mentioned by you that solves my problem. But now I need to modify the program whose array size is to be defined by the running time and hence a dyanimic array has to be used. I would therefore like to know as how to use dynamic array since the minor size is not known unlike what u have suggested for the same in
int (*EL)[col] = new int[row][col];
your kind help would see me run the program succesfully.

thanks and regards
Bharat
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: in C++ segmenatation fault

 
0
  #6
Aug 15th, 2006
Like this
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int ** alloc2D ( int row, int col ) {
  6. int **p = malloc ( row * sizeof *p ); /* all the row pointers */
  7. int *q = malloc ( col * row * sizeof *q );/* all the data */
  8. int r;
  9. for ( r = 0 ; r < row ; r++ ) { /* set pointers for each row */
  10. p[r] = q;
  11. q += col;
  12. }
  13. return p;
  14. }
  15.  
  16. void free2D ( int **arr ) {
  17. free( arr[0] ); /* free all the data */
  18. free( arr ); /* free the row pointers */
  19. }
  20. int main(void)
  21. {
  22. int **arr = alloc2D ( 10, 20 );
  23. int r, c;
  24. for ( r = 0 ; r < 10 ; r++ ) {
  25. for ( c = 0 ; c < 20 ; c++ ) {
  26. arr[r][c] = 0;
  27. }
  28. }
  29. free2D( arr );
  30. return 0;
  31. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC