使用malloc时出错 [英] Error using malloc

查看:103
本文介绍了使用malloc时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将char ** inputmain()传递给processInExp()函数,然后再次将其从processInExp()函数传递给getInput()函数,以在读取文件时动态分配它.

I pass char ** input from main() to processInExp() function, then I pass it again from processInExp() function to getInput() function to dynamically allocate it over while reading through the file.

内部getInput()函数input在检查时已正确分配了内存,但是在in processInExp()中使用它时遇到运行时错误.可能是什么问题?

Inside getInput() function input is allocated memory properly when checked, but while using it in in processInExp() it encounters gets runtime error. What can be the issue?

下面是我的代码:

int getInput(char ** input, const char * fileName)
{
    int numInput = 0;
    int i, j;
    char c;
    char tempInput[100];
    FILE * pFile;
    if((pFile = fopen(fileName, "r")) == NULL)
    {
        printf("Cannot read file %s\n", fileName);
        system("PAUSE");
        exit(1);
    }
    while(!feof(pFile))
    {
        c = fgetc(pFile);
        if(c == '\n') ++numInput;

    }
    /* printf("%d\n", numInput); */
    input = (char**)malloc(numInput * sizeof(char*)); /* #2 MALLOC input */
    rewind(pFile);
    for(i = 0; !feof(pFile); ++i)
    {
        fscanf(pFile, "%[^\n]%*c", tempInput);
        /* printf("%s\n", tempInput); */
        input[i] = (char*)malloc((strlen(tempInput) + 1) * sizeof(char)); /* #3 MALLOC input[] */
        strcpy(input[i], tempInput);
        /* printf("%s\n", input[i]); */ /* #4 PRINT OUT PERFECTLY */
        memset(tempInput, 0, sizeof(tempInput));
    }
    fclose(pFile);
    return numInput;
}
void processInExp(char ** input, char ** output, const char * fileName)
{
    int numFormula;
    int i;

    numFormula = getInput(input, fileName); /* #1 PASSING input */
    /* printf("%s\n", input[0]); */ /* #5 RUNTIME ERROR */
    output = (char**)malloc(numFormula * sizeof(char*));
    system("PAUSE");

    for(i = 0; i < numFormula; ++i)
    {
        convertIntoPost(input[i], output[i]);
        printf("%d. %s -> %s", (i + 1), input[i], output[i]);
    }

}

推荐答案

C使用值传递来传递函数参数.因此,从函数getInput()内部,您无法更改变量input,并且期望该更改会反映在传递给函数的实际参数中.为此,您需要传递一个指向指针的变量,例如在这种情况下,您需要这样做

C uses pass-by-value for function argument passing. So, from inside the function getInput(), you cannot change the variable input and expect that change to be reflected back in the actual argument, passed to the function. For that, you'll need a pointer-to variable to be passed, like in this case, you need to do

   int getInput(char *** input, const char * fileName) { //notice the extra *

并且需要这样称呼

   char ** inp = NULL;
   getInput(&inp, ..........);

然后,getInput()将能够在该函数内部的*input中分配内存,该内存将反映到inp中.

Then, getInput() will be able to allocate memory to *input inside the function which will be reflected into inp.

否则,从getInput()返回后,实际参数仍将未初始化,并进一步使用该参数(在您的情况下,在processInExp()函数中的for循环中)将导致

Otherwise, after returning from the getInput(), the actual argument will still be uninitialized and using that further (in your case, in the for loop in processInExp() function) will lead to undefined behaviour.

也就是说,还有两点要注意,

That said, two more important things to notice,

  1. 查看为什么不强制转换 malloc()及其C中的family的返回值.
  2. 检查为什么while ( !feof (file) )总是错误的?
  1. Please see why not to cast the return value of malloc() and family in C.
  2. Check Why is while ( !feof (file) ) always wrong?

这篇关于使用malloc时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆