<分区>
我想使用 C 函数获取数组并计算可变大小 (@runtime)。我按如下方式实现此功能:
void getList(int **listArray, int *count){
// get the total count
int totalListCount = getTotalListCount();
// Initialize the array
int theList[totalListCount];
memset( theList, 0, sizeof(int)*totalListCount );
// Set the elements in the array
for (int i = 0; i < totalListCount; i++) {
theList[i] = theElementAtIndex(i);
}
// Assign the value to the pointer.
*count = totalListCount;
*listArray = theList;
}
获取数组和计数后,我可以打印值:
int *list;
int count;
getList(&list, &count);
for (int i = 0; i < count; i++) {
printf("list[%d]: %d \n", i, list[i]);
}
执行是否正确?我是否需要管理这些指针的内存,以及如何管理?