473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Length of array

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 5564
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;
}

Jun 26 '06 #2
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

anali...@yandex.ru wrote:
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.

Jun 26 '06 #4
> 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
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
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<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 ;
}

Jun 26 '06 #7
analizer <an******@yandex.ru> wrote:

[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
--
jb

(reply address in rot13, unscramble first)
Jun 26 '06 #8
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
8
by: Fernando Barsoba | last post by:
Hi, I decided to start a new topic about my memory allocation question. One of the answers was that variable length arrays are possible using C99. I found this topic that also mentions it. ...
1
by: moonriver | last post by:
I intend to generate a variable-length array, similar to link lists in plain C. For example, I define the following array int a Initially I assign 5 elements to the array as a = new int...
8
by: lovecreatesbeauty | last post by:
Hello experts, I have seen following the code snippet given by Marc Boyer (with slight changes by me for a better format), and have doubts on it. I am so grateful if you can give me your kindly...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.