int someInts[3] 和 int* someInts = new int[3] 之间的区别?

发布于 2025-01-07 02:57:06 字数 105 浏览 1 评论 0原文

使用 int someInts[3] 声明新的整数数组与使用 int* someInts = new int[3] 声明新的整数数组有什么区别?

What is the difference between declaring a new integer array by using int someInts[3], versus using int* someInts = new int[3]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

原来是傀儡 2025-01-14 02:57:06

有两个主要区别:

  1. 第一个将在堆栈上分配内存,一旦函数返回,该内存将不可用。
    第二个将在自由存储上分配一块内存,该内存在删除之前一直可用。

  2. 第一个 someInts 是一个整数数组,您不能为其分配新地址。
    第二个是指向 int 的指针,因此您可以为其分配新地址。

There are 2 main differences:

  1. The first will allocate a memory on the stack, that will be unavailable once the function returns.
    The second will allocate a memory on the freestore, which will be available until deleted.

  2. The first someInts is an array of ints, you cannot assign new address to it.
    The second is a pointer to int, so you may assign a new address to it.

独夜无伴 2025-01-14 02:57:06

通常重要的区别(特别是当您处理除 ints 之外的东西时)是当您使用后者时 (int *someints = new int[3];int *someints = new int[3];) 当你使用完数据后,你必须显式地删除它。

大多数时候,您想要使用 std::vector; someints(3); 相反。这(通常)会类似地为数据分配空间,但是当变量超出范围时(包括例如通过抛出异常离开范围,当您手动分配/释放内存时,正确处理要困难得多)。

The difference that's generally important (especially when you're dealing with something other than ints) is that with when you use the latter (int *someints = new int[3];) you have to explicitly delete the data when you're done using it.

Most of the time, you want to use std::vector<int> someints(3); instead. This will (normally) allocate the space for the data similarly, but it'll automatically delete that space when the variable goes out of scope (including, for example, leaving the scope via an exception being thrown, which is much more difficult to handle correctly when you allocate/free the memory manually).

金橙橙 2025-01-14 02:57:06

声明 int* someInts = new int[3] 在堆上分配内存。声明 int someInts[3] 会在堆栈上分配它。

Declaring int* someInts = new int[3] allocates the memory on the heap. Declaring int someInts[3] allocates it on the stack.

黄昏下泛黄的笔记 2025-01-14 02:57:06

当你执行 someInts[3] 时,你在堆栈上分配内存,这意味着它将删除自身(很好),但如果你想在函数结束后访问,你会遇到麻烦,因为它已经被删除了。 IE:

int* returnPointerThingy(){
    int someInts[3];
    someInts[0] = 3;
    someInts[1] = 2;
    someInts[2] = 1;
    return someInts
}

这将返回一个空指针,因为 someInts 已被删除。如果你尝试访问 someInts 中的某些内容,上帝会帮助你。

如果这与您要执行的操作类似,您需要使用 new 关键字。它将允许您在“堆”上分配一些东西,并且它将在声明它的函数结束后继续存在。因此:

int* returnPointerThingy(){
    int* someInts = new int[3];
    someInts[0] = 3;
    someInts[1] = 2;
    someInts[2] = 1;
    return someInts
}

不会返回空指针,您将能够使用 someInts 存储的值。然而,这有一个缺点,您必须像这样删除 someInts:

delete [] someInts

这样,当堆被指针等占用时,您就不会导致内存泄漏。

这取决于您的情况来使用,因为两者在各自的情况下更好。

When you do someInts[3], you allocate memory on the stack, this means that it will delete itself (good) but if you want to access after the function has ended you'll run into trouble as it will already have been deleted. IE:

int* returnPointerThingy(){
    int someInts[3];
    someInts[0] = 3;
    someInts[1] = 2;
    someInts[2] = 1;
    return someInts
}

This will return a null pointer since someInts has been deleted. If you try to access something in someInts god help you.

If this is similar to what you which to do you want to use the new keyword. It will allow you to allocate something on the "Heap" and it will live after the function it was declared in has ended. Thus this:

int* returnPointerThingy(){
    int* someInts = new int[3];
    someInts[0] = 3;
    someInts[1] = 2;
    someInts[2] = 1;
    return someInts
}

Will not return a null pointer and you will be able to use the values stored by someInts. However this comes with a drawback, you must delete someInts like this:

delete [] someInts

So you don't end up with memory leaks, when the heap gets taken up by pointers and such.

It depends on your situation for which to use as both are better in their own situations.

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