簡體   English   中英

動態分配數組的C問題

[英]C issue with dynamically allocating array

我程序的輸入文件的第一行包含一個int(稱為N),該int表示將有多少個后續整數(每個新行中的整數)。 然后它將整數讀入num_array並打印出來。 我的問題是num_array分配不正確。 無論N是多少,代碼中的調試語句都會輸出8,即sizeof(num_array)。

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

int *num_array;

int main (int argc, char**argv){
    int numThreads = (int) argv[1];
    char *inputFile = argv[2];
    int N = 0;

    char line[20];
    FILE *file = fopen(inputFile, "r");

    int fileCounter = 0;

    while(fgets(line, sizeof(line), file)) {
        if (fileCounter==0){
            N = atoi(line);
            num_array = malloc(sizeof(int)*(N+1));
        }
        else{
            num_array[fileCounter-1] = atoi(line);
        }
        fileCounter++;
    }
    fclose(file);
    int i;

    printf("%d", sizeof(num_array));
    printf("\n");
    printf("\n");
    for(i = 0; i<sizeof(num_array); i++){
        printf("%d\n", num_array[i]);
    }
    return 0;
}

輸入文件示例:

9
10
9
3
212
56
99
4
5
6

將打印出:

8  
10
9
3
212
56
99
4
5

如您所見,數組的最后一個元素被切除(不打印6),並且num_array的大小似乎不正確(應包含N個整數,在輸入文件的第一行中N為int)

您的程序存在許多問題:

  1. 您的main()函數的第一行有一個非常嚴重的錯誤

     int numThreads = (int) argv[1] 

    在c強制轉換中不會轉換類型,這種轉換當然是可能的,但是沒有給出您期望的結果,您需要這樣的東西

     char *endptr; int numThreads = strtol(argv[1], &endptr, 10); if (*endptr != '\\0') { printf("`%s' cannot be converted to an integer\\n", argv[1]); return -1; } 
  2. 您不確定要向程序的命令行提供任何參數,需要檢查一下, argc包含傳遞給程序的命令行參數數量+ argv[0] ,因此必須檢查

     if (argc < 2) { printf("Use: %s NumberOfThreads, where NumberOfThreads is `int'\\n", argv[0]); return -1; } 
  3. 您無需檢查fopen()返回NULL ,這將在fgets() file指針時引起更多問題。

  4. sizeof運算符不給出數組的長度,它給出該數組占用的字節數,並且您的變量不是數組,而是指針,因此在這種情況下, sizeof運算符給出的是指針的大小。

    事實證明,文件包含9值,並在平台的指針大小是8 ,所以sizeof(num_array)8 ,其是9 - 1因此你丟失一個值,則已經有該陣列的元件的數量N ,所以用吧

  5. 您永遠不會調用free()

這是您代碼的一個版本,已經修復,並且更加安全

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

int *num_array;

int main (int argc, char**argv) 
 {
    char  line[20];
    /*int   numThreads  = 1;*/
    char *inputFile   = "data.dat";
    int   N           = 0;
    int   fileCounter = 0;
    int   i           = 0;
    FILE *file = fopen(inputFile, "r");
    if (file == NULL)
        return -1;
    while (fgets(line, sizeof(line), file) != NULL)
     {
        if (fileCounter == 0)
         {
            N         = atoi(line);
            num_array = malloc((1 + N) * sizeof(int));
            if (num_array == NULL)
             {
                fclose(file);
                return -1;
             }
         }
        else
         {
            num_array[fileCounter - 1] = atoi(line);
         }
        fileCounter++;
     }
    fclose(file);

    printf("%ld", sizeof(num_array));
    printf("\n");
    printf("\n");
    for (i = 0 ; i < N ; i++)
     {
        printf("%d\n", num_array[i]);
     }
    free(num_array);
    return 0;
 }

暫無
暫無

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

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