Checking the size of an array

N

Noah Roberts

The said:
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.
 
I

int2str

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
 
T

Thomas Tutone

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
 
T

The Cool Giraffe

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

Victor Bazarov

The said:
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
 
V

Vallabha

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

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
 
N

Noah Roberts

Victor said:
The said:
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.
 
V

Victor Bazarov

Noah said:
Victor said:
The said:
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
 
N

Noah Roberts

Victor said:
Noah said:
Victor said:
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 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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top