Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Checking the size of an array

Reply
Thread Tools

Checking the size of an array

 
 
Noah Roberts
Guest
Posts: n/a
 
      02-14-2007
The Cool Giraffe wrote:
> I have a dynamically declared array of integers and i'd
> like to check the number of elements. For some reason,
> the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
> work. I've been googling and i got a head ache more
> than understanding.


You can't do it.

>
> Let me put my question this way. In Java, i'd go as
> mtx.length to learn the size. How can it be done
> _easily_ in C++? The shorter the better.
>


If you use std::vector<> instead of a dynamically allocated array you
will be able to get the element count from the size() member.
 
Reply With Quote
 
 
 
 
int2str@gmail.com
Guest
Posts: n/a
 
      02-14-2007
On Feb 14, 1:06 pm, "The Cool Giraffe" <(E-Mail Removed)>
wrote:
> I have a dynamically declared array of integers and i'd
> like to check the number of elements.


You cannot do this in C++ since an array is essentially a pointer to a
memory region.

> Let me put my question this way. In Java, i'd go as
> mtx.length to learn the size. How can it be done
> _easily_ in C++? The shorter the better.


Use a std::vector instead.
Then you can use mtx.size() down the road.

Cheers,
Andre

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      02-14-2007
On Feb 14, 4:06 pm, "The Cool Giraffe" <(E-Mail Removed)>
wrote:

> I have a dynamically declared array of integers and i'd
> like to check the number of elements. For some reason,
> the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
> work. I've been googling and i got a head ache more
> than understanding.
>
> Let me put my question this way. In Java, i'd go as
> mtx.length to learn the size. How can it be done
> _easily_ in C++? The shorter the better.


To do this easily in C++, don't use an array, use a std::vector.
std::vector::size() returns the size of the vector.

E.g.:

#include <vector>

int main()
{
using namespace std;
vector<int> v;
v.push_back(7);
v.push_back(-3);
v.push_back(6);
v.push_back(12);
// etc.
int theSize = v.size();
}

Best regards,

Tom




 
Reply With Quote
 
The Cool Giraffe
Guest
Posts: n/a
 
      02-14-2007
I have a dynamically declared array of integers and i'd
like to check the number of elements. For some reason,
the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
work. I've been googling and i got a head ache more
than understanding.

Let me put my question this way. In Java, i'd go as
mtx.length to learn the size. How can it be done
_easily_ in C++? The shorter the better.

--
Všnligen Kerstin Viltersten
(The Cool Giraffe)


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-14-2007
The Cool Giraffe wrote:
> I have a dynamically declared array of integers and i'd
> like to check the number of elements. For some reason,
> the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
> work. I've been googling and i got a head ache more
> than understanding.
>
> Let me put my question this way. In Java, i'd go as
> mtx.length to learn the size. How can it be done
> _easily_ in C++? The shorter the better.


There is no [portable] way.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Vallabha
Guest
Posts: n/a
 
      02-15-2007
On Feb 15, 2:06 am, "The Cool Giraffe" <(E-Mail Removed)>
wrote:
> I have a dynamically declared array of integers and i'd
> like to check the number of elements. For some reason,
> the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
> work. I've been googling and i got a head ache more
> than understanding.
>
> Let me put my question this way. In Java, i'd go as
> mtx.length to learn the size. How can it be done
> _easily_ in C++? The shorter the better.
>
> --
> Všnligen Kerstin Viltersten
> (The Cool Giraffe)


I prefer vectors over dynamic array in such situaltions, as vectors
are more handy (say initilization, growing elements etc) ..and using
vectors you can easily find out the length also..

-Vallabha
S7 Software Solutions
http://www.s7solutions.com

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      02-15-2007
Victor Bazarov wrote:
> The Cool Giraffe wrote:
>> I have a dynamically declared array of integers and i'd
>> like to check the number of elements. For some reason,
>> the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
>> work. I've been googling and i got a head ache more
>> than understanding.
>>
>> Let me put my question this way. In Java, i'd go as
>> mtx.length to learn the size. How can it be done
>> _easily_ in C++? The shorter the better.

>
> There is no [portable] way.


I'm curious, what is a non-portable one? I imagine if you knew how the
heap was tracked and had access to the memory regions containing the
information you want, you could read it. It won't be the element count
though, it will be the size of the allocated region, which may or may
not coincide with how much was requested. So even if you had access,
which in most cases I don't believe you do, the information you where
able to read probably wouldn't be of much use to you and would be
inaccurate for your purpose.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-15-2007
Noah Roberts wrote:
> Victor Bazarov wrote:
>> The Cool Giraffe wrote:
>>> I have a dynamically declared array of integers and i'd
>>> like to check the number of elements. For some reason,
>>> the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
>>> work. I've been googling and i got a head ache more
>>> than understanding.
>>>
>>> Let me put my question this way. In Java, i'd go as
>>> mtx.length to learn the size. How can it be done
>>> _easily_ in C++? The shorter the better.

>>
>> There is no [portable] way.

>
> I'm curious, what is a non-portable one? I imagine if you knew how
> the heap was tracked and had access to the memory regions containing
> the information you want, you could read it. It won't be the element
> count though, it will be the size of the allocated region, which may
> or may not coincide with how much was requested. So even if you had
> access, which in most cases I don't believe you do, the information
> you where able to read probably wouldn't be of much use to you and
> would be inaccurate for your purpose.


Divide the size [of the allocated region] by the sizeof(*ptr) and you
get the size of the array, no? If it matters to know exactly how much
was *requested*, it would be passed to the same function as a separate
argument.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      02-15-2007
Victor Bazarov wrote:
> Noah Roberts wrote:
>> Victor Bazarov wrote:
>>> The Cool Giraffe wrote:
>>>> I have a dynamically declared array of integers and i'd
>>>> like to check the number of elements. For some reason,
>>>> the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
>>>> work. I've been googling and i got a head ache more
>>>> than understanding.
>>>>
>>>> Let me put my question this way. In Java, i'd go as
>>>> mtx.length to learn the size. How can it be done
>>>> _easily_ in C++? The shorter the better.
>>> There is no [portable] way.

>> I'm curious, what is a non-portable one? I imagine if you knew how
>> the heap was tracked and had access to the memory regions containing
>> the information you want, you could read it. It won't be the element
>> count though, it will be the size of the allocated region, which may
>> or may not coincide with how much was requested. So even if you had
>> access, which in most cases I don't believe you do, the information
>> you where able to read probably wouldn't be of much use to you and
>> would be inaccurate for your purpose.

>
> Divide the size [of the allocated region] by the sizeof(*ptr) and you
> get the size of the array, no? If it matters to know exactly how much
> was *requested*, it would be passed to the same function as a separate
> argument.


Well, I think that is what is desired though. How much was actually
allocated isn't of much use...the extra space is likely to not be
initialized at all, much less filled with useful information.

I'm pretty sure what the OP wants is something like the following:

void f(char [] ptr)
{
for (int i = 0; i < ptr.length; ++i)
putchar(ptr[i]);
}

I don't think even your non-portable solution will do what they want
much of the time. AFAIK it wouldn't even be dependable on a single
given architecture with a particular compiler and version...or even
between runs.

Of course, they could write their own allocator and it could provide
something similar to your proposal and it could even be implemented in a
portable manner. Lots of work though.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
Could a struct with size 44 bytes point always points to a char array with size 2024 bytes? eagle_jyjh@citiz.net C++ 8 04-10-2006 03:05 PM
Could a struct with size 44 bytes point always points to a char array with size 2048 bytes? eagle_jyjh@citiz.net C Programming 5 04-09-2006 02:49 PM
On template method to obtain the size of an array. Was: [OT] Re: 2 stupid array questions!! Zygmunt Krynicki C++ 1 10-11-2003 01:18 AM



Advertisments