944,184 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 976
  • C++ RSS
Aug 13th, 2006
0

in C++ segmenatation fault

Expand Post »
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” .

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bharat.bhooshan is offline Offline
2 posts
since Aug 2006
Aug 14th, 2006
0

Re: in C++ segmenatation fault

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])
Reputation Points: 13
Solved Threads: 0
Newbie Poster
psycho^ is offline Offline
5 posts
since Aug 2006
Aug 14th, 2006
0

Re: in C++ segmenatation fault

@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.
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Aug 14th, 2006
1

Re: in C++ segmenatation fault

> 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;
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 15th, 2006
0

Re: in C++ segmenatation fault

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bharat.bhooshan is offline Offline
2 posts
since Aug 2006
Aug 15th, 2006
0

Re: in C++ segmenatation fault

Like this
C++ Syntax (Toggle Plain Text)
  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. }
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: friend plz help me to solve this problem
Next Thread in C++ Forum Timeline: find the vector with the most elements





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC