446,148 Members | 1,373 Online
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

# Length of array

 P: n/a hi all! This is an interesting question asked in interview of HCL. Suppose a situation that u have a pointer to an array and u don't the size of array, so how could u find the size of array by having only pointer to that array? I tried but haven't ound any solution yet. Regards, Zia Jun 26 '06 #1
9 Replies

 P: n/a There is _msize() function in Visual C compilers. It can be used with following way: template int ArrayLength(T* p) { return (_msize(p)/sizeof(T)); } void main() { int *a=new int[10]; printf("array size: %d\n",ArrayLength(a)); delete[]a; } Jun 26 '06 #2

 P: n/a vicky wrote: hi all! This is an interesting question asked in interview of HCL. Suppose a situation that u have a pointer to an array and u don't the size of array, so how could u find the size of array by having only pointer to that array? I tried but haven't ound any solution yet. You can't. Who's this 'u' you are addressing? -- Ian Collins. Jun 26 '06 #3

 P: n/a anali...@yandex.ru wrote: There is _msize() function in Visual C compilers. It can be used with following way: template int ArrayLength(T* p) { return (_msize(p)/sizeof(T)); } void main() { int *a=new int[10]; printf("array size: %d\n",ArrayLength(a)); delete[]a; } Not sure this is what the OP is looking for. What happens if a is not dynamically allotted? AFAIK, there is no way to find the size of the array. Jun 26 '06 #4

 P: n/a > Not sure this is what the OP is looking for. What happens if a is not dynamically allotted? Ok, you are right. This method works only if memory is dinamically-allocated and unable to get size of stack-allocated array. So, it is better than nothing :) Jun 26 '06 #5

 P: n/a vicky wrote: hi all! This is an interesting question asked in interview of HCL. Suppose a situation that u have a pointer to an array and u don't the size of array, so how could u find the size of array by having only pointer to that array? What's 'u'? A function? I tried but haven't ound any solution yet. There is none, unless your array has a specific end marker that 'u' could check for. Jun 26 '06 #6

 P: n/a vikram_p_na...@yahoo.com wrote: anali...@yandex.ru wrote: There is _msize() function in Visual C compilers. It can be used with following way: template int ArrayLength(T* p) { return (_msize(p)/sizeof(T)); } void main() { int *a=new int[10]; printf("array size: %d\n",ArrayLength(a)); delete[]a; } Not sure this is what the OP is looking for. What happens if a is not dynamically allotted? AFAIK, there is no way to find the size of the array. If the size is not dynamically allocated, there's a portable way to find the size if you have a reference to the array. See example GetArrayLength below. If it's dynamically allocated, you would have to use non-standard methods, like using _msize. Example code: #include #include using namespace std ; template class ConcreteArrayLength { private: template struct deref_t {typedef TT type_t;}; template struct deref_t {typedef typename deref_t::type_t type_t;}; public: typedef typename deref_t::type_t ref_t; typedef T type_t; size_t operator()(ref_t &t) { return sizeof(t)/sizeof(t[0]); } private: size_t operator()(ref_t *t); }; template size_t GetArrayLength(T& t) //Portable method { return ConcreteArrayLength()(t); } template size_t GetArrayPtrLength(T& p) { return (_msize(p)/sizeof(T)); //Not portable method } int main() { int *dyn_alloc = new int[8]; cout << "dyn_alloc size: " << GetArrayPtrLength(dyn_alloc) << endl; //cout << "dyn_alloc size: " << GetArrayLength(dyn_alloc) << endl; //This will give compile time warning int concrete_obj [] = { 1, 2, 3, 4, 5 } ; cout << "concrete_obj size: " << GetArrayLength(concrete_obj) << endl; //cout << "concrete_obj size: " << GetArrayPtrLength(concrete_obj) << endl; //Will give runtime error delete[] dyn_alloc; system("pause"); return 0 ; } Jun 26 '06 #7

 P: n/a analizer wrote: [VC++ specific half-solution] template int ArrayLength(T* p) { return (_msize(p)/sizeof(T)); } This method works only if memory is dinamically-allocated It might be worth noting that it only works if the memory was dynamically allocated with malloc, calloc or realloc (according to the MSDN). If you use new/new[] as you should, this might not work either. regards -- jb (reply address in rot13, unscramble first) Jun 26 '06 #8

 P: n/a > It might be worth noting that it only works if the memory was dynamically allocated with malloc, calloc or realloc (according to the MSDN). If you use new/new[] as you should, this might not work either. It works with plain old data arrays, of course in some cases it can work wrong with new operator when this operator is overloaded by some class. Jun 26 '06 #9

 P: n/a vicky posted: hi all! This is an interesting question asked in interview of HCL. Interesting to a novice, perhaps. Suppose a situation that u have a pointer to an array Here's an array: int array[64]; Here's an pointer to an array: int (*p_array)[64] = array; and u don't the size of array, You always know the size of an array if you have a pointer to it. (I suspect you're mistakenly referring to "a pointer to the first element".) so how could u find the size of array by having only pointer to that array? sizeof( *p_array ) / sizeof( **p_array ) If, on the other hand, we're dealing with a pointer to the first element of an array, e.g.: int array[64]; int *p_first_element = array; And we then pass it to a function such as the following one: void ArbitraryFunc( int * ); Then we've already lost the type information... so there's no way of retrieving the length of the array. -- Frederick Gotham Jun 26 '06 #10

### This discussion thread is closed

Replies have been disabled for this discussion.