Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting size of array from pointer c++

Tags:

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!

like image 856
user906357 Avatar asked Nov 10 '13 20:11

user906357


People also ask

How do you find the size of an array of pointers?

int size= sizeof(Array)/sizeof(Array[0]); where as in the second code it returns the size of pointer.

How do you find the size of an array in C?

We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);

Can you use sizeof on a pointer?

The code calls sizeof() on a malloced pointer type, which always returns the wordsize/8. This can produce an unexpected result if the programmer intended to determine how much memory has been allocated. The use of sizeof() on a pointer can sometimes generate useful information.

Can you use sizeof on a pointer in C?

Size of a Pointer to Pointer As we already know, the size of the pointer in C is dependent only on the word size of a particular system. So, the size of a pointer to a pointer should have the usual values, that is, 2 bytes for a 16-bit machine, 4 bytes for a 32-bit machine, and 8 bytes for a 64-bit machine.


2 Answers

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) << "\n"; } 

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)

like image 123
kfsone Avatar answered Sep 21 '22 05:09

kfsone


There's no way to do that. This is one good reason (among many) to use vectors instead of arrays. But if you must use an array then you must pass the size of the array as a parameter to your function

int largest(int *list, int list_size, int highest_index) 

Arrays in C++ are quite poor, the sooner you learn to use vectors the easier you will find things.

like image 42
john Avatar answered Sep 20 '22 05:09

john