在C中的函数中动态分配数组

时间:2015-02-22 11:08:29

标签: 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分都有值。

感谢。

3 个答案:

答案 0 :(得分:3)

您对阵列的表示形式不匹配: 在你的主要部分,你期待一个连续的记忆点数组(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是否成功。可能是你的内存不足,因而无法获得你所要求的内容。

答案 1 :(得分:3)

您不需要使用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;
  }

答案 2 :(得分:0)

您应该分配一个点结构数组,而不是指向点结构的指针数组。此外,您正在混合间接级别。

使用局部变量point *array;来保存指向已分配数组的指针并将其作为array[i].x = k*10;...访问,并将此指针存储为*outArray = array