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

发布于 2024-08-10 12:22:04 字数 128 浏览 2 评论 0 原文

我知道可以增加动态分配数组的大小。

但是我可以增加静态分配数组的大小吗? 如果是,怎么办?

编辑:虽然这个问题是针对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

尐偏执 2024-08-17 12:22:04

简单的答案是否定的,这是不可能做到的。因此得名“静态”。

现在,许多语言都有看起来像静态分配数组的东西,但实际上是对动态分配数组的静态分配引用。那些你可以调整大小的。

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.

绳情 2024-08-17 12:22:04

在 VB .NET 中,它会是:

Redim Preserve ArrayName(NewSize)

虽然不确定您要使用什么语言...

而且我不会经常使用这个命令...它的效率非常低。链表和不断增长的数据结构效率更高。

in VB .NET it would be:

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.

情定在深秋 2024-08-17 12:22:04

不,事实并非如此。这里有两个选项:

  1. 使用动态选项
  2. 或者,冒着浪费内存的风险,如果您知道数组将存储的最大元素数,则相应地静态分配 是的

,那就是 C。

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.

灵芸 2024-08-17 12:22:04

如果您小心的话,可以使用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 used malloc (you don't have to free it though, that's done automatically). I'll let you decide whether to call that a "static" array.

栖竹 2024-08-17 12:22:04

不会。静态分配允许编译器做出各种假设,然后在编译期间将其融入到程序中。

这些假设包括:

  1. 将其他数据紧接在数组后面放置是安全的(不会留下增长空间),并且
  2. 数组从某个地址开始,然后该地址将成为程序机器代码的一部分;您无法在某处分配新数组(并使用它),因为对地址的引用无法更新。

(好吧,如果程序存储在内存中,则可以更新引用,但自修改程序是非常不受欢迎的,而且肯定比动态数组更麻烦。)

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.)

夜血缘 2024-08-17 12:22:04

从技术上讲,在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文