简体   繁体   English

如何在 function - ANSI C 中使用本地数组?

[英]How use local array in function - ANSI C?

I want to add something to the end of the array passed to the function.我想在传递给 function 的数组末尾添加一些内容。
Which is better, declaring a new larger array or using alloc ()?声明一个新的更大的数组还是使用 alloc() 哪个更好?
1. 1.

void array_append(int *block, size_t size)
{
   int new_block[size + 2];
   memcpy(new_block, block, size);
   (...append)

}
void array_append(int *block, size_t size)
{
   int *new_block = calloc(1, sizeof(int) + 2);
   memcpy(new_block, block, size);

   (...append)

   free(new_block);
}

I am not returning the newly created array anywhere.我不会在任何地方返回新创建的数组。

I only use new_block inside functions.我只在函数内部使用 new_block。
Does not modify the original array in the function.不修改 function 中的原始数组。
Declaring new_block as static is omitted.将 new_block 声明为 static 被省略。
I know how calloc() / malloc() works, I know that this operation has to be validated.我知道 calloc() / malloc() 是如何工作的,我知道这个操作必须经过验证。
new_block is only meant to live in a function. new_block 仅适用于 function。
I just wonder which solution is better and why...我只是想知道哪种解决方案更好,为什么...

regards问候

You should dynamically allocate an array instead of using a variable length array because in general in the last case the code can be unsafe due to a comparatively big size of the array that can lead to the stack overflow.您应该动态分配数组而不是使用可变长度数组,因为通常在最后一种情况下,由于数组的相对较大会导致堆栈溢出,因此代码可能不安全。

I want to add something to the end of the array我想在数组的末尾添加一些东西

But you cannot really.但你不能真的。 Unless with realloc() .除非使用realloc() This is how your ...append trick can be done, whatever it means.这就是您的...append技巧可以完成的方式,无论它意味着什么。

If you need a temporary array to work with and then copy into your array (but not at the end,).如果您需要一个临时数组来使用,然后复制到您的数组中(但不是在最后)。 then all methods for allocation are allowed - it really depends on how often and with which sizes.然后允许所有分配方法 - 这实际上取决于频率和大小。

If it is called very often with limited sizes, it could be a static array.如果它经常以有限的大小被调用,它可能是一个static数组。

There is no easy solution for growing arrays (or for memory management in general).增长 arrays(或一般的 memory 管理)没有简单的解决方案。 At the extreme you allocate every element individually and link them together: a linked list.在极端情况下,您单独分配每个元素并将它们链接在一起:链表。

--> avoid reaching the end of your arrays. --> 避免到达 arrays 的末尾。 Define a higher maximum or then implement a linked list.定义一个更高的最大值或然后实现一个链表。

In certain situations realloc() also makes sense (big changes in size, but not often).在某些情况下realloc()也有意义(大小变化很大,但不经常)。 Problem is sometimes the whole array has to be memcopied to keep the larger array contiguous: "realloc", not "append".问题是有时必须对整个数组进行内存复制以保持较大的数组连续:“realloc”,而不是“append”。 So it is "expensive".所以它是“昂贵的”。

I am not returning the newly created array anywhere.我不会在任何地方返回新创建的数组。

That is part of the problem.这是问题的一部分。 You actually seem to be doing half of what realloc() does: allocate the new space, memcpy() the old contents...and then free the old and return the new array(-pointer) to the caller.实际上,您似乎只做了 realloc() 的一半:分配新空间,memcpy() 旧内容......然后释放旧内容并将新数组(-指针)返回给调用者。

First version can not return the array pointer, because end of function is also end of local auto arrays, VLA or not.第一个版本不能返回数组指针,因为 function 的结尾也是本地自动 arrays 的结尾,VLA 与否。

If the append can be done to the existing array (which it can if the caller expects this and the memory of the array has room), you can merely append to the existing array.如果 append 可以对现有数组进行(如果调用者期望这样做并且该数组的 memory 有空间),则可以仅对现有数组进行 append。

Otherwise, you need a new array.否则,您需要一个新数组。 In this case, the array must be returned to the caller.在这种情况下,数组必须返回给调用者。 You can do this by returning a pointer to its first element or by having the caller pass a pointer to a pointer, and you modify the pointed-to pointer to point to the first element of the new array.您可以通过返回指向其第一个元素的指针或让调用者传递一个指向指针的指针来完成此操作,然后修改指向的指针以指向新数组的第一个元素。

When you provide a new array, you must allocate memory for it with malloc or a similar routine.提供新数组时,必须使用malloc或类似例程为其分配 memory。 You should not use an array defined inside your function without static , as the memory for such an array is reserved only until execution of the function ends. You should not use an array defined inside your function without static , as the memory for such an array is reserved only until execution of the function ends. When your function returns to the caller, that memory is released for other uses.当您的 function 返回给调用者时,该 memory 将被释放用于其他用途。 (Generally, you also should not use an array declared with static , but for reasons involving good design, reducing bugs, and multiple serial or parallel calls to the function.) (通常,您也不应该使用用static声明的数组,但出于良好设计、减少错误以及对 function 的多次串行或并行调用等原因。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM