I have simple OpenGL C program (from NeHe lesson 2). There is initialization function InitGL.
And i have functionfoo
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:
doubl开发者_如何学Ce *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?
Silver_Ghost's edited question, entered here so the question doesn't show up as having 0 answers:
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.
精彩评论