【问题标题】:Segmentation fault when call an static library function调用静态库函数时出现分段错误
【发布时间】:2010-10-05 13:20:45
【问题描述】:

我有简单的 OpenGL C 程序(来自NeHe lesson 2)。有初始化函数InitGL。
我的静态库中有函数foo

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

当我在InitGL 的开头定义双精度数组时:

double arr[1000];

并在InitGL 中修改它一切正常。

当我为这个数组动态分配内存并从InitGL 调用foo 时也一切正常:

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

但是当我在InitGL 的开头定义数组并从InitGL 调用foo 时:

double p[1000];
foo(p);

我在线路上遇到分段错误

p[0] = 1.0;

哪里出错了?

【问题讨论】:

  • 向我们展示实际代码——没有它,我们只是在猜测。
  • 提供的代码应该可以正常工作。我会从您的代码的另一部分归咎于内存损坏,因为它仅在放入堆栈时发生。
  • 很抱歉,我发现错误并更新了我的问题:)
  • 您应该将您的解决方案作为答案发布,然后接受它,因此这不会显示为未回答的问题。
  • @Software,我刚刚编辑了问题并将他们的“答案”发布为 wiki 答案,这在这种情况下似乎是合理的。

标签: c segmentation-fault static-libraries


【解决方案1】:

Silver_Ghost 已编辑的问题,在此处输入,因此问题不会显示为 0 个答案:

非常非常愚蠢的错误:D
最初我写了一个函数:

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 空间。当然有 分段错误,因为我必须 声明列数(我读过 30 分钟前在 Kernighan 和 里奇书 :) 当我修复功能时 声明到

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

一个我忘记改变的for循环 10000 到 1000。现在一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 2011-01-08
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    相关资源
    最近更新 更多