Length of array

V

vicky

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
 
A

analizer

There is _msize() function in Visual C compilers. It can be used with
following way:

template<class T> 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;
}
 
I

Ian Collins

vicky said:
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?
 
V

vikram_p_nayak

There is _msize() function in Visual C compilers. It can be used with
following way:

template<class T> 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.
 
A

analizer

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 :)
 
R

Rolf Magnus

vicky said:
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.
 
A

Axter

There is _msize() function in Visual C compilers. It can be used with
following way:

template<class T> 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 <iostream>
#include <malloc.h>
using namespace std ;


template<typename T>
class ConcreteArrayLength
{
private:
template <typename TT> struct deref_t {typedef TT type_t;};
template <typename TT> struct deref_t<TT*> {typedef typename
deref_t<TT>::type_t type_t;};
public:
typedef typename deref_t<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<class T>
size_t GetArrayLength(T& t) //Portable method
{
return ConcreteArrayLength<T>()(t);
}

template<class T> 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 ;
}
 
J

Jakob Bieling

[VC++ specific half-solution]
template<class T> 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
 
A

analizer

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.
 
F

Frederick Gotham

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top