How can you get the number of elements in an array allocated dynamically at runtime? For example -int *p = (int *) malloc(sizeof(int) * n);int numElems = ??????? /* n is not available here */

Showing Answers 1 - 10 of 10 Answers

byte

  • Nov 8th, 2005
 

To get the numElements in a situation where we don't know the n following logic will work.

1. First get the total size of array. i.e. X = sizeof(p) * n

2. Now divide the X with size of each element. numElements = X/sizeof(int).

  Was this answer useful?  Yes

If the intent of the question is getting the number of elements in dynamically allocated array from the given pointer, in my view, it is not possible to get it. Need for array length of static memory allocation is valid since there is no link to access compile time constants (array length) from run-time. But the need for array length of dynamic memory allocation is invalid since allocation is done based on the total number of elements. So it is the responsibility of programmer to retain that data (total number of elements)But as in the earlier reply, X = sizeof(p) * n , X never gives the total size of array (may the code here gives the array size correctly since int and int* sizeof is same). sizeof(p) gives total number of bytes occupied by the pointer variable p which is always different from each data-type/structure variable size.

  Was this answer useful?  Yes

Neil

  • Nov 30th, 2005
 

One way according to me would this. We know the base address of the array cos we have a pointer to it. Then we also know the unit length. Now simply traverse through the dynamic array like a data structure (queue) and get the number of elements dynamically allocated. This way we can keep track.

  Was this answer useful?  Yes

kiranck007

  • Jan 26th, 2006
 

as such there are no boundry conditions like "\n" for a string, in case of integers..so u shud know the length of the array

  Was this answer useful?  Yes

kbjarnason

  • Jul 2nd, 2010
 

The short answer is, you can't.

With static arrays, you know the size of both the array and its elements and can simply work it out.  With dynamically allocated arrays, the size information is not available, so you've nothing to use to determine element count.

The only way to do it is to record the size (or, more usefully, the element count) and pass it around as necessary.

Side note: don't cast malloc.  In C, it can hide potentially serious bugs, in C++ you've got new, why use malloc?

If all you have is the pointer p, then there is no (standard) way to determine how many elements were allocated to the chunk it's pointing to.  If you have to keep track of how big your dynamic buffers are, then you need to store that information separately. 

BTW, the preferred form for malloc calls is

<pre>
T *p = malloc(sizeof *p * number_of_elements);
<pre>

Two things to note: first, I am not casting the return value of malloc(); second, I am using the sizeof operator on the object, not the type.

Casting the return value of malloc() is a bad idea because it can supress a useful diagnostic if you forget to include stdlib.h or otherwise don't have a prototype for malloc() in scope.  It's also unnecessary, since in C, pointers to void can be implicitly converted to pointers to other types.  This is not the case in C++, but since C++ provides the new operator, you don't have to worry about it. 

Using sizeof on the object instead of the type saves some hassles if the type of p ever has to change. 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions