簡體   English   中英

在C中寫入char *數組會引發segfault

[英]Writing to char* array in C throws segfault

我是一名信息安全專業的學生,​​剛剛開始學習C。我被分配了一個使用此原型編寫簡單翻譯程序的程序: char* dictionary[number_of_words][2];

輸出應如下所示:

Enter # of words to add: 3

dictionary[0][0]= plane
dictionary[0][1]= Flugzeug
dictionary[1][0]= house
dictionary[1][1]= Haus
dictionary[2][0]= cat
dictionary[2][1]= Katze

Enter english word to translate: house
---> Haus

我當前的代碼(正在進行中)如下所示:

void clean_stdin(void);
int main(void)
{
     unsigned int i = 0,j = 0,size; 
     printf("Enter # of words to add: ");
     scanf("%u",&size);
     clean_stdin(); //clears input buffer

     char* dictionary[size][2];

     while(i <= size) 
     {   
         printf("dictionary[%u][%u]= ",i,j);
         fgets(dictionary[i][j],100,stdin);

         if(j >= 1)
         {
             j = 0;
             ++i; 
         }
         else
             j = 1;
         }   

     return 0;
 }

使用gcc -Wall main.c編譯時,代碼不會引發任何錯誤,但這是我得到的行為:

Enter # of words to add: 3
dictionary[0][0]= plane
dictionary[0][1]= Flugzeug
dictionary[1][0]= house
dictionary[1][1]= Haus
fish: “./a.out” terminated by signal SIGSEGV (Adressbereichsfehler)

*****

Enter # of words to add: 4
dictionary[0][0]= plane
fish: “./a.out” terminated by signal SIGSEGV (Adressbereichsfehler)

我的思考過程中存在一個基本缺陷。 任何幫助/注意都非常感謝。 干杯!

讓我們看一下代碼的這一部分:

char* dictionary[size][2];

 while(i <= size) 
 {   
     printf("dictionary[%u][%u]= ",i,j);
     fgets(dictionary[i][j],100,stdin);

每個dictionary[i][j]都是指向char的指針,但是您尚未將它們設置為指向有意義的任何地方-每個數組元素都包含一些隨機位模式,該模式可能對應也可能不對應可寫地址。 您的前幾個條目會寫到某個地方 ,但最終您嘗試寫到一個您不擁有或無法訪問的存儲位置。

您將需要預留額外的內存來存儲每個單獨的字符串。 您要么需要創建一個char的3D數組(不是char * ):

#define MAX_STR_LEN 100
...
char dictionary[size][2][MAX_STR_LEN+1]; 

否則您將需要為每個數組條目動態分配內存:

while ( i < size )  // <, not <=
{
  dictionary[i][j] = malloc( sizeof *dictionary[i][j] * (MAX_STR_LEN + 1));
  if ( !dictionary[i][j] )
  {
    // memory allocation failed, handle error
  }
  printf("dictionary[%u][%u]= ",i,j);
  fgets(dictionary[i][j],MAX_STR_LEN,stdin);

如果動態分配內存,則需要在完成后顯式free內存:

for ( i = 0; i < size; i++ )
{
  free( dictionary[i][0] );
  free( dictionary[i][1] );
}

建議您不要使用多個維度的數組,而建議使用結構:

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

int main()
{
    unsigned int size;
    printf("Enter # of words to add: ");
    if (scanf("%u", &size) != 1)
        return 1; // cannot convert input to integer

#define WORD_MAX_SIZE 100

    struct {
        char word[WORD_MAX_SIZE];
        char translation[WORD_MAX_SIZE];
    } dictionary[size];

    memset(dictionary, 0, sizeof(dictionary));

    for (int i = 0; i < size; i++) {
        printf("dictionary[%u].word= ", i);
        fgets(dictionary[i].word, WORD_MAX_SIZE, stdin);
        printf("dictionary[%u].translation= ", i);
        fgets(dictionary[i].translation, WORD_MAX_SIZE, stdin);
    }

    // do something with your stuff
}

fgets(dictionary[i][j],100,stdin); 從未初始化的變量( dictionary[i][j]從未提供值)中讀取,該變量具有未定義的行為。

另外, i以后會越界。

暫無
暫無

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

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