本文翻译自:How to find the 'sizeof' (a pointer pointing to an array)?
First off, here is some code: 首先,这是一些代码:
int main()
{
int days[] = {1,2,3,4,5};
int *ptr = days;
printf("%u\n", sizeof(days));
printf("%u\n", sizeof(ptr));
return 0;
}
Is there a way to find out the size of the array that ptr
is pointing to (instead of just giving its size, which is four bytes on a 32-bit system)? 有没有办法找出ptr
指向的数组的大小(而不是仅仅给出其大小,在32位系统上为4个字节)?
#1楼
参考:https://stackoom.com/question/245G/如何找到-sizeof-指向数组的指针
#2楼
There is a clean solution with C++ templates, without using sizeof() . 使用C ++模板有一个干净的解决方案,而不使用sizeof() 。 The following getSize() function returns the size of any static array: 以下getSize()函数返回任何静态数组的大小:
#include <cstddef>
template<typename T, size_t SIZE>
size_t getSize(T (&)[SIZE]) {
return SIZE;
}
Here is an example with a foo_t structure: 这是一个具有foo_t结构的示例:
#include <cstddef>
template<typename T, size_t SIZE>
size_t getSize(T (&)[SIZE]) {
return SIZE;
}
struct foo_t {
int ball;
};
int main()
{
foo_t foos3[] = {{1},{2},{3}};
foo_t foos5[] = {{1},{2},{3},{4},{5}};
printf("%u\n", getSize(foos3));
printf("%u\n", getSize(foos5));
return 0;
}
Output: 输出:
3
5
#3楼
As all the correct answers have stated, you cannot get this information from the decayed pointer value of the array alone. 正如所有正确答案所述,您不能仅从数组的衰减指针值中获取此信息。 If the decayed pointer is the argument received by the function, then the size of the originating array has to be provided in some other way for the function to come to know that size. 如果衰减的指针是函数接收到的参数,则必须以其他方式提供原始数组的大小,以使函数知道该大小。
Here's a suggestion different from what has been provided thus far,that will work: Pass a pointer to the array instead. 这是与到目前为止所提供的建议不同的建议,它将起作用:将指针传递给数组。 This suggestion is similar to the C++ style suggestions, except that C does not support templates or references: 该建议与C ++样式建议相似,不同之处在于C不支持模板或引用:
#define ARRAY_SZ 10
void foo (int (*arr)[ARRAY_SZ]) {
printf("%u\n", (unsigned)sizeof(*arr)/sizeof(**arr));
}
But, this suggestion is kind of silly for your problem, since the function is defined to know exactly the size of the array that is passed in (hence, there is little need to use sizeof at all on the array). 但是,此建议对于您的问题有点愚蠢,因为已定义函数以确切知道传入的数组的大小(因此,几乎不需要在数组上使用sizeof)。 What it does do, though, is offer some type safety. 但是,它所做的是提供某种类型的安全性。 It will prohibit you from passing in an array of an unwanted size. 它将禁止您传入不需要大小的数组。
int x[20];
int y[10];
foo(&x); /* error */
foo(&y); /* ok */
If the function is supposed to be able to operate on any size of array, then you will have to provide the size to the function as additional information. 如果该函数能够在任意大小的数组上运行,那么您将必须为函数提供大小,以作为附加信息。
#4楼
#define array_size 10
struct {
int16 size;
int16 array[array_size];
int16 property1[(array_size/16)+1]
int16 property2[(array_size/16)+1]
} array1 = {array_size, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
#undef array_size
array_size is passing to the size variable: array_size传递给size变量:
#define array_size 30
struct {
int16 size;
int16 array[array_size];
int16 property1[(array_size/16)+1]
int16 property2[(array_size/16)+1]
} array2 = {array_size};
#undef array_size
Usage is: 用法是:
void main() {
int16 size = array1.size;
for (int i=0; i!=size; i++) {
array1.array[i] *= 2;
}
}
#5楼
My solution to this problem is to save the length of the array into a struct Array as a meta-information about the array. 我对这个问题的解决方案是将数组的长度保存到结构Array中,作为有关该数组的元信息。
#include <stdio.h>
#include <stdlib.h>
struct Array
{
int length;
double *array;
};
typedef struct Array Array;
Array* NewArray(int length)
{
/* Allocate the memory for the struct Array */
Array *newArray = (Array*) malloc(sizeof(Array));
/* Insert only non-negative length's*/
newArray->length = (length > 0) ? length : 0;
newArray->array = (double*) malloc(length*sizeof(double));
return newArray;
}
void SetArray(Array *structure,int length,double* array)
{
structure->length = length;
structure->array = array;
}
void PrintArray(Array *structure)
{
if(structure->length > 0)
{
int i;
printf("length: %d\n", structure->length);
for (i = 0; i < structure->length; i++)
printf("%g\n", structure->array[i]);
}
else
printf("Empty Array. Length 0\n");
}
int main()
{
int i;
Array *negativeTest, *days = NewArray(5);
double moreDays[] = {1,2,3,4,5,6,7,8,9,10};
for (i = 0; i < days->length; i++)
days->array[i] = i+1;
PrintArray(days);
SetArray(days,10,moreDays);
PrintArray(days);
negativeTest = NewArray(-5);
PrintArray(negativeTest);
return 0;
}
But you have to care about set the right length of the array you want to store, because the is no way to check this length, like our friends massively explained. 但是,您必须关心设置要存储的数组的正确长度,因为就像我们的朋友大量解释的那样,这是无法检查该长度的方法。
#6楼
No, you can't use sizeof(ptr)
to find the size of array ptr
is pointing to. 不,您不能使用sizeof(ptr)
来查找ptr
指向的数组的大小。
Though allocating extra memory(more than the size of array) will be helpful if you want to store the length in extra space. 如果要将长度存储在额外的空间中,尽管分配额外的内存(大于数组的大小)会很有帮助。