简体   繁体   English

我可以增加静态分配数组的大小吗?

[英]Can I increase the size of a statically allocated array?

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?编辑:虽然这个问题是针对 C 语言的,但也可以考虑其他语言。是否可以用其他语言?

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.那些你可以调整大小的。

in VB .NET it would be:在 VB .NET 中,它将是:

Redim Preserve ArrayName(NewSize)

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.链表和不断增长的数据结构效率更高。

No. It is not.不它不是。 There are two options here:这里有两个选项:

  1. Use a dynamic one使用动态的
  2. Or,at the risk of wasting memory, if you have an idea about the maximum number of elements that the array will store, statically allocate accordingly或者,冒着浪费内存的风险,如果您知道数组将存储的最大元素数,请相应地静态分配

Yes, that was C.是的,那是C。

If you're careful, you can use alloca() .如果你很小心,你可以使用alloca() The array is allocated on the stack, but in terms of the code style it's a lot like if you used malloc (you don't have to free it though, that's done automatically).该数组是在堆栈上分配的,但就代码风格而言,它与您使用malloc非常相似(尽管您不必free它,这是自动完成的)。 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:这些假设包括:

  1. it is safe to put other data immediately after the array (not leaving you room to grow), and将其他数据立即放在数组之后是安全的(不给您留下增长空间),并且
  2. that the array starts at a certain address, which then becomes part of the machine code of the program;数组从某个地址开始,然后成为程序机器代码的一部分; you can't allocate a new array somewhere (and use it) because the references to the address can't be updated.您无法在某处分配新数组(并使用它),因为无法更新对地址的引用。

(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.) (好吧,如果程序存储在 ram 中,引用可以更新,但自修改程序非常不受欢迎,而且肯定比动态数组更麻烦。)

Technically, in C it isn´t even possible to increase the size of a dynamically allocated array.从技术上讲,在 C 中甚至不可能增加动态分配数组的大小。

In fact, realloc() does some kind of "create new object & copy the data" routine.实际上, realloc()执行某种“创建新对象并复制数据”例程。 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.唯一的区别是,新数组的大小与旧数组的大小相等,需要在编译时固定。

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

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