"Segmentation fault" by value assignment

royxu's Avatar
Light Poster
Hi folks. I am encoutering a problem in C where I try to assign values to a 2D array. There is no violation in declaration, etc, but it keeps poping up the "segmentation fault" message.
And the debugger tells something wrong with the line:
v[i][j]=0;

I am totally lost at this point. Has anybody been exposed to this situation before? Similar value assignment of 2D arrays has been run in a short and separate code.

I will appreciate if anybody shares their experience on this issue. Thanks.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Okay, listen up. There are syntax errors, where the compiler will give a warning or error messages. There are failures to include the appropriate libraries, so that the linker will issue an error message. Then there are the programming errors that make a system fail at run time. These are usually segmentation faults (trying to access memory which is not yours to access) or bus faults (trying to access memory off-boundary).

The latter are often due to dereferencing an invalid pointer, or due to trying to access an array beyond its valid bounds. Since you mention an array, I'd put a few bucks on that.

Now, if you were to read the "Before you make a query" post, you would see some statements to the effect that information (preferably accurate information) is key to debugging. You DO mention an array, in passing, but you might consider posting a snippet of code which reproduces the fault.

Incidentally, if you do post code, refer to that same thread regarding the use of code tags, when properly posting code.

Just curious. You have one post. That means new user. Does the title, "Before you make a query" even ring a bell, or do you just not give a chit about being rude to the people you ask for free help?
0
royxu's Avatar
Light Poster
Thank you DaWei. I apologize for all the inconvenience and unpleasant feelings, but there is absolutely no intention of being rude...
Here's code:

Code:
#include <stdio.h>
#include <math.h>
//double pow(double x, double y);
//#include <grid.h>
//#include <stddev.h>

//#include "ranlib.h"
#define M 100 
#define N 200
#define P 100
#define Q 400000
#define X 200  //width
#define Y 400  // height
#define T 500
#define K 0.6
#define MU 0.00089 //viscosity water
#define PHm 0.68
#define n0 20
#define ph0 0.1
#define C 2147483647 // 2^31-1
#define A 16807 //7^5

main()
{
int i, j, k, l, re, rez, rep, p2;
double bin, pi, delx, dely, delt, s, vx1, vx2, vy1, vy2, a, vv, theta, zeta, psi, nn, gau;
double t[P], xx[M+1], yy[N+1], no[M][N][P], D[P][Q], v[M+1][N+1], gammadot[M][N], phi[M][N][P], yita[M][N][P];
double bex[Q][P], bey[Q][P];
//M separations in X direction
//N   separations in Y direction
//P  no. time intervals
//Q  no. particles

pi=4.0*atan(1.0);
re=1;   // controls initial theta
rez=16; // controls initial positions of particles
rep=7;  // same as above
p2=33; // controls Gaussian distribution
theta=2*pi*re/C;
a=.1;
vv=3*pi*a*a*a/4;
delx=X/M;
dely=Y/N;
delt=T/P;
bin=0;
for(i=0;i<=P-1;i++)
{
t[i]=delt*i;
}

for(i=0;i<=M;i++)
{
  xx[i]=i*delx;
  for(j=0;j<=N;j++)
  {
  yy[j]=j*dely;
  v[i][j]=0;
    for(k=1;k<=5;k++) //the first 5 terms of FFT
    {
    s=2*k-1;
    v[i][j]=v[i][j]+1/s/s/s*(1-cosh(s*pi*(xx[i]-X/2)/Y)/cosh(s*pi*X/(2*Y)))*cos(s*pi*(yy[j]-Y/2)*X/(2*Y*Y)); 
    }
  }
}
The debugger targets the 8th last line: v[i][j]=0 that caused the segmentation fault. Also the 5th last line: s=2*k-1 will cause the same error if I disable v[i][j]=0
Thank you for your time and hopefully someone can help out.

Last edited by shabbir; 24Jul2007 at 11:37.. Reason: Code block
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
I can't run your code. You are declaring so many large arrays as locals that I'm getting a stack overflow. Your code as shown does have a missing closing brace. The flaky indentation makes it difficult to pick up on. You also have a noob error. Main returns an int. You should therefore declare it as such and put a return statement at the end. C++ will do that for you. A compliant C compiler won't. Even if you have a loose compiler that allows that, you should develop the habit of including it.

Rather than rewrite your program to use dynamic memory, at this time, I'll just suggest that you are exceeding the bounds of your array. Check your i,j values at the time of the fault.
0
royxu's Avatar
Light Poster
Thanks a lot DaWei. I'm really naive in programming and I did miss the closing brace when posting the code.
I'll try following your suggestions. Btw, the debugger (gdb under Linux) shows that the program stops at i=j=0. So I don't think the bounds were exceeded.
Again, thank you for you time and patience.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Okay, rather than allocate the memory on the heap, I just added a static keyword to the two lines declaring all the arrays. This prevents them from being allocated on the stack.

The program ran to completion with no faults. I suspect that your message is due to a stack overflow, possibly undetected on your box (I'm on a Windows box). This could cause access to memory that you don't have access rights to.
0
royxu's Avatar
Light Poster
Thank you so much DaWei. I think your comments make sense. However, my machine is down at this moment... will let you know later. Have a good one.
0
royxu's Avatar
Light Poster
Thanks you very much Master DaWei, for your wonderful advice. The code is running well. I learnt a great deal from you.