Dimension of a matrix - Page 3

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

Translate This Thread From English to

Threaded View
Re: Type-safety and C++, was: Re: Dimension of a matrix
On Mon, 27 Mar 2017 23:42:08 -0700, Paul Rubin wrote:

Quoted text here. Click to load it

This _is_ an embedded group.

When I'm writing desktop code I'm more prone to use 'at', or otherwise  
actively test for overrunning boundaries.

--  

Tim Wescott
Wescott Design Services
We've slightly trimmed the long signature. Click to see the full one.
Re: Dimension of a matrix
On Thu, 23 Mar 2017 23:21:07 -0700, Paul Rubin wrote:

Quoted text here. Click to load it

I know what I'm doing.  I just want to know if the oft-used #DIM  
construct has ever been formalized.

--  
Tim Wescott
Control systems, embedded software and circuit design
We've slightly trimmed the long signature. Click to see the full one.
Re: Dimension of a matrix
On 24/03/17 17:05, Tim Wescott wrote:
Quoted text here. Click to load it

The short answer is "no".


Re: Dimension of a matrix
On Fri, 24 Mar 2017 17:33:58 +0100, David Brown wrote:

Quoted text here. Click to load it

Thanks.

--  
Tim Wescott
Control systems, embedded software and circuit design
We've slightly trimmed the long signature. Click to see the full one.
Re: Dimension of a matrix
Tim Wescott wrote:
Quoted text here. Click to load it

It has not been formalized to my knowledge. Which is curious.

--  
Les Cargill

Re: Dimension of a matrix
Paul Rubin wrote:
Quoted text here. Click to load it

It might and it might not be. There are cases where a
(hacky C-style) library may expect a contiguous block of
some type.

--  
Les Cargill

Re: Dimension of a matrix
On 24/03/17 00:17, Tim Wescott wrote:
Quoted text here. Click to load it

This is the common way to define such a macro.  It does not work if you
pass arrays around, of course - they decay into pointers when you pass
them as function pointers (unless your array is wrapped in a struct).

Quoted text here. Click to load it


Re: Dimension of a matrix
David Brown wrote:
Quoted text here. Click to load it

I could not tell you which versions of which compilers support this,
but in some toolchains, it's possible to specify the size of an array  
being passed - void p(char x[256])... .

I may be thinking of compiling C code using a C++ compiler, or  it may  
be in C99. I don't think it's in C89/ANSI.

Quoted text here. Click to load it

--  
Les Cargill

Re: Dimension of a matrix
Am 25.03.2017 um 19:12 schrieb Les Cargill:
Quoted text here. Click to load it


All C compilers are actually required to allow you to pass a size like  
that.  It doesn't have any effect whatsoever, though.  Any number you  
put between those [] is really just decoration.  These 4 function  
declarations are totally equivalent:

    void p(char x[256]);
    void p(char x[7]);
    void p(char x[]);
    void p(char *x);



Re: Dimension of a matrix

Quoted text here. Click to load it

I recall (vaguely) code where sizeof(x) would return 256, but it's a  
hazy memory. I don't remember the toolchain; probably latter day MS
VS stuff.

And even if it's just decoration, it preserves the size of the array out  
of band.

--  
Les Cargill

Re: Dimension of a matrix
On 25/03/17 21:17, Les Cargill wrote:

Quoted text here. Click to load it

That would be a broken compiler.

Quoted text here. Click to load it

Specifying the size here is just a comment.  And it is a dangerous  
comment, because it /looks/ like it might be something that the compiler  
will check, or that lets you use "sizeof" in the manner you describe -  
but it will not.  In all the cases above, "x" is a pointer to char -  
/not/ an array.


Re: Dimension of a matrix
David Brown wrote:
Quoted text here. Click to load it

Quoted text here. Click to load it

It might have been a C++ toolchain. I don't recall.

Those are out there for sure - although given all the furor over
Blind Pointers Will Be The End of Western Civilization these days....

Quoted text here. Click to load it

It is and it isn't.

Quoted text here. Click to load it

As a programmer, you have to check what things are.

--  
Les Cargill



Re: Dimension of a matrix
On 26/03/17 18:55, Les Cargill wrote:
Quoted text here. Click to load it

Quoted text here. Click to load it

Section 6.7.6.3 "Function declarators (including prototypes)" of the C  
standards has paragraph 7:

    A declaration of a parameter as ??array of type?? shall be adjusted  
to ??qualified pointer to
    type??, where the type qualifiers (if any) are those specified  
within the [ and ] of the
    array type derivation. If the keyword static also appears within the  
[ and ] of the
    array type derivation, then for each call to the function, the value  
of the corresponding
    actual argument shall provide access to the first element of an  
array with at least as many
    elements as specified by the size expression.

Using an array as a parameter type has /exactly/ the same effect as  
using a pointer.  Applying "sizeof" to it returns the size of a pointer.

C++ has the same thing (but without the option is using "static").  The  
wording in C++14 section 8.3.5 paragraph 5 is different but gives the  
same results.

As a programmer, you absolutely should /not/ have to check that the  
compiler follows the fundamental rules of the language you are using.  A  
compiler - C or C++ - which gives the "sizeof" a C array parameter as  
the sizeof the array rather than the size of the pointer, is /broken/.  
This is not an option, or a feature, or something that some compilers do  
- it is how C and C++ arrays work.  And it is part of the reason for the  
introduction of std::array in C++.



Re: Dimension of a matrix
David Brown wrote:
Quoted text here. Click to load it
<snip>
Quoted text here. Click to load it

The rules may be opaque - one toolchain had been rebuilt with the  
default char signedness set to unsigned char. Maybe it's just me,
but I have to check. It doesn't take too long.

I have seen so many toolchains, some of which have bizarre extensions...

Quoted text here. Click to load it

That unfortunately does not mean they do not exist.

Quoted text here. Click to load it
--  
Les Cargill



Re: Dimension of a matrix
Quoted text here. Click to load it

That's another annoyance of mine. :-)

If I had my way, integers would be unsigned by default in all of today's
programming languages and you would have to ask for a signed integer if
you wanted one. As it is, all of my variables are declared as unsigned,
regardless of language (assuming the language supports it), unless I need
a signed one.

Simon.

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

Re: Dimension of a matrix
Quoted text here. Click to load it

What happens if you subtract 3 from 2?  Are these unsigneds supposed to
represent natural numbers, so you throw an exception if you get a
negative result?  Or are you saying all languages should use twos
complement wraparound arithmetic including the ones that have exact
integers now?

Re: Dimension of a matrix
On 30/03/17 04:55, Paul Rubin wrote:
Quoted text here. Click to load it

Presumably he uses signed integers in situations where it makes sense to
subtract 3 from 2, and in other situations he simply does not subtract 3
from 2.  Making your "noOfOranges" variable signed does not let you
subtract 3 oranges from 2 oranges.


Re: Dimension of a matrix
Quoted text here. Click to load it

If you think your variable might go negative, you use a signed integer
otherwise you use an unsigned integer.

Quoted text here. Click to load it

Exactly. You pick the data type which most closely models the problem
you are trying to solve. Picking a signed integer when you know your
data is always going to be positive is not a good match if unsigned
integers are available in your chosen language.

In my code (both work and hobbyist) I use unsigned values vastly
more frequently than I do signed values and another reason is that
signed integers have caused security issues in the past.

Simon.

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

Re: Dimension of a matrix
On 30/03/17 02:36, Les Cargill wrote:
Quoted text here. Click to load it

Some toolchains have plain char as signed, others as unsigned.  The
target ABI might specify it, it might not.  On the same platform,
different compiler versions might swap the default.  Compiler flags can
change the default.

But it should never matter - if your code is affected by the signedness
of plain "char", your code is /bad/.  If you need a signed char, use
"signed char" (or, more usually, int8_t).  If you need an unsigned char,
use "unsigned char" (or uint8_t).

For most of my targets and tools, I don't even know whether a plain char
is signed or unsigned - and that is the way it should be.

This is not a matter of "opaque rules", "strange bugs", "extensions", or
"compilers that don't follow the standards".  The C standards are
perfectly clear on this matter, and the rules are simple.  Some people
misunderstand the rules, and think that plain char is always signed -
but it is /your/ job, as a professional programmer, to learn the rules
correctly.

Quoted text here. Click to load it

Extensions are fine.  If you want to use a toolchain's extensions, you
read the docs for the toolchain and you use them.

But you are suggesting that some compilers - a relatively modern version
of MS VC, no less - arbitrarily break a simple and fundamental part of
standard C that was worked the same way since the first K&R book, and
gives no conceivable advantage by being broken.  I simply do not believe
this happened - I think your test code has a bug, or you misinterpreted
it, or misremembered the problem.

Quoted text here. Click to load it

There are compilers with bugs - that is certainly true.  There are a few
compilers sold commercially that have serious bugs that you have a
realistic chance of hitting.

But you are suggesting a design decision here to actively work in a way
contrary to the C standards.  That is much rarer.  It is not impossible,
but it is much rarer.  I can think of four cases that I know about where
compiler manufacturers have made such decisions - in each case it is
because they thought the change gave significant benefits to the
developers and made the developers' work easier.  In every case, they
were wrong, because breaking the standards means it is harder to write
correct code.  But the manufacturers had good reasoning for their decisions.

Making sizeof an array parameter act differently from the standards
would be a stupid idea.  People rarely use array parameters (rather than
pointers) anyway, and even more rarely apply "sizeof" to them.  It would
break the standards for no reason, and greatly complicate the use of
array parameters.  It would not happen except perhaps on a "toy"
compiler made by complete amateurs (and while such tools do exist, this
is /not/ going to be the first symptom you find).

Quoted text here. Click to load it


Re: Dimension of a matrix
Op 24-Mar-17 om 00:17 schreef Tim Wescott:
Quoted text here. Click to load it

Just for my curiosity, why would you want to do that? You don't need it  
for iteration.

Maybe to make a same-sized temporary array? But then it would make sense  
to name the array type, and use that type. But maybe for a same-sized  
array of a different element type. But I think I could make a template  
to do that.

And if you do need it, if the array declaration is in scope, I assume  
that it was defined with some named literal dimension, so why not used that?

Wouter "Objects? No Thanks!" van Ooijen


Site Timeline