473,327 Members | 2,094 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,327 software developers and data experts.

'segmentation fault' error

This is my programme in c for solving differential equations.When i run my programme in terminal in ubuntu it it is displaying the following error 'segmentation fault'.For compling the programme gcc lorenz.c and ./a.out is the code.I think the problem with arrays in my programme but i am not able to find that mistake.I get a Segmentation fault if N=1000000 but not in case of 10000. Could anyone please help me with this.




/*
solving lorenz system of differential equations by using runge kutta method

*/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>


#define Step 0.01
#define N 1000000

FILE *fp_out;
const double sigma = 10;
double bet;
const double rho = 28;

int i,k;

double fx( double x, double y, double z)
{
double xdot;
xdot = sigma * (y - x) ;
return xdot;
}

double fy( double x, double y, double z)
{
double ydot;
ydot = x*(rho - z) - y;
return ydot;
}

double fz( double x, double y, double z)
{
double zdot;
zdot = y * x - bet * z;
return zdot;
}


/*
putting random initial conditions

*/
void initial(double *x, double *y, double *z)
{
int i;
//srand((unsigned int)time(0)); //Seed number for rand()
x[i] = (rand()/(RAND_MAX + 1.0));
y[i] = (rand()/(RAND_MAX + 1.0));
z[i] = (rand()/(RAND_MAX + 1.0));

}

void rk4(double *x, double *y, double *z)
{
double kx1, kx2, kx3, kx4;
double ky1, ky2, ky3, ky4;
double kz1, kz2, kz3, kz4;


for ( k = 0; k < N; k++ )

{
{
kx1 = Step*fx( x[k], y[k], z[k]);
ky1 = Step*fy( x[k], y[k], z[k]);
kz1 = Step*fz( x[k], y[k], z[k]);

kx2 = Step*fx( x[k] + 0.5*kx1, y[k] + 0.5*ky1, z[k] + 0.5*kz1);
ky2 = Step*fy( x[k] + 0.5*kx1, y[k] + 0.5*ky1, z[k] + 0.5*kz1);
kz2 = Step*fz( x[k] + 0.5*kx1, y[k] + 0.5*ky1, z[k] + 0.5*kz1);

kx3 = Step*fx( x[k] + 0.5*kx2, y[k] + 0.5*ky2, z[k] + 0.5*kz2);
ky3 = Step*fy( x[k] + 0.5*kx2, y[k] + 0.5*ky2, z[k] + 0.5*kz2);
kz3 = Step*fz( x[k] + 0.5*kx2, y[k] + 0.5*ky2, z[k] + 0.5*kz2);

kx4 = Step*fx( x[k] + kx3, y[k] + ky3, z[k] + kz3);
ky4 = Step*fy( x[k] + kx3, y[k] + ky3, z[k] + kz3);
kz4 = Step*fz( x[k] + kx3, y[k] + ky3, z[k] + kz3);


x[k+1] = x[k] + ( kx1 + 2.0*( kx2 + kx3 ) + kx4 )/6.0;
y[k+1] = y[k] + ( ky1 + 2.0*( ky2 + ky3 ) + ky4 )/6.0;
z[k+1] = z[k] + ( kz1 + 2.0*( kz2 + kz3 ) + kz4 )/6.0;

}
}
}

void out_sys( double *x, double *y, double *z)
{
for( k = 9000; k < N; k++ )
{
fprintf(fp_out,"%lf\t", k*Step);
fprintf(fp_out,"%lf\t%lf\t%lf\n", x[k], y[k], z[k]);
}
}

int main()
{
double x[N];
double y[N];
double z[N];

char fn_out[50];
int i;

/*
give value of bet
*/
{
//initialize

bet=8/3;

initial(x,y,z);

rk4(x,y,z);

//writing the output file
sprintf(fn_out,"bh_%.1lf.out",bet);
fp_out = fopen(fn_out,"w");
out_sys(x,y,z);

fclose(fp_out);

}
return 0;
}
Mar 31 '12 #1

✓ answered by weaknessforcats

Then you are address modifying yourself beyond the array bounds.

Like maybe right here:

Expand|Select|Wrap|Line Numbers
  1. x[k+1] = x[k] + ( kx1 + 2.0*( kx2 + kx3 ) + kx4 )/6.0;
  2.  y[k+1] = y[k] + ( ky1 + 2.0*( ky2 + ky3 ) + ky4 )/6.0;
  3.  z[k+1] = z[k] + ( kz1 + 2.0*( kz2 + kz3 ) + kz4 )/6.0;
k varies from 0 to <N. Therefore, on the Nth cycle of the loop k<N is true so you go inside the loop and operate on K+1, which is N but there is no Nth element of the array. Remember, for arrays the index varies from 0 to <N.

9 2447
weaknessforcats
9,208 Expert Mod 8TB
It may be right here:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2.  { 
  3. double x[N];
  4.  double y[N];
  5.  double z[N];
  6.  etc...
N is 1000000 so you have 3 million doubles where each double is probably 8 bytes resulting in 24MB on the stack.

Often stack memory is limited so you may have exhausted all of your stack memory.

So the first thing I would do is use malloc() to allocate these arrays on the heap, which is essentially unlimited in size.

You would not need to change anything else in your code.
Mar 31 '12 #2
A quick way to solve this is to make your those variables global variables (outside the int main function), which will be on the heap.
Mar 31 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
You can't make that statement. The concept of "stack" and "heap" are not part of the language specification. The only way to know a variable is on the heap is to allocate it yourself. Otherwise, your compiler will allocate it and you have no idea where it is.

All you can say is that a variable defined outside any block (pair of braces) has the scope from the point od definition to the end of the source file that defines it.
Mar 31 '12 #4
Ok. Thanks for the correction. I shouldn't say it's heap then. All I can say is that it has space enough for the original poster's 1000000-sized array. (I had always been declaring large arrays as global variables; didn't think anything was wrong with that.)
Apr 1 '12 #5
I have used it but still i am not getting.can you pls correct it in my programme because i am newer to c and i dont know how to use malloc()[where i have to use it] in my programme.
Apr 2 '12 #6
If you want to do it my way, just transfer these three lines
Expand|Select|Wrap|Line Numbers
  1. double x[N];
  2. double y[N];
  3. double z[N];
all the way to the top, after #define N 1000000. Note that although you have local variables x, y and z in the other functions, within those functions, x, y and z will still refer to the local variable (in that scope the global variables x, y and z are "hidden").

If you want to do it weaknessforcats's way, don't do the above. Instead delete the three lines mentioned, and in its place put:
Expand|Select|Wrap|Line Numbers
  1. double *x,*y,*z;
  2. x=(double*)malloc(sizeof(double)*N);
  3. y=(double*)malloc(sizeof(double)*N);
  4. z=(double*)malloc(sizeof(double)*N);
Or I might make it:
Expand|Select|Wrap|Line Numbers
  1. double *x,*y,*z;
  2. x=(double*)malloc(sizeof(double)*N*3);
  3. y=x+N;
  4. z=y+N;
just for fun.
Apr 2 '12 #7
thanks yaar but , i tried all methods which are shown above but still i am getting the same error 'segmentation fault'
Apr 2 '12 #8
weaknessforcats
9,208 Expert Mod 8TB
Then you are address modifying yourself beyond the array bounds.

Like maybe right here:

Expand|Select|Wrap|Line Numbers
  1. x[k+1] = x[k] + ( kx1 + 2.0*( kx2 + kx3 ) + kx4 )/6.0;
  2.  y[k+1] = y[k] + ( ky1 + 2.0*( ky2 + ky3 ) + ky4 )/6.0;
  3.  z[k+1] = z[k] + ( kz1 + 2.0*( kz2 + kz3 ) + kz4 )/6.0;
k varies from 0 to <N. Therefore, on the Nth cycle of the loop k<N is true so you go inside the loop and operate on K+1, which is N but there is no Nth element of the array. Remember, for arrays the index varies from 0 to <N.
Apr 2 '12 #9
AlexGr
1
FOUND A SOLUTION !!
although nothing from above helped me either when i visited the site for a solution , i came back to post my solution.

The error is probably about the memory used . I am not linux or c++ (i started 2 months ago) geek but , you can get rid of the arrays and print your computation in a txt with a very simple #include <ftsream> header

it would be somethin like that
...
#include <fstream>

int main(){

ofstream txtvalues;
txtvalues.open("/home/put_your_path_here/file_name.txt");
x0=...;
y0=...;
z0=....;
t0=....;

x=x0;
y=y0;
z=z0;
t=t0;
for(int i......){
....
RUNGE KUTTA HERE
...
txtvalues<<t<<" "<<x<<" "<<y<<" "<<z<<"\n";
}
txtvalues.close()

}

And you are done . If you want to visualize your results , u can use gnuplot , it is quite easy.

Hope i helped someone
Feb 29 '16 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Polar | last post by:
Hi! i'm a newbie in C language and i'm writing my first simple codes. In one of these, my purpose is to append the ascii value of an interger (example 101 --> e) at the end of a string to...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
1
by: utab | last post by:
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <iterator> using std::cout; using std::vector; using std::string; using std::endl;
3
by: Dhieraj | last post by:
While compiling a C++ code I am getting the following error : CC -c -I/opt/iona/artix/2.0/include -I/opt/iona/asp/6.0/include -I/opt/ar/api63/include -I//var/tmp/vidya/aotscommon/include ...
14
by: cnixuser | last post by:
I am new to C programming and am still at an early level in java and C#. I am posting regarding a segmentation fault error code I get when I try to run a program that I am developing. I am coding on...
1
by: Alvin | last post by:
Note: Source code and compile commands are at the bottom. I've spent some time with a friend making a nice and simple to use OOPified C++ networking layer for TCP that works on multiple...
4
by: william | last post by:
I received a segfault error in a very short program for test purpose. I found no luck even if I read some tricks about tracking down the segfault. Can any one give me a hint which line of the...
1
by: Sharad Maloo | last post by:
Hi, I am running the image file of my project, it is giving following segmentation fault: ERROR----- > Program received signal SIGSEGV, Segmentation fault. 0x004240cb in strlen () from...
20
by: enjoyfate | last post by:
I am newbie ,in linux/gnu envirement,wrot test file below to see the first argument. #include "stdio.h" void main (int argc, char *argv) { printf ("%s", argv); } After compiled it, $ ./test...
1
by: jcancino | last post by:
The program compiles and works all the way until the multiplecheck function, any help would be great. The program is not finished yet and I am new to C programming so I know the program may not be...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.