Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   "Segmentation fault" by value assignment (http://www.go4expert.com/forums/segmentation-fault-value-assignment-t5421/)

royxu 24Jul2007 04:11

"Segmentation fault" by value assignment
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:

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.

DaWei 24Jul2007 07:54

Re: "Segmentation fault" by value assignment
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?

royxu 24Jul2007 10:59

Re: "Segmentation fault" by value assignment
Thank you DaWei. I apologize for all the inconvenience and unpleasant feelings, but there is absolutely no intention of being rude... :o
Here's 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

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

re=1;  // controls initial theta
rez=16; // controls initial positions of particles
rep=7;  // same as above
p2=33; // controls Gaussian distribution

    for(k=1;k<=5;k++) //the first 5 terms of FFT

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.

DaWei 24Jul2007 16:32

Re: "Segmentation fault" by value assignment
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.

royxu 24Jul2007 21:25

Re: "Segmentation fault" by value assignment
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.

DaWei 24Jul2007 21:49

Re: "Segmentation fault" by value assignment
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.

royxu 25Jul2007 00:53

Re: "Segmentation fault" by value assignment
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.

royxu 25Jul2007 05:17

Re: "Segmentation fault" by value assignment
Thanks you very much Master DaWei, for your wonderful advice. The code is running well. I learnt a great deal from you.

All times are GMT +5.5. The time now is 12:36.