我可以增加静态分配数组的大小吗?
我知道可以增加动态分配数组的大小。
但是我可以增加静态分配数组的大小吗? 如果是,怎么办?
编辑:虽然这个问题是针对C语言的,但也可以考虑其他语言。在任何其他语言中都可能吗?
I know its possible to increase the size of a dynamically allocated array.
But can I increase the size of a statically allocated array?
If yes,how?
EDIT: Though this question is intended for C language, consider other languages too.Is it possible in any other language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
简单的答案是否定的,这是不可能做到的。因此得名“静态”。
现在,许多语言都有看起来像静态分配数组的东西,但实际上是对动态分配数组的静态分配引用。那些你可以调整大小的。
Simple answer is no, this cannot be done. Hence the name "static".
Now, lots of languages have things that look like statically allocated arrays but are actually statically allocated references to a dynamically allocated array. Those you could resize.
在 VB .NET 中,它会是:
虽然不确定您要使用什么语言...
而且我不会经常使用这个命令...它的效率非常低。链表和不断增长的数据结构效率更高。
in VB .NET it would be:
not sure what langauge you're after though...
And I wouldn't use this command a lot... its terribly inefficient. Linked lists and growing data structures are much more efficient.
不,事实并非如此。这里有两个选项:
,那就是 C。
No. It is not. There are two options here:
Yes, that was C.
如果您小心的话,可以使用
alloca()
。该数组是在堆栈上分配的,但就代码风格而言,它很像您使用malloc
(不过您不必free
它,这样就完成了)自动地)。我会让您决定是否将其称为“静态”数组。If you're careful, you can use
alloca()
. The array is allocated on the stack, but in terms of the code style it's a lot like if you usedmalloc
(you don't have tofree
it though, that's done automatically). I'll let you decide whether to call that a "static" array.不会。静态分配允许编译器做出各种假设,然后在编译期间将其融入到程序中。
这些假设包括:
(好吧,如果程序存储在内存中,则可以更新引用,但自修改程序是非常不受欢迎的,而且肯定比动态数组更麻烦。)
No. Static allocation gives the compiler permission to make all kinds of assumptions which are then baked into the program during compilation.
Among those assumptions are that:
(Well, references could be updated, if the program was stored in ram, but self-modifying programs are highly frowned upon, and surely more trouble than dynamic arrays.)
从技术上讲,在 C 中甚至不可能增加动态分配数组的大小。
事实上,realloc() 执行某种“创建新对象并复制数据”例程。它根本不会修改现有堆内存对象的大小。
所以答案很简单,任何对象或对象数组在分配后都无法更改其大小,无论是动态还是静态分配。
您可以做的是使用相同的策略,通过开发一个函数来创建另一个具有所需大小的静态分配的对象数组并复制数据。如果新的对象数组小于旧的对象数组,则差异内的值将被丢弃。
唯一的区别是,新数组的大小相当于旧数组的大小,需要在编译时固定。
Technically, in C it isn´t even possible to increase the size of a dynamically allocated array.
In fact,
realloc()
does some kind of "create new object & copy the data" routine. It does not modify the size of an existant heap-memory object at all.So the answer is simple as that, that you are not be able to change the size of any object or array of objects after it has been allocated, neither if it was dynamically or statically allocated.
What you can do is to use the same strategy by developing a function which is creating another static allocated array of objects with the desired size and copy the data. If the new array of objects is smaller than the old one, the values inside the difference are discarded.
The only difference is, that the size of the new array, equivalent to the size of the old array, need to be fixed at compile-time.