0

我正在使用指向 1D 数组的指针数组来模拟 2d int 数组。大小是动态的,因为数据是从文件中读取的,所以我创建了一个动态分配函数(alloc2DArrInt)。在我开始用新数据测试我的程序之前它一直运行良好,现在第一个 malloc 有时会崩溃(分段错误?)。这是代码的相关(我希望)部分:

int** t_fv = NULL; // these are global
int** t_ofv = NULL;
int** b_fv = NULL;
int** b_ofv = NULL;

// assume these 2 lines are in main:

if (readFeatureVectors(&t_fv, &t_ofv, targetFilename, &trows, &tcolumns) < 0) { }
if (readFeatureVectors(&b_fv, &b_ofv, backgroundFilename, &brows, &bcolumns) < 0) { }

int readFeatureVectors(int*** fv, int*** ofv, char* fileName, int* rows, int* columns) {
    // hidden code
    alloc2DArrInt(fv, *rows, *columns); //&*fv
    alloc2DArrInt(ofv, *rows, *columns);
    // hidden code
}

void inline alloc2DArrInt(int*** array, int rows, int columns) {
    int i;
    *array = malloc(rows * sizeof(int*)); // sometimes crashes
    if (*array != NULL ) {
        for (i = 0; i < rows; i++) {
            (*array)[i] = malloc(columns * sizeof(int));
            if ((*array)[i] == NULL ) {
                printf("Error: alloc2DArrInt - %d columns for row %d\n", columns, i);
                exit(1);
            }
        }
    } else {
        printf("Error: alloc2DArrInt - rows\n");
        exit(1);
    }
}

t_fvt_ofvb_fv的分配工作但程序在b_ofv的第一个 malloc 处崩溃。当我切换readFeatureVectors调用的顺序时,程序在t_fv(不是t_ofv)的第一个 malloc 处崩溃。

我还开发了该函数的 realloc 和 dealloc 版本,但此时它们在代码中并没有发挥作用。

我知道我应该开始使用调试器或内存检查工具,但我无法让它与 Eclipse Juno 一起使用。我可能会迁移到 Ubuntu 并尝试使用 valgrind,但如果可能的话,我希望现在避免使用它。

4

2 回答 2

0

I don't think there is anything in your code that you showed here that should be making it crash. I put your program into eclipse and ran debug mode on it and it runs fine. I used the following code:

#define NULL 0

int** t_fv = NULL; // these are global
int** t_ofv = NULL;
int** b_fv = NULL;
int** b_ofv = NULL;

int main()
{
    int trows = 3000;
    int tcolumns = 3000;

    int brows = 5000;
    int bcolumns = 5000;

    char targetFilename[64] = "target";
    char backgroundFilename[64] = "background";

    if (readFeatureVectors(&t_fv, &t_ofv, targetFilename, &trows, &tcolumns) == 1)
    {printf("Worked1\n"); }

    if (readFeatureVectors(&b_fv, &b_ofv, backgroundFilename, &brows, &bcolumns) == 1)
    { printf("Worked2\n"); }

    printf("We are done now\n");

}

int readFeatureVectors(int*** fv, int*** ofv, char* fileName, int* rows, int* columns) {
    // hidden code
    alloc2DArrInt(fv, *rows, *columns); //&*fv
    alloc2DArrInt(ofv, *rows, *columns);
    // hidden code

    return 1;
}

void inline alloc2DArrInt(int*** array, int rows, int columns) {
    int i;
    *array = malloc(rows * sizeof(int*)); // sometimes crashes
    if (*array != NULL ) {
        for (i = 0; i < rows; i++) {
            (*array)[i] = malloc(columns * sizeof(int));
            if ((*array)[i] == NULL ) {
                printf("Error: alloc2DArrInt - %d columns for row %d\n", columns, i);
                exit(1);
            }
        }
    } else {
        printf("Error: alloc2DArrInt - rows\n");
        exit(1);
    }
}

Stepping through step by step, it all seems to work just fine. I tried smaller arrays first of 3x3 and 5x5 and they worked as well. I then tried 10 times the size and I still didn't run out of memory.

My first guess would be that your sizes of the rows and columns you are requesting are too large and you are running out of memory. You could put a busy wait in your code somewhere and do a memory check to see how much your program is taking (free in linux, taskmgr in windows). Also, you could printf rows and columns to make sure you aren't requesting something of an unreasonable size.

I would think it may be possible that you are having problems with your //hidden code as well.

You would be well served to get Eclipse or something similar up and running. I think you will find your problem very quickly with a debug tool like that as you will be able to find the exact line you are crashing on and figure out the state of your memory at that time.

于 2013-07-29T13:37:32.293 回答
0

malloc崩溃的唯一原因是堆数据结构损坏,或者array指针无效。最有可能的结果是 SIGSEGV。

否则,无论您传递给它的任何参数,malloc 都不会崩溃。它在最坏的情况下会返回 NULL。

例如,由于缓冲区溢出/下溢,堆数据结构可能会损坏。使用 valgrind 来检测那个或无效的array指针条件。

于 2013-07-29T13:45:43.583 回答