簡體   English   中英

在C中的函數中動態分配數組

[英]Dynamically allocating an array in a function in C

我正在編寫的程序有問題。 這個小程序是一個簡短的版本,僅用於顯示問題。

對於此示例,我定義了一個名為point的結構,該結構具有X和Y。我希望該函數計算點的數量。在此示例中,我假設始終為5,但這在我的實際程序中不是恆定的。

#include <stdio.h>

typedef struct  point {
   int x;
   int y;
}point;


// suppose this is dynamic. it return a value according to some parameter;
int howManyPoints() {
   // for this demo assume 5.
   return 5;
}


int createAnArrayOfPoints(point** outArray,int* totalPoints) {

   // how many points?
   int neededPoints = howManyPoints();

   // create an array of pointers
   *outArray =malloc(neededPoints * sizeof(point*));

   // malloc memory of a point size for each pointer
   for (int i=0;i<neededPoints;i++) outArray[i] = malloc(sizeof(point));

   // fill the points with some data for testing
   for (int k=0;k<neededPoints;k++) {
      outArray[k]->x = k*10;
      outArray[k]->y = k*5;
   }

   // tell the caller the size of the array
   *totalPoints = neededPoints;

   return 1;
  }


int main(int argc, const char * argv[]) {

   printf("Program Started\n");

   point* arrayOfPoints;
   int totalPoints;
   createAnArrayOfPoints(&arrayOfPoints,&totalPoints);

   for (int j=0;j<totalPoints;j++) {
    printf("point #%d is at %d,%d\n",j,arrayOfPoints[j].x,arrayOfPoints[j].y);
   }

   printf("Program Ended\n");

   return 0;
}

我的控制台輸出如下所示:

Program Started
point #0 is at 0,0
point #1 is at 0,0
point #2 is at 10,5
point #3 is at 0,0
point #4 is at 20,10
Program Ended

我究竟做錯了什么? 我希望所有5點都具有價值。

謝謝。

您對數組的表示形式不匹配:在您的主機中,您期望的是一個點數組( point* arrayOfPoints; ),它是一個連續的內存。 但是,在createAnArrayOfPoints分配它的方式不同:

在該功能讓你在一塊內存僅攜帶指向arrayOfPoints點point與指針指向的大小的內存初始化point你分配。 這是一種過多的間接訪問,並且在打印時還會在已分配的內存之外進行訪問。

相反,您應該執行以下操作:

// Allocate enough memory to store an array of points of the expected size.
// To keep the code more readable, the resulting pointer is stored in a 
// intermediate variable.
points *theArray = malloc(neededPoints * sizeof(point));
if (theArray == NULL) {
    // do some error handling here as you apparently ran out of memory...
}

// fill the points with some data for testing
for (int k=0;k<neededPoints;k++) {
   theArray[k].x = k*10;
   theArray[k].y = k*5;
}

// Now save the pointer to the output-variable.
*outArray = theArray;

讓我也警告一下:在使用返回值之前,您始終應檢查malloc是否成功。 可能是您內存不足,因此無法獲得您所要求的。

您不需要使用2個malloc。 試試下面的代碼。 您的方法嘗試實際創建點矩陣並初始化每條線的第一個元素。

當您打印時,實際上打印的是尚未完全初始化的第一行。

int createAnArrayOfPoints(point** outArray,int* totalPoints) {

   // how many points?
   int neededPoints = howManyPoints();

   // create an array of pointers
   *outArray =(point*)malloc(neededPoints * sizeof(point));

   // fill the points with some data for testing
   for (int k=0;k<neededPoints;k++) {
      (*outArray)[k].x = k*10;
      (*outArray)[k].y = k*5;
   }

   // tell the caller the size of the array
   *totalPoints = neededPoints;

   return 1;
  }

您應該分配一個點結構數組,而不是一個指向點結構的指針數組。 此外,您正在混合間接級別。

使用局部變量point *array; 保留指向已分配數組的指針並將其作為array[i].x = k*10;...並將其存儲為*outArray = array

暫無
暫無

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

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