Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have pointer of array(array which is in memory)

Can i calculate the size of array from its pointer ??????
i dont know actually where is the array in memory..i m only getting pointer adress(suppose 9001) using that adress i have to calculate array size.

Thanks.
Posted
Updated 21-Feb-12 20:47pm
v3
Comments
CPallini 22-Feb-12 3:07am    
Short answer: you cannot.

If all you have is the pointer to the first element then you can't:
C#
int array[6]= { 1, 2, 3, 4, 5, 6 };
void main() 
  {
  int *parray = &(array[0]);
  int len=sizeof(array)/sizeof(int);
  printf("Length Of Array=%d\n", len);
  len = sizeof(parray);
  printf("Length Of Array=%d\n", len);
  getch();
}
Will give two different values: 6, and 4.
The six is because array contains 6 elements, so sizeof returns the number of bytes in the whole array, and we divide it by the element size (int).
The four is because sizeof returns the size of the pointer, rather than the object pointed to.
 
Share this answer
 
Comments
CPallini 22-Feb-12 3:07am    
5.
Stefan_Lang 22-Feb-12 7:42am    
Note that unless you drop that array size in your first line, calculating the array length is a rather moot point. ;)

Still a nice and absolutely valid explanation. My 5.
OriginalGriff 22-Feb-12 8:18am    
Yeah - I was just making it as obvious as I could for the example.
Malli_S 23-Apr-12 4:30am    
5.
stib_markc 23-Apr-12 4:40am    
5
1. If you have only a pointer, the answer is no.
C++
std::size_t getsize(int* parray) {
   return 0; // sorry, no way to tell!
}


2. If you have s statically allocated array, you can determine the number of elements from the arrays name. However, if that is the case, and you don't use C++ functionality, then most likely there wouldn't be a need for this in the first place.
C++
int array[10];
std::size_t length = 10; // big surprise, that number is right in the definition!

or:
C++
int array[] = {4,78,3,7,9,2,56,2,76,23,6,2,1,645};
std::size_t length = sizeof(array)/sizeof(int); // see solution 1


3. Alternately, use the containers from the STL, e. g. std::array or std::vector. These containers behave very similar to standard arrays, but you can query their size at runtime, no problem.
C++
std::vector<int> array;
array.pushback(2);
array.pushback(7);
array.pushback(344);
array.pushback(45);
array.pushback(89);
array.pushback(28);
std::size_t length = array.size(); // easy!
 
Share this answer
 
v2
If you have ONLY the pointer that is passed to the procedure, you can't do OriginalGriff's solution[^]. You will need to explicitly pass the size of the array to the function. This is not uncommon in C/C++ development. Lot of C library functions have this behavior.
 
Share this answer
 
 
Share this answer
 
Comments
krumia 22-Feb-12 3:46am    
StackOverflow seems to be the rather active forum! :|
Hi,

<pre>sizeof(arrayname);</pre>

will do the trick..

Thanks,
 
Share this answer
 
Comments
CPallini 22-Feb-12 3:06am    
It would probably fail in the OP scenario.
Please see my article here:How to get size of array from its pointer[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900