簡體   English   中英

在C中填充動態2D數組時出現分段錯誤

[英]Segmentation Fault when filling a dynamical 2D array in C

我嘗試創建一個2D數組,並嘗試用文件中的整數填充它。 但是,當我運行程序時,根據Valgrind的說法,在這一行出現了Segmentation Fault:

*(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);
}

該代碼有什么問題?

如果我理解正確,那么“分段錯誤”將超出程序分配的內存。 因此,我的問題應該出在我的2D陣列的尺寸上,還是在我嘗試填充它的方式上。

但是,我分配了許多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));
}

當我用2D數組填充

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

它不會出現錯誤,因為我的循環在[1,95] intervall中(這是我的2D數組每列的大小)。

另一種解釋是,根據這個問題 ,我的一個指針懸空了,但是如何呢?

查看以下代碼片段:

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) )首次有效,則使用read_char的值填充array_valid_char所有95個元素。

但是,如果測試條件第二次有效,那么您將執行相同的操作。 但是現在array_valid_char超出了它的范圍。 因為它指向數組的96.元素(不存在)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM