传递一个动态数组中的功能用C [英] Passing a dynamic array in to functions in C
问题描述
我试图创建一个函数,它接受一个数组作为参数,增加值,它(增加它的大小,如果需要的话),并返回的项目数。
到目前为止,我有:
I'm trying to create a function which takes an array as an argument, adds values to it (increasing its size if necessary) and returns the count of items. So far I have:
int main(int argc, char** argv) {
int mSize = 10;
ent a[mSize];
int n;
n = addValues(a,mSize);
for(i=0;i<n;i++) {
//Print values from a
}
}
int addValues(ent *a, int mSize) {
int size = mSize;
i = 0;
while(....) { //Loop to add items to array
if(i>=size-1) {
size = size*2;
a = realloc(a, (size)*sizeof(ent));
}
//Add to array
i++;
}
return i;
}
这工作,如果是MSIZE大到足以容纳阵列的所有潜在要素,但如果需要调整,我得到一个分段错误。
This works if mSize is large enough to hold all the potential elements of the array, but if it needs resizing, I get a Segmentation Fault.
我也尝试:
int main(int argc, char** argv) {
...
ent *a;
...
}
int addValues(ent *a, int mSize) {
...
a = calloc(1, sizeof(ent);
//usual loop
...
}
要无果。
我想这是因为当我打电话的realloc的副本'一'在别处指出 - ?怎么可能修改该让'A'永远指向同一个位置
I assume this is because when I call realloc, the copy of 'a' is pointed elsewhere - how is it possible to modify this so that 'a' always points to the same location?
我要对这个正确?有没有更好的方式来处理在C动态结构?我应该实现一个链表来处理这些?
Am I going about this correctly? Are there better ways to deal with dynamic structures in C? Should I be implementing a linked list to deal with these?
推荐答案
这里的主要问题是,你正在尝试使用realloc的一个堆栈分配的数组。您有:
The main problem here is that you're trying to use realloc with a stack-allocated array. You have:
ent a[mSize];
这是在栈上自动分配。如果你想在这以后使用的realloc(),您将创建在使用堆的malloc(数组),就像这样:
That's automatic allocation on the stack. If you wanted to use realloc() on this later, you would create the array on the heap using malloc(), like this:
ent *a = (ent*)malloc(mSize * sizeof(ent));
这样的库的malloc(从而realloc的()等)知道你的数组。从这个外观上来看,你可能会产生混淆 C99变长数组与真正的动态数组的,所以一定要试图解决这一问题前,理解上的差异在那里。
So that the malloc library (and thus realloc(), etc.) knows about your array. From the looks of this, you may be confusing C99 variable-length arrays with true dynamic arrays, so be sure you understand the difference there before trying to fix this.
真的,不过,如果你用C编写动态数组,你应该尝试使用OOP十岁上下的设计,封装有关阵列的信息,并从用户隐藏。你想了解你的阵列信息(例如指针和大小)整合到一个结构和操作(例如分配,添加元素,删除元素,释放等)到符合结构工作的特殊功能。所以,你可能有:
Really, though, if you are writing dynamic arrays in C, you should try to use OOP-ish design to encapsulate information about your arrays and hide it from the user. You want to consolidate information (e.g. pointer and size) about your array into a struct and operations (e.g. allocation, adding elements, removing elements, freeing, etc.) into special functions that work with your struct. So you might have:
typedef struct dynarray {
elt *data;
int size;
} dynarray;
和您可以定义某些功能dynarrays工作:
And you might define some functions to work with dynarrays:
// malloc a dynarray and its data and returns a pointer to the dynarray
dynarray *dynarray_create();
// add an element to dynarray and adjust its size if necessary
void dynarray_add_elt(dynarray *arr, elt value);
// return a particular element in the dynarray
elt dynarray_get_elt(dynarray *arr, int index);
// free the dynarray and its data.
void dynarray_free(dynarray *arr);
这样,用户不必记得到底如何分配的事物或什么大小的阵列当前。希望得到您开始。
This way the user doesn't have to remember exactly how to allocate things or what size the array is currently. Hope that gets you started.
这篇关于传递一个动态数组中的功能用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!