有没有什么办法来确定C ++编程数组的大小?如果不是,为什么? [英] Is there any way to determine the size of a C++ array programmatically? And if not, why?

查看:123
本文介绍了有没有什么办法来确定C ++编程数组的大小?如果不是,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题的灵感来自于一个类似的问题:<一href=\"http://stackoverflow.com/questions/197675/how-does-delete-know-the-size-of-the-operand-array\">How并删除[]知道的操作数组的大小?

This question was inspired by a similar question: How does delete[] "know" the size of the operand array?

我的问题是有一点不同:有什么方法来确定C ++编程数组的大小?如果不是,为什么?每次我看到函数,它接受一个数组也需要一个整数参数给它的大小。但作为链接的提问时指出,删除[] 必须知道要被释放的内存的大小。

My question is a little different: Is there any way to determine the size of a C++ array programmatically? And if not, why? Every function I've seen that takes an array also requires an integer parameter to give it the size. But as the linked question pointed out, delete[] must know the size of the memory to be deallocated.

考虑这个C ++ code:

Consider this C++ code:

int* arr = new int[256];
printf("Size of arr: %d\n", sizeof(arr));

这版画尺寸改编的:4 ,这是该指针的尺寸。这将是不错的一些功能,打印256,但我不认为用C存在一个++。 (同样,问题的一部分,就是为什么它不存在。)

This prints "Size of arr: 4", which is just the size of the pointer. It would be nice to have some function which prints 256, but I don't think one exists in C++. (Again, part of the question is why it doesn't exist.)

澄清:我知道,如果我宣布栈,而不是堆中的数组(即 INT ARR [256]; )的的sizeof 运算符将返回1024(数组长度*的sizeof(int)的)。

Clarification: I know that if I declared the array on the stack instead of the heap (i.e. "int arr[256];") that the sizeof operator would return 1024 (array length * sizeof(int)).

推荐答案

删除[] 不知道是分配的大小。然而,这些知识驻留在运行时或在操作系统的存储器管理器,这意味着它不提供给在编译期间,编译器。和的sizeof()是不是一个真正的函数,它实际上是由编译器,这是一件好事它不能用于动态分配的数组做的,其大小计算为一个常数是未知在编译过程中。

delete [] does know the size that was allocated. However, that knowledge resides in the runtime or in the operating system's memory manager, meaning that it is not available to the compiler during compilation. And sizeof() is not a real function, it is actually evaluated to a constant by the compiler, which is something it cannot do for dynamically allocated arrays, whose size is not known during compilation.

另外,还要考虑这个例子:

Also, consider this example:


int *arr = new int[256];
int *p = &arr[100];
printf("Size: %d\n", sizeof(p));

如何将编译器知道是什么 P 的大小?这个问题的根源是,在C和C ++数组不是第一类对象。他们腐烂的指针,有没有办法让编译器或程序本身知道是否一个指针指向由,或分配的内存块的开始单个对象,或在内存分配的一大块中间一些地方通过

How would the compiler know what the size of p is? The root of the problem is that arrays in C and C++ are not first-class objects. They decay to pointers, and there is no way for the compiler or the program itself to know whether a pointer points to the beginning of a chunk of memory allocated by new, or to a single object, or to some place in the middle of a chunk of memory allocated by new.

这种情况的一个原因是,C和C ++离开存储器管理向程序员和操作系统,这也是为什么他们没有垃圾收集。实施删除不是C ++标准的一部分,因为C ++,就是要在各种使用平台上,这可能是非常不同的方式管理他们的记忆。这可能让C ++保留所有的分配数组的轨道,它们的大小,如果你正在写最新的英特尔CPU上运行Windows中一个字处理器,但是当你正在编写运行在嵌入式系统中,可能是完全不可行一个DSP。

One reason for this is that C and C++ leave memory management to the programmer and to the operating system, which is also why they do not have garbage collection. Implementation of new and delete is not part of the C++ standard, because C++ is meant to be used on a variety of platforms, which may manage their memory in very different ways. It may be possible to let C++ keep track of all the allocated arrays and their sizes if you are writing a word processor for a windows box running on the latest Intel CPU, but it may be completely infeasible when you are writing an embedded system running on a DSP.

这篇关于有没有什么办法来确定C ++编程数组的大小?如果不是,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆