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

查看:75
本文介绍了通过编程确定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.

另外,请考虑以下示例:

Also, consider this example:


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 ++旨在用于多种平台,这些平台可能以非常不同的方式管理其内存.如果您正在为运行在最新Intel CPU上的Windows框编写文字处理器,则可以让C ++跟踪所有分配的数组及其大小,但是当您编写在Windows XP上运行的嵌入式系统时,这可能是完全不可行的. 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天全站免登陆