c - 在 C 中填充动态二维数组时出现段错误

标签 c multidimensional-array segmentation-fault malloc

我尝试创建一个二维数组并尝试用文件中的整数填充它。 但是当我运行我的程序时,根据 Valgrind,我在这一行遇到了一个段错误:

*(array_valid_char++) = read_char;

这是我的代码:

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

int* f(char* name_file_in, int key_length)
{
    FILE *file_in;
    int* array_valid_char = malloc(95 * sizeof(int));//array of validated characters
    int count_char = 0;//count number of characters in file_in
    int read_char;//character being read

    file_in = fopen(name_file_in,"rb");

    if(file_in)
    {
        while ((read_char = fgetc(file_in)) != EOF)//browse the entire file
        {
            if ((count_char % key_length) == 0)//test if character's position in file belongs to the modulo of the key
            {
                for (int i = 0; i < 95; i++)//browse the entire array tabLatin
                {
                    *(array_valid_char++) = read_char;//read character put into array of validated characters
                }
            }
            count_char++;
        }
    }

    if(file_in){fclose(file_in);}

    return array_valid_char;
}

int** C1(char* name_file_in, int key_length)
{
    int **array_valid_keys = malloc(key_length * sizeof(int*));//array of array filled with all valid characters for a character of the key
    for (int i = 0; i < key_length; i++)
    {
        array_valid_keys[i] = malloc(95 * sizeof(int));
    }
    for (int i = 0; i < key_length; i++)
    {
        array_valid_keys[i] = f(name_file_in,i + 1);//for each character'position of the key is assigned an array given by f
    }
    return array_valid_keys;
}


int main(int argc,char* argv[])
{
    int key_length = atoi(argv[2]);
    int** array_valid_key = C1(argv[1], key_length);
    for(int i = 0; i < key_length; i++)
    {
        for(int j = 0; j < 95; j++)
        {
            printf("[%d] ",array_valid_key[i][j]); //printing all valid characters for each character of the key
        }
        printf("\n");
    }
    return(0);
}

这段代码有什么问题?

如果我没理解错的话,Segmentation Fault 是一个超出范围的 程序分配的内存。所以我的问题应该是大小 我的二维数组或我如何尝试填充它。

但是,我分配了一些像这样的 key_length 列:

int **array_valid_keys = malloc(key_length * sizeof(int*));

并且,我已将每一列分配为一个包含 95 个案例的数组:

for (int i = 0; i < key_length; i++)
{
    array_valid_keys[i] = malloc(95 * sizeof(int));
}

当我填充这个二维数组时

for (int i = 0; i < 95; i++)//browse the entire array tabLatin
{
    *(array_valid_char++) = read_char;
}

它不应该出错,因为我的循环在 [1, 95] 区间 (这是我的二维数组中每一列的大小)。

另一种解释是,根据这个 question,我的一个指针悬空了但是如何呢?

最佳答案

查看以下代码片段:

while ((read_char = fgetc(file_in)) != EOF)//browse the entire file
{
    if ((count_char % key_length) == 0)//test if character's position in file belongs to the modulo of the key
    {
        for (int i = 0; i < 95; i++)//browse the entire array tabLatin
        {
            *(array_valid_char++) = read_char;//read character put into array of validated characters
        }
    }
    count_char++;
}

外层循环(while 循环)用于读取单个字符,直到到达末尾。现在在内部循环(for 循环)中,您从 0 数到 94(如果 i 达到 95,则循环结束)。在该循环中,您正在递增 array_valid_char 的指针。

如果测试条件 (((count_char % key_length) == 0)) 第一次有效,您将用值填充 array_valid_char 的所有 95 个元素的 read_char

但是如果测试条件第二次有效,你就是在做同样的事情。但是现在 array_valid_char 超出了它的范围。因为它指向数组的第 96 个元素(不存在)。

关于c - 在 C 中填充动态二维数组时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48130587/

相关文章:

c++ - 对于基于指针和数组的字符串初始化,strlen 和 sizeof 的不同答案

c++ - opengl中glSelectBuffer的返回值

c++ - 代码执行 : How to create a multiple dimensional array manually?

c++ - 如何在类构造函数中初始化动态分配的数组

c - 使用 perf 或其他方式获取 C 程序的运行时间(或其他统计信息)

java - 逐列打印二维数组

matlab - 体素邻域索引 - 使用线性索引在 26 个邻域访问中检测 "out of bounds"

c - 高斯消除期间段错误(核心转储)

c - 段错误,大数组

c - 无法理解函数中宏定义的范围