C数组作为函数参数:大小检查? [重复]

时间:2022-07-01 02:41:56

This question already has an answer here:

这个问题在这里已有答案:

I wrote the following function in C:

我在C中写了以下函数:

   void dummy(int* my_array, int size)
   {
     //my implementation
   }

Is there any way to check whether size is REALLY the size of my_array? For example, if I call the function and use for my_array an array with 4 elements but pass 5 for size, is there any way to know that 5 is not really the size of the array?

有没有办法检查大小是否真的是my_array的大小?例如,如果我调用该函数并使用my_array一个包含4个元素但是传递5个大小的数组,有没有办法知道5实际上不是数组的大小?

Thank you in evidence.

谢谢你的证据。

4 个解决方案

#1


4  

You're looking at it the wrong way. An array is contiguous piece of memory. In C, you can represent this concept with a pointer to its start and its size. Since your array is represented by a <my_array, size> tuple, it doesn't make sense to talk about my_array's size, since it's only the start-pointer of the array.

你正在以错误的方式看待它。数组是连续的内存。在C中,您可以使用指向其开头及其大小的指针来表示此概念。由于你的数组由 元组表示,所以谈论my_array的大小是没有意义的,因为它只是数组的起始指针。 ,size>

#2


0  

unfortunately, there is no such way in C. As an array is always a pointer to an arbitrary address in memory, there is no such thing as "array bounds" that can be checked.

遗憾的是,在C中没有这样的方法。由于数组总是指向内存中任意地址的指针,因此不存在可以检查的“数组边界”。

This is why many of the C functions have that special size parameter: because there's no other way of determining an array's size.

这就是许多C函数具有特殊大小参数的原因:因为没有其他方法可以确定数组的大小。

#3


0  

The size of the array is however big you made it when you declared and initialized it.

然而,当您声明并初始化数组时,数组的大小很大。

int my_array[100] = {0}; has a size of 100.

int my_array [100] = {0};的大小为100。

int my_other_array[50] = {0}; has a size of 50.

int my_other_array [50] = {0};的大小为50。

If you want the length of the data, then you're thinking in more abstract terms than the language can handle. The length of your data is a non-measurable parameter when the language does not support it.

如果你想要数据的长度,那么你正在考虑比语言能够处理的更抽象的术语。当语言不支持时,数据的长度是不可测量的参数。

#4


-1  

If my_array is dynamically allocated (with malloc) you can get the size of memory block but it depends on specific compiler. Unfortunately in most cases there is address alignments and you will not have the exact size to 1 byte but aligned to 32 bits or other.

如果动态分配my_array(使用malloc),则可以获得内存块的大小,但这取决于特定的编译器。不幸的是,在大多数情况下,存在地址对齐,并且您将不具有1字节的确切大小,而是与32位或其他对齐。

#1


4  

You're looking at it the wrong way. An array is contiguous piece of memory. In C, you can represent this concept with a pointer to its start and its size. Since your array is represented by a <my_array, size> tuple, it doesn't make sense to talk about my_array's size, since it's only the start-pointer of the array.

你正在以错误的方式看待它。数组是连续的内存。在C中,您可以使用指向其开头及其大小的指针来表示此概念。由于你的数组由 元组表示,所以谈论my_array的大小是没有意义的,因为它只是数组的起始指针。 ,size>

#2


0  

unfortunately, there is no such way in C. As an array is always a pointer to an arbitrary address in memory, there is no such thing as "array bounds" that can be checked.

遗憾的是,在C中没有这样的方法。由于数组总是指向内存中任意地址的指针,因此不存在可以检查的“数组边界”。

This is why many of the C functions have that special size parameter: because there's no other way of determining an array's size.

这就是许多C函数具有特殊大小参数的原因:因为没有其他方法可以确定数组的大小。

#3


0  

The size of the array is however big you made it when you declared and initialized it.

然而,当您声明并初始化数组时,数组的大小很大。

int my_array[100] = {0}; has a size of 100.

int my_array [100] = {0};的大小为100。

int my_other_array[50] = {0}; has a size of 50.

int my_other_array [50] = {0};的大小为50。

If you want the length of the data, then you're thinking in more abstract terms than the language can handle. The length of your data is a non-measurable parameter when the language does not support it.

如果你想要数据的长度,那么你正在考虑比语言能够处理的更抽象的术语。当语言不支持时,数据的长度是不可测量的参数。

#4


-1  

If my_array is dynamically allocated (with malloc) you can get the size of memory block but it depends on specific compiler. Unfortunately in most cases there is address alignments and you will not have the exact size to 1 byte but aligned to 32 bits or other.

如果动态分配my_array(使用malloc),则可以获得内存块的大小,但这取决于特定的编译器。不幸的是,在大多数情况下,存在地址对齐,并且您将不具有1字节的确切大小,而是与32位或其他对齐。