views:

126

answers:

0

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

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

When i define array of double at start of InitGL:

double arr[1000];

and modify it in InitGL all works fine.

When i allocate memory for this array dynamically and call foo from InitGL all works fine too:

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

But when i define array at start of InitGL and call foo from InitGL:

double p[1000];
foo(p);

i get segmentation fault at line

p[0] = 1.0;

Where is the error?

[UPDATE]

Very very stupid mistake :D
Initially i've writen 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 there was segmentation fault because i must declare number of columns (i read it 30 minutes ago in Kernighan and Ritchie book :)
When i fix function declaration to

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-loop where i forgot change 10000 to 1000.
Now all works fine. Thanks for all!