开发者

Two pointers different information stored

Is it true that having:

int* p = new int;

and:

int* p1开发者_如何学Python = new int[5]();

in case of p1 there will be extra info stored?


Yes, there might be.

I recommend you read the following to items from C++-faq :

  • How does the compiler know there are n objects to be destructed using delete[] ?
  • How do compilers use "over-allocation" to remember the number of elements in an allocated array ?

A relevant quote extracted from the first link :

The run-time system stores the number of objects, n, somewhere where it can be retrieved if you only know the pointer, p. There are two popular techniques that do this. [...]

  • Over-allocate the array and put n just to the left of the first Fred object.
  • Use an associative array with p as the key and n as the value


This C++-FAQ entry should answer your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜