# Thread: Calloc Pointer to Array, Find Num Elements Post-Initialization

1. ## Calloc Pointer to Array, Find Num Elements Post-Initialization

This is more of a theory question than a specific question to a problem.

Let's say I have dynamically allocated an array on the heap:

Code:
```int numitems;
double *ptr;
scanf("&#37;d",&numitems);
ptr = (double*) calloc(numitems,sizeof(double));```
Now, without knowing the value of numitems, one can figure out the number of declared elements in an array with:

Code:
`num_elements = sizeof(array_name)/sizeof(array_name)`
(or they can divide by sizeof(array_type) )

However, when working with pointers, I have not been able to find a method to do the same thing.

Code:
`sizeof(*ptr);`
returns 4, the size of the memory address and
Code:
`sizeof(ptr);`
returns 8, the size of a double

How can one find the number of elements in a dynamically allocated array with the use of pointers?

The reason I'm asking this is because I had been working on a semester project for my programming class in college. Coming from my c++ beginnings, this was a very frustrating problem, since in c++ you can just use .length() to figure this out. In my project, I had multiple functions being called in other functions. While the original function did not need to know the number of elements in the array, the various functions being called did. Therefore, I initially set out to solve this problem using a sizeof() technique rather than adding a parameter to functions that did not need to know the number of elements. After no luck, I finally just added the extra parameter to the functions.

While I know just adding the extra parameter is most likely the easiest method, I am still curious as to how one can find the size of the whole array a pointer is pointingto, rather than a single element, without access to the array itself. It has to be able to be done, since the size of the array must be stored somewhere in order for
Code:
`free(ptr)`
to work correctly

I'm sorry for the long post, and I extremely appreciate any help or hints.

Austin 2. You can't find out how much memory was allocated, simply from looking at the pointer.

> since the size of the array must be stored somewhere
Yes, it is stored somewhere.
The problem for you is there is no portable API which allows you to extract that value.

> since in c++ you can just use .length() to figure this out.
But only on a container like std::vector which provides a portable mechanism for storing additional information such as this, along with a method for retrieving it.

Oh, and read this - http://c-faq.com/malloc/calloc.html
regarding your use of calloc when allocating space for doubles.

Also, read the FAQ on why casting malloc/calloc in a C program is bad. 3. You can't. Just remember how much memory you allocate. Pass that argument around to functions that need to know the block size.

If you want to go into code that isn't portable I believe Microsoft has a function called _msize() or something similar that can give you the size of a dynamic block of memory. This won't work for all compilers on all systems, though. 4. If you need to remember the size of the buffer, then remember it, i.e. in a variable somewhere. If this seems like a pain, then put the buffer and the size together in a struct. 5. There is no portable way of doing this. Some compilers (or runtime libraries) have extensions to allow the size to be extracted from the "malloc header".

My suggestion would be that "you remember what you done", in other words, you pass along a size to the functions that need to know how many elements it is.

By the way ".length()" only works because vectors, etc, store the length. If you want to do the same in C, you can use a struct like this:
Code:
```struct double_vector
{
size_t length;
double *data;
};```
--
Mats 6. thank you for the quick replies

I had the struct idea, and I don't remember exactly why I didn't end up going through with it.

I ended up using the variable solution, but I just wanted to know if there was an alternative method. As for the calloc and malloc usage, I just followed the instructions of the professor, but I will certainly check out the provided links and look into _msize().

Thanks again for the information,
Austin Popular pages Recent additions 