Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

getting size of array from pointer c++

I am writing a simple function that returns the largest integer in an array. The problem I am having is finding the number of elements in the array.

Here is the function header:

int largest(int *list, int highest_index)

How can I get the number of integers in the array 'list'.

I have tried the following methods:

int i = sizeof list/sizeof(int); //returns incorrect value
int i = list.size(); // does not compile

Any help will be greatly appreciated!

question from:https://stackoverflow.com/questions/65937162/c-new-array-with-dynamic-size-always-has-size-8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

C++ is based on C and inherits many features from it. In relation to this question, it inherits something called "array/pointer equivalence" which is a rule that allows an array to decay to a pointer, especially when being passed as a function argument. It doesn't mean that an array is a pointer, it just means that it can decay to one.

void func(int* ptr);

int array[5];
int* ptr = array; // valid, equivalent to 'ptr = &array[0]'
func(array); // equivalent to func(&array[0]);

This last part is the most relevant to your question. You are not passing the array, you are passing the address of the 0th element.

In order for your function to know how big the incoming array is, you will need to send that information as an argument.

static const size_t ArraySize = 5;
int array[ArraySize];
func(array, ArraySize);

Because the pointer contains no size information, you can't use sizeof.

void func(int* array) {
    std::cout << sizeof(array) << "
";
}

This will output the size of "int*" - which is 4 or 8 bytes depending on 32 vs 64 bits.

Instead you need to accept the size parameters

void func(int* array, size_t arraySize);

static const size_t ArraySize = 5;
int array[ArraySize];
func(array, ArraySize);

Even if you try to pass a fixed-sized array, it turns out this is syntactic sugar:

void func(int array[5]);

http://ideone.com/gaSl6J

Remember how I said that an array is NOT a pointer, just equivalent?

int array[5];
int* ptr = array;

std::cout << "array size " << sizeof(array) << std::endl;
std::cout << "ptr size " << sizeof(ptr) << str::endl;

array size will be 5 * sizeof(int) = 20 ptr size will be sizeof(int *) which will be either 4 or 8 bytes.

sizeof returns the size of the type being supplied, if you supply an object then it deduces the type and returns the size of that

If you want to know how many elements of an array are in the array, when you have the array and not a pointer, you can write

sizeof(array) / sizeof(array[0])

or sizeof(array) / sizeof(*array)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...