473,388 Members | 1,220 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,388 software developers and data experts.

Checking the size of an array

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)
Feb 14 '07 #1
8 5634
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.
Feb 14 '07 #2
On Feb 14, 1:06 pm, "The Cool Giraffe" <giraf...@viltersten.com>
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

Feb 14 '07 #3
On Feb 14, 4:06 pm, "The Cool Giraffe" <giraf...@viltersten.com>
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<intv;
v.push_back(7);
v.push_back(-3);
v.push_back(6);
v.push_back(12);
// etc.
int theSize = v.size();
}

Best regards,

Tom


Feb 14 '07 #4
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
Feb 14 '07 #5
On Feb 15, 2:06 am, "The Cool Giraffe" <giraf...@viltersten.com>
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

Feb 15 '07 #6
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.
Feb 15 '07 #7
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
Feb 15 '07 #8
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.
Feb 15 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Oeln | last post by:
If I want to check for input of an integer I've got the following (I get the form input with $input = "$_POST"): if(!ereg("^+$",$_POST)) { echo "Input is incomplete or incorrect."; } If,...
2
by: Tek9_AK | last post by:
I have a function which reads a csv file into an array from there I want to check if a value is in it. Basically the csv files look like this: 23123233123,name,some@one.com...
7
by: Jim Cook | last post by:
We have a macro which takes various index constants as an argument and offsets into an array. The macro can be an Lvalue or Rvalue. The index is not zero based. I would like a compile time error...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
21
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
14
by: Urs Thuermann | last post by:
What is the most elegant way to check certain conditions at compile time? I.e. I want a compile time error to be generated if for example the size of a struct is not a multiple of 4 or if one...
3
by: john | last post by:
Hey, I know we use the pointer this to obtain a class object or class member data. I don't follow the reason for example this code. I'am quite confused assingment operator const B...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
11
by: Bryan Crouse | last post by:
I am looking a way to do error checking on a string at compile time, and if the string isn't the correct length have then have the compiler throw an error. I am working an embedded software that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.