以编程方式确定 C++ 数组的大小? [英] Determine the size of a C++ array programmatically?

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

问题描述

这个问题的灵感来自一个类似的问题:delete[] 如何知道"操作数数组的大小?

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

我的问题有点不同:有没有办法以编程方式确定 C++ 数组的大小?如果不是,为什么? 我见过的每个接受数组的函数也需要一个整数参数来指定大小.但是正如链接的问题所指出的那样,delete[] 必须知道要释放的内存的大小.

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++ 代码:

Consider this C++ code:

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

这会打印Size of 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)).

推荐答案

delete [] 确实知道分配的大小.但是,该知识驻留在运行时或操作系统的内存管理器中,这意味着它在编译期间对编译器不可用.而且 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.

另外,考虑这个例子:


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

编译器如何知道p 的大小?问题的根源在于 C 和 C++ 中的数组不是一等对象.它们衰减为指针,编译器或程序本身无法知道指针是指向由 new 分配的内存块的开头,还是指向单个对象,或者指向new 分配的内存块中间的某个位置.

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++ 将内存管理留给了程序员和操作系统,这也是它们没有垃圾收集的原因.newdelete 的实现不是 C++ 标准的一部分,因为 C++ 旨在用于各种平台,这些平台可能以非常不同的方式管理它们的内存.如果您正在为运行在最新英特尔 CPU 上的 Windows 框编写文字处理器,则可以让 C++ 跟踪所有分配的数组及其大小,但是当您编写运行在其上的嵌入式系统时,这可能完全不可行一个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天全站免登陆