2

我有一个小型c++程序无法分配超过 2880 个malloc. 该声明:

void* tmpptr = malloc(2881);

崩溃,同时

void* tmpptr = malloc(2880);

没有。_ 每次!

我正在使用 MinGW 并使用

g++ -std=c++0x -pedantic -Wall -Wextra -g -D_GLIBCXX_DEBUG -static-libgcc -static-libstdc++

我知道malloc在 C++ 中不鼓励使用 of ,并且无论如何我都打算重写它,但我仍然想知道为什么这不起作用。当我用 gcc 编译它时,相同的代码一直在工作。

更新:这是主要的函数调用:

image * img = readPPM("pic/pic.ppm");
bw_image * sky = skyline(img, ref);
cont * lin = contour(sky, 0); // <-- chash
...

函数开始于:

#include <cstdlib>
cont * contour(const bw_image * img, const char wrap) {
    int test = 2880;
    void* ptr1 = malloc(test);
    void* ptr2 = malloc(test);
...

现在第一个malloc可以工作,但第二个不行。如果我改变test = 1440;,同样的结果。但; 因为test = 140;第一个已经malloc失败了。

我已经单独尝试了代码:

int main(int argc, char *argv[]) {
    int size = 2881;
    void* tmpptr;
    printf("Allocating, %d\n", size);
    tmpptr = malloc(size);
    printf("Allocated %d bytes successfully\n", size);
}

它可以毫无问题地工作,所以它似乎是在main做这件事。

rem_artifacts看起来像这样

void rem_artifacts(bw_image * sky) {
    for (int y = 0; y < sky->y; ++y) for (int x = 0; x < sky->x; ++x) {
        int xp = x - 1, xn = x + 1;
        if (xp < 0) xp = sky->x - 1;
        if (xn == sky->x) xn = 0;
        int c = sky->data[x][y];  // this is wrong
        if (
            (y == 0 || sky->data[x][y-1] != c) &&  // and this
            (y == sky->y-1 || sky->data[x][y+1] != c) && // and this
            sky->data[xp][y] != c &&  // and this
            sky->data[xn][y] !=c  // and this
        ) sky->data[x][y] = !c; // and this
    }
}
bw_image * skyline(const image * img, const image * ref) {
    double tilt = 114.0 - 90.0;
    double pi = 3.14159265358979323846;
    double chang = 360.0 / 2.0 / pi;
    //double sint = sin(tilt / chang);
    //double cost = cos(tilt / chang);
    bw_image * sky = (bw_image*)malloc(sizeof(bw_image));
    sky->x = img->x;
    sky->y = img->y;    //  
    double cos30 = sqrt(3)/2;
    int lim0 = (int)((double)(img->y) / 2.0 + (double)(img->x) * tan(tilt/chang) * cos30);
    sky->data = (char**)malloc(sizeof(char*) * sky->y);
    for (int y = 0; y < sky->y; ++y) {
        sky->data[y] = (char*)malloc(sizeof(char) * sky->x);
        for (int x = 0; x < sky->x; ++x)
            sky->data[y][x] = !(y < lim0 && colour_dist_sq(img->data[y][x], ref->data[y][x]) < 5000.0);
    }
    rem_artifacts(sky);
    return sky;
}
4

3 回答 3

2

谢谢大家,

事实证明,我是在分配的内存之外写的(有点混淆了 ax 和 y)。奇怪的是,尽管该程序一直运行到现在(24/7 持续了好几个月)并且按照我的期望运行。它可以解释一些我无法解释的奇怪事情。

现在我无论如何都会用标准的 C++ 内存分配重写代码。

于 2013-03-27T19:47:18.603 回答
0

It looks to me like your heap is getting corrupted somewhere else in your program (probably during that big loop in main. The first thing you can do is start turning on warnings -Wall and -Wnon-virtual-dtor at a very minimum and then fixing them.

If you have access to Linux your best bet is to run this under valgrind and be amazed as it tells you exactly where you're overwriting memory.

Alternately if you have access to Purify ($$$) on Windows that may be able to help you as well. There are also memory checking malloc libraries available you may be able to use.

If none of those tools are available you're going to have to do diagnostics yourself by trying to remove sections of code and see if it still crashes or not.

于 2013-03-27T14:46:08.290 回答
0

如果我敢猜测,那是您没有正确分配sky->data或其子指针。

这必须分两步完成:

  1. 首先分配sky->data

    sky->data = new char*[some_x_size];
    
  2. 然后你必须分别分配所有子数组:

    for (int x = 0; x < some_x_size; x++)
        sky->data[x] = new char[some_y_size];
    

您当然可以使用malloc而不是new.

释放数据时,您会以相反的方式进行操作:首先释放循环中的每个子数组,然后释放主数组。

于 2013-03-27T15:00:39.063 回答