Segmentation error when calling a static library function

I have a simple OpenGL C program (from the NeHe 2 lesson ). There is an initialization function InitGL.
And I have a foo function in my static library:

 void foo(double *p) { p[0] = 1.0; } 

When I define a double array at the beginning of InitGL :

 double arr[1000]; 

and change it in InitGL , everything works fine.

When I allocate memory for this array dynamically and call foo from InitGL , everything works fine:

 double *p = (double *)malloc(1000 * sizeof(double)); foo(p); 

But when I define the array at the beginning of InitGL and call foo from InitGL :

 double p[1000]; foo(p); 

I get a segmentation error in the line

 p[0] = 1.0; 

Where is the mistake?

+4
source share
1 answer

Silver_Ghost edited the question introduced here so that the question does not appear as having 0 answers:

Very stupid mistake: D
I originally wrote a function:

 void cg_random_points(double **dest, int n, double xl, double xr, double yl, double yr, double zl, double zr) for generate set of random points in 

3D space. Of course, the segmentation error, because I have to declare the number of columns (I read it 30 minutes ago in Kernighan and Richie's Book :) When I correct the declaration function

 void cg_random_points(double (*dest)[3], int n, double xl, double xr, double yl, double yr, double zl, double zr) segmetation fault continued throw by 

one for the loop where I forgot to change 10000 to 1000. Now everything works fine.

+1
source

All Articles