Dimension of a matrix

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
I just put the following into yet another c/c++ file:

#define DIM(x) (sizeof(x) / sizeof(*x))

It calculates the number of elements in the array x (assuming that x is,  
indeed an array).

Is there anything in the standard libraries that does this easy and  
obvious thing?

--  

Tim Wescott
Wescott Design Services
We've slightly trimmed the long signature. Click to see the full one.
Re: Dimension of a matrix
Quoted text here. Click to load it

In full-fat C++ systems it's preferable to use STL containers for arrays
instead of using hacky C-style pointers to simulate them.  On embedded
devices STL might be considered too much bloat though.

Re: Dimension of a matrix
On 24/03/17 07:21, Paul Rubin wrote:
Quoted text here. Click to load it

std::array should involve no "bloat" whatsoever.  It's basically a
type-safe wrapper around C arrays, with the dimension easily available.


Type-safety and C++, was: Re: Dimension of a matrix
Quoted text here. Click to load it

Well, "type-safe" if you ignore the little fact that the normal
indexing operator "[]" is not bounds checked. :-)

In the C++ library, it would be nice if the indexing operator most
people would use by default, "[]", was the bounds checked one and
the alternative indexing operator, "at()", was the non-bounds checked
one. That way, you would have to do something unusual in your code
to turn _off_ bounds checking instead of having to do something
unusual in your code to turn _on_ bounds checking...

Simon.

--  
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
Microsoft: Bringing you 1980s technology to a 21st century world

Re: Type-safety and C++, was: Re: Dimension of a matrix
On Fri, 24 Mar 2017 18:24:44 +0000, Simon Clubley wrote:

Quoted text here. Click to load it

Even better (for me) would be if there were a define that could turn  
bounds checking on -- that way I could do testing with bounds checking  
on, and release (presumably faster) code with bounds checking off.

--  
Tim Wescott
Control systems, embedded software and circuit design
We've slightly trimmed the long signature. Click to see the full one.
Re: Type-safety and C++, was: Re: Dimension of a matrix
Am 25.03.2017 um 01:32 schrieb Tim Wescott:
Quoted text here. Click to load it

Then it would again be "bloat" compared to a regular array...

Quoted text here. Click to load it

As far as I understand, such a thing would be a legal C++ implementation
(bounds violations are undefined behaviour, which means an
implementation could define it as "raises an exception" or "writes a
coredump"), so bug your compiler maker until they implement that :-)
(Don't we already have some "checked" STL implementations?)


  Stefan

Re: Type-safety and C++, was: Re: Dimension of a matrix
Tim Wescott wrote:
Quoted text here. Click to load it


I have added functions to systems that return yes/no to the question "is  
this index in range?". Those can then be disabled after test.

It'd be nice to have this be built in, but it's hardly necessary.

--  
Les Cargill

Re: Type-safety and C++, was: Re: Dimension of a matrix
On 24/03/17 19:24, Simon Clubley wrote:
Quoted text here. Click to load it

The types are safe - it's just the ranges that are not...

Quoted text here. Click to load it

Yes, I think that would be a sensible idea.  In many cases, the compiler  
can remove the run-time overhead of the checks.




Re: Type-safety and C++, was: Re: Dimension of a matrix
On 25/03/17 11:57, David Brown wrote:
Quoted text here. Click to load it

Beyond the bounds, you're looking at something which is
probably not the type you think you're looking at. That
is pretty-much the definition of "not type-safe".


Re: Type-safety and C++, was: Re: Dimension of a matrix
Quoted text here. Click to load it

Some people would disagree with that definition of "type-safe". :-)

Quoted text here. Click to load it

And that's exactly why they would disagree. If the bounds are
not enforced, then it's not type-safe IMHO.

Simon.

--  
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
Microsoft: Bringing you 1980s technology to a 21st century world

Re: Type-safety and C++, was: Re: Dimension of a matrix
On 27/03/17 01:53, Simon Clubley wrote:
Quoted text here. Click to load it

Type safety is primarily a compile-time feature.  The aim of type safety
is to stop you trying to use the wrong "type" of "thing" (objects,
classes, types, whatever) in the wrong place.  And by restricting the
operations you can do on your "things", you spot more mistakes at
compile-time.  A vital point is that type safety checks are free with
respect to run-time costs.  And your aim is to stop certain classes of
error being /possible/.

Checking the bounds of array accesses is primarily a run-time feature.
There are run-time safety checks you can make - they are not type safety
checks.  They involve some run-time overhead, and you need run-time
methods of dealing with detected errors - your aim is to detect certain
classes of error when they happen, and stop them leading to worse problems.

Sometimes there is a middle ground.  The compiler may be able to check,
at compile time, whether an array access is always safe, possibly safe,
or always bad.  It is certainly a good thing if the compiler can warn
you on third case, and omit any run-time checks in the first case.

You can argue that a std::array type is a type with a fixed known size,
and therefore should not allow any access beyond its bounds.  But
actually it is defined as a type that wraps a C array with no run-time
overheads for normal use, without run-time checking of bounds.  It gives
you methods for accessing the data, but there is a pre-condition on the
[] operator that you are within the bounds of the array.  This is a
perfectly good way to define a type and its operations - type-safety
does /not/ require run-time checking that pre-conditions to methods are
met.  That is the responsibility of the code using the type.

Of course one could make a type for which bounds checking was part of
the [] operator method specification.  But even then, the check is not
part of the type safety system because the index is not part of a type,
and the check is not at compile-time.  If you want that kind of type
safety, use a std::tuple and get<i> functions for access - /those/
accesses are type checked.



Re: Type-safety and C++, was: Re: Dimension of a matrix
On 27/03/17 18:51, David Brown wrote:
Quoted text here. Click to load it

Sorry, but you can't just redefine "type safety" to mean "static
type safety" and exclude "dynamic type safety".

I get it, static >> dynamic, but they're both about type safety.

Clifford Heath.

Re: Type-safety and C++, was: Re: Dimension of a matrix
On 27/03/17 11:19, Clifford Heath wrote:
Quoted text here. Click to load it

Fair enough, I suppose.  C++ supports dynamic type safety too, such as
the dynamic_cast operator.  But static type safety is the main mechanism
for type safety in C++.

std::array is defined to be a type with greater static type safety
compared to standard C/C++ arrays - it knows its size, does not decay to
a pointer, etc.  It is not defined to have run-time bounds checking by
default - but you have the option if you want (with the "at" method).

Type safety - dynamic or static - does not mean preventing any possible
errors.  It merely means making it hard to accidentally make errors from
mixing up types - and it is a balance between safety, convenience (in
writing the code) and efficiency (code size and speed at run-time).  It
is not an absolute term (at least, not in real-world programs in
real-world programming languages).  It would be fair to say that an
array container that always checked its bounds is /safer/ than
std::array, and is more "type safe".  But it could never be completely
type safe - not in C++, where you have reinterpret_cast, unions, void*,
memcpy, and all sorts of other ways to cheat on type safety.



Re: Type-safety and C++, was: Re: Dimension of a matrix
Quoted text here. Click to load it

I use .at() all the time now.  Every time I've tested it both ways in a
real program, the checks didn't make any noticible slowdown.  It could
be that the compiler is able to hoist them out of lots of loops.

Re: Type-safety and C++, was: Re: Dimension of a matrix
On Fri, 24 Mar 2017 18:24:44 -0000 (UTC), Simon Clubley

Quoted text here. Click to load it

Agreed.  However, that would violate the C/C++ mantra that those who
do not want a feature should not have to pay for it.  

Strict bounds checking - particularly checks per dimension as opposed
to a single check that the overall target lies within the array -
would break some existing code.  
[For the same reasons that pointers may be one-of before or after the
array, and iterators may be considered to point "between" elements.]

George

Re: Type-safety and C++, was: Re: Dimension of a matrix
On Fri, 24 Mar 2017 23:14:36 -0400, George Neuner wrote:

Quoted text here. Click to load it

We're talking about the C++ STL "array" class, which is defined as having  
a certain size, with no guarantees about what lies outside the range of  
the actual data.

Bounds checking on the C '[]' operator would probably break some code,  
for the reasons you mention.

--  
Tim Wescott
Control systems, embedded software and circuit design
We've slightly trimmed the long signature. Click to see the full one.
Re: Type-safety and C++, was: Re: Dimension of a matrix
wrote:

Quoted text here. Click to load it

Yes. But not every array in C++ uses the STL class, and I was reacting
to Simon's comment about the '[]' index operator.  

It would be hopelessly confusing if the index operator of the array
class was checked, but the general (C compatible) index operator was
not.  If the general operator were checked, undoubtedly some code
would break, and even though STL arrays are heap allocated with some
overrun slop, I suspect that checking the operator there also would
break some existing code.
[Not that breaking crappy code is a bad thing ... 8-) ]

YMMV,
George

Re: Type-safety and C++, was: Re: Dimension of a matrix
Op 26-Mar-17 om 11:45 schreef George Neuner:
Quoted text here. Click to load it

There are no such things as 'STL arrays', and the ones you probably talk  
about (containers?) are not guranteed to have overrun 'slop' (room  
before and after?).

Wouter "Objects? No Thanks!" van Ooijen


Re: Type-safety and C++, was: Re: Dimension of a matrix
Quoted text here. Click to load it

I believe we've been using the term to refer to this:

  http://en.cppreference.com/w/cpp/container/array

Re: Type-safety and C++, was: Re: Dimension of a matrix
Op 26-Mar-17 om 20:08 schreef Paul Rubin:
Quoted text here. Click to load it

That type of array has nothing to do with heap storage, and it has zero  
overhead.

(In contrast to for instance std::vector and other dynamically-sized  
array-like things, which do use the heap.)

Wouter "Objects? No Thanks!" van Ooijen


Site Timeline