简体   繁体   English

C - Malloc导致程序崩溃

[英]C - Malloc causing program to crash

I am writing this C code which takes in a file and reads in values from it, the code doesn't do anything yet, but this is what I have so far. 我正在编写这个C代码,它接收一个文件并从中读取值,代码还没有做任何事情,但这是我到目前为止所做的。 The program is crashing in the block that is calling four mallocs. 该程序在调用四个malloc的块中崩溃。 The program works fine if I comment out y, f, and yp. 如果我注释掉y,f和yp,该程序工作正常。 I don't know what it is causing it. 我不知道是什么造成的。 So any help will be appreciated. 所以任何帮助将不胜感激。

Note: I am testing this on ubuntu with gcc. 注意:我正在使用gcc在ubuntu上进行测试。 And I did trying casting the malloc to "(float *)" but I still get the same error. 我确实尝试将malloc转换为“(float *)”,但我仍然得到同样的错误。

int main( int argc, char *argv[])
{
    FILE *rhs, *output;
    int niter, n, i = 0, j = 0, k = 0, n1 = n + 1;

    rhs = fopen(argv[1], "r");
    // ab+ opens file for writting and creates the file if need be
    output = fopen(argv[2], "ab+");
    niter = atoi(argv[3]);

    // check if files open up or not, if not exit.
    if((rhs == NULL) || (output == NULL))
    {
        printf("Error Opening files.\n");
        exit(1);
    }

    // read in N
    fscanf(rhs, "%d", &n);

    // THIS IS THE BLOCK CAUSING THE CRASH
    // CODE WORKS WHEN I COMMENT OUT LINES AND ONLY LEAVE ONE OF THEM IN
    // generate array to hold values from rhs file
    float *numbers = malloc(sizeof(float) * ((n1)*(n1)));
    float *y = malloc(sizeof(float) * ((n1)*(n1)));
    float *f = malloc(sizeof(float) * ((n1)*(n1)));
    float *yp = malloc(sizeof(float) * ((n1)*(n1)));

    // get numbers and store into array
    while(fscanf(rhs, "%f", &numbers[i]) != EOF)
    {
        printf("In while %f\n", numbers[i]);
        i++;
    }

    fclose(rhs);

    return 0;

} }

One issue is: 一个问题是:

You are initializing n1 with an uninitialized value from n : 您正在使用来自n的未初始化值初始化n1

int niter, n, i = 0, j = 0, k = 0, n1 = n + 1;
                                        ^
                                        +-- "n" is not initialized here, might have any value.
                                            thus, "n1" is also not initialized to a known value.

Therefore, your call to malloc most likely recieves a too large value to be allocated at all. 因此,您对malloc的调用很可能会收到太大的值,无法分配。 Initialize "n1" after you have read "n": 读完“n”后初始化“n1”:

// read in N
fscanf(rhs, "%d", &n);
n1 = n + 1;

In any case, it is worth checking the return value from malloc() to see if it returned NULL in case the memory could not be allocated. 在任何情况下,值得检查malloc()的返回值,以查看是否在无法分配内存时返回NULL。

The n1 contains a garbage at the moment of the malloc() call. n1malloc()调用时包含一个垃圾。 Thus you simply try to allocate a HUGE amount of memory . 因此,您只需尝试分配大量内存

Check whether memory has been allocated successfully or not by checking the value of numbers, y, f, yp, if their value is NULL, then memory can't be allocated. 通过检查数字y,f,yp的值来检查内存是否已成功分配,如果它们的值为NULL,则无法分配内存。 How large is the value of n1? n1的值有多大?

It is because n1 is undefined at the point you call malloc() . 这是因为在调用malloc()时, n1未定义。

int niter, n, i = 0, j = 0, k = 0, n1 = n + 1;

The above line didn't initialize n , so n1 = n + 1 assigns an undefined value to n1 . 上面的行没有初始化n ,所以n1 = n + 1n1分配一个未定义的值。

You may need to put n1 = n + 1; 您可能需要输入n1 = n + 1; after fscanf(rhs, "%d", &n); fscanf(rhs, "%d", &n);之后fscanf(rhs, "%d", &n); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM