【问题标题】:Segmentation fault after while loop that follows mallocmalloc 之后的 while 循环后的分段错误
【发布时间】:2015-03-31 05:04:13
【问题描述】:

我正在尝试动态创建一个二维数组,然后打开一个 txt 文件并复制每个宽松的二维数组。然后将此数组保存回我的主目录。我一直遇到分段错误。任何建议如何修复此代码? 顺便说一句,我认为问题在第二次 while 循环发生后出现......

    #include<stdio.h>

    char **randomArrayofStrings(){
        char **twoArray=null;
        int rows=50;
        int col=20;
        i=0;
        FILE *file=null;
        int messageSize=50;//this is number is trivial
        file = fopen("somefile.txt","r");
        twoArray= malloc(rows*sizeof(char*));
        for(i=0;i<col;i++)
        {
             twoArray[i]=malloc(rows*sizeof(char));
             strcpy(twoArray[i], "some random word");
        }
        while(!feof(file))
        {
             fgets(dArray[i],messageSize, file);
             strtok(dArray[i], "\n");
             i++;
        }   
        return twoArray;
    }


    int main(int argc, char **argv)
    {
        char **localArray=null;
        localArray=randomArrayofStrings();
        for(i=0;i<20;i++)//20 is just a random number
             printf("Strings: %s", localArray[i]);
    }

【问题讨论】:

  • return twoArray -> return twoArray;(缺少;)。

标签: c arrays pointers segmentation-fault malloc


【解决方案1】:

如我所见,在您的函数 randomArrayofStrings 循环 for 中通过列 "i cols 在您的代码中。因此,您首先分配指针数组并将其视为 cols,然后在循环中分配 @ 987654324@.

malloc之后检查返回的值,如果内存分配后为NULL,则不要使用指针。

要释放分配的内存,请使用相反的顺序 - 在循环中释放所有 rows,然后释放 cols 一次。例如:

        for(i=0;i<col;i++){
            free(twoArray[i]);
        }
        free(twoArray);
        twoArray = NULL;

编辑:

此外,要使用mallocfree,您需要#include &lt;stdlib.h&gt;#include &lt;string.h&gt; 用于strcopyint i=0; 应该代替i=0;,并且指针的正确空值是@987654336 @。

dArray 是什么?我没有看到声明或定义?你是说twoArray吗?

EDIT2:

以下是我的程序版本:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char **randomArrayofStrings(){
    char **twoArray=NULL;
    char * ptr = NULL;
    int rows=50; // this will be also message size
    int cols=20;
    int i=0;
    FILE *file=NULL;
    file = fopen("somefile.txt","r");
    if( file == NULL )
        return NULL;
    twoArray = (char**) malloc(cols * sizeof(char*));
    if(twoArray == NULL)
    {
        return NULL;
    }
    for(i=0;i<cols;i++)
    {
        twoArray[i] = (char*)malloc(rows*sizeof(char));
        if(twoArray[i] == NULL)
            return NULL;
        strcpy(twoArray[i], "some random word");
    }
    i = 0; // reset counter
    while(!feof(file))
    {
        fgets(twoArray[i], rows, file);
        ptr = strchr(twoArray[i],'\n');
        if( ptr )
            *ptr = '\0';
        else
            twoArray[i][rows-1] = '\0';
        i++;
        if( i >= cols)
            break;
    }   
    fclose(file);
    return twoArray;
}

void freeMy2dArray(char **twoArray, int n)
{
    int i;
    for(i=0; i < n; i++){
         free(twoArray[i]);
    }
    free(twoArray);
    twoArray = NULL;
}


int main(int argc, char **argv)
{
    int i;
    char **localArray=NULL;
    localArray = randomArrayofStrings();
    if( localArray == NULL )
        return 1;
    for(i=0;i<20;i++)//20 is just a random number
        printf("Strings: %s\n", localArray[i]);
    freeMy2dArray(localArray, 20);
}

【讨论】:

    【解决方案2】:

    您不应该在randomArrayofStrings() 中使用free() twoArray。使用完分配的内存后,您必须在 main() 中释放它们。

    也就是说,您在main() 中使用sizeof(localArray) 的方式是错误的。您必须使用您用来填充 twoArray 的确切值。

    【讨论】:

    • 我不明白如果我将其地址复制到 localArray 后如何释放(twoArray),(根据我的菜鸟知识)main 无法访问和释放 twoArray
    • @RT89 好吧,如果上述情况属实,您为什么要返回twoArray?它不应该在main() 中使用,对吧?好吧,它在main() 可用和有效的,除非你free() 它。动态内存分配范围是全局的,只要你有一个 valid 指针来访问它,就可以了。
    • @RT89 您正在分配内存并返回指针twoArray,该指针收集在localArray 中。因此,您可以使用localArray 访问内存,就像使用twoArray 一样。
    • 如果我理解正确,那么我可以在此行之后添加 free(twoArray):(localArray=randomArrayofStrings();)
    • @RT89 不太可能。看我上面的评论。
    猜你喜欢
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2023-03-18
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多