有没有办法区分 new 和 new[] 的返回值?

发布于 2024-11-12 05:25:53 字数 534 浏览 2 评论 0原文

考虑这段代码:

int *p = new int;
cout << sizeof(*p);
delete p;

正如预期的那样,结果是 4。现在,考虑一下其他代码:

int *p = new int[10];
cout << sizeof(*p);
delete[] p;

我期望得到 40(分配的数组的大小),但是结果仍然是 4

现在,假设我有一个函数 int *foo(),它返回一个指向使用 new 或使用 new[] 创建的结构的指针(但是我不知道是哪一个):

int *p = foo();

我的问题是,有没有办法(或黑客)知道 p 是否指向单个整数或整数数组?

请记住,这只是一个理论问题。我不会以这种方式编写真正的代码。

Consider this code:

int *p = new int;
cout << sizeof(*p);
delete p;

As expected the result is 4. Now, consider this other code:

int *p = new int[10];
cout << sizeof(*p);
delete[] p;

I expected to get 40 (the size of the allocated array), however the result is still 4.

Now, suppose I have a function int *foo() that returns a pointer to a structure created with new or with new[] (but I don't know which one):

int *p = foo();

My question is, is there a way (or hack) to know if p points to a single integer or an array of integers?

Please keep in mind that this is just a theoretical question. I won't be writing real code in this fashion.

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

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

发布评论

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

评论(7

晨曦慕雪 2024-11-19 05:25:53

不,没有办法做到这一点。但是知道其中的区别,因为您编写的代码称为newnew[]

顺便说一下,

 cout << sizeof(*p);

在这两种情况下都给你 4 的原因是因为 p 是一个指向 int 的指针,表达式 *p 表示这样一个指针所指向的东西(即 int)以及你的平台上 int 的大小是 4。这都是在编译时计算的,因此即使 new[] 确实返回了一个特殊值,sizeof 也无法使用它。

No, there is no way of doing that. But you know the difference, because the code you wrote called new or new[].

The reason by the way that:

 cout << sizeof(*p);

gives you 4 in both cases is because p is a pointer to an int, the expression *p means the thing pointed to by such a pointer (i.e. an int) and the size of an int on your platform is 4. This is all evaluated at compile time, so even if new[] did return a special value, sizeof would not be able to use it.

三人与歌 2024-11-19 05:25:53

不,因为你的结果是一个地址(这就是为什么在这两种情况下 sizeof() 都会得到 4 的原因)。你创建了它,所以你应该知道它是什么。

No, because your result is an address (that's why you get 4 for sizeof() in both cases). You created it, so you're expected to know what it is.

万人眼中万个我 2024-11-19 05:25:53

在这两个示例中,p 的类型相同:int *。 sizeof 对类型进行操作,而不是对数据进行操作。它是在编译时计算的。

你有几个选择。您可以自己跟踪数组大小,也可以冒险使用标准库中的容器之一,例如 vector<整数>。这些容器将为您跟踪大小(例如 vector::size())。

In both examples the type of p is the same: int *. sizeof operates on the type, not the data. It's computed at compile time.

You have a couple of choices. You can keep track of the array size yourself, or you can venture into using one of the containers in the standard library such as vector< int >. These containers will track the size (e.g. vector< int >::size()) for you.

坦然微笑 2024-11-19 05:25:53

sizeof(x) 返回包含声明的 x 所需的内存量。

这根本没有动态的方面。

sizeof (*foo),其中 foo 是一个 bar * 将始终与 sizeof(bar) 相同

sizeof(x) returns the amount of memory needed to contain x as declared.

There is no dynamic aspect to this at all.

sizeof (*foo) where foo is a bar * will always be the same as sizeof(bar)

面犯桃花 2024-11-19 05:25:53

不,没有什么办法。

强制性问题:你为什么需要知道?

如果是“因为我需要知道是说 delete [] 还是 delete”,那么就一直使用数组,如果出于某种晦涩的原因你无法弄清楚看看你在自己的代码中使用了哪一个。

No, there isn't any way.

Obligatory question: Why do you need to know?

If it's "because I need to know whether to say delete [] or delete", then just use arrays all the time, if for some obscure reason you can't figure out which one you used in your own code.

和我恋爱吧 2024-11-19 05:25:53

拥有一个可以返回指向单个项目或数组的指针的函数是一个糟糕的设计决策。您始终可以返回一个指向大小为 1 的数组的指针:

return new int[1];

Having a function that can return a pointer to a single item or an array is a bad design decision. You can always return a pointer to an array of size 1:

return new int[1];
小帐篷 2024-11-19 05:25:53

首先,sizeof(*p) 始终返回整数值,因此它始终返回4

现在,您如何知道 p 是否指向 int 还是 int[]

没有标准方法。但是,您可以破解该平台并使其为人所知。例如,如果您尝试为某些编译器打印 p[-1], p[-2], ..., p[-4] 等(在我的例子中是 linux),那么您将查看该位置的值的特定模式。然而,这只是一个 hack,你不能总是依赖它。

First, sizeof(*p) returns always a value to the integer, so it's always returning 4.

Now, how can you know whether p is pointing to int or int[] ?

There is no standard way of it. However, you can hack the platform and get it known. For example, if you try printing p[-1], p[-2], ..., p[-4] etc. for certain compilers (say linux in my case) then you will see a particular pattern in the value of this locations. However, this is just a hack and you cannot rely upon it always.

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