Re: "undefined behavior"?

Liste des GroupesRevenir à l c 
Sujet : Re: "undefined behavior"?
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 15. Jun 2024, 20:57:49
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4ko7d$3jbip$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
User-Agent : Mozilla Thunderbird
On 15/06/2024 00:35, Malcolm McLean wrote:
On 14/06/2024 22:29, Ben Bacarisse wrote:
Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
>
On 14/06/2024 12:44, Ben Bacarisse wrote:
Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
>
On 14/06/2024 00:55, Ben Bacarisse wrote:
Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
>
On 13/06/2024 19:01, bart wrote:
>
And here it just gets even uglier. You also get situations like this:
        uint64_t i=0;
        printf("%lld\n", i);
This compiles OK with gcc -Wall, on Windows64. But compile under Linux64
and it complains the format should be %ld. Change it to %ld, and it
complains under Windows.
It can't tell you that you should be using one of those ludicrous macros.
I've also just noticed that 'i' is unsigned but the format calls for
signed. That may or may not be deliberate, but the compiler didn't say
anything.
>
Exactly. We can't have this just to print out an integer.
This is how C works.  There's no point in moaning about it.  Use another
language or do what you have to in C.
>
In Baby X I provide a function called bbx_malloc(). It's is guaranteed
never to return null. Currently it just calls exit() on allocation failure.
But it also limits allocation to slightly under INT_MAX. Which should be
plenty for a Baby program, and if you want more, you always have big boy's
malloc.
And if you need to change the size?
>
But at a stroke, that gets rid of any need for size_t,
But sizeof, strlen (and friends like the mbs... and wcs... functions),
strspn (and friend), strftime, fread, fwrite. etc. etc. all return
size_t.
>
But these are not Baby X functions.
Neither is malloc but you wanted t replace that to get rid of the need
for size_t.
I confess that I am all at sea about what you are doing.  In essence, I
don't understand the rules of the game so I should probably just stop
commenting.
>
Yes, I really need to get that website together so that people cotton on to
what Baby X is, what it can and cannot do, and what is the point.
>
I know what Baby X is.  I don't know why "these are not Baby X
functions" applies to the ones I listed and not to malloc.
>
...
However if you need to pass a colour value to a fuction, you normall pass a
BBX_RGBA value, which is typedefed to unsigned long, and is opaque, and you
query the channels using the macros in bbx_color.h
>
#ifndef bbx_color_h
#define bbx_color_h
>
typedef unsigned long BBX_RGBA;
>
>
Curious.  The macros below seem to assume that int is 32 bits, so why
use long?
>
#define bbx_rgba(r,g,b,a) ((BBX_RGBA) ( ((r) << 24) | ((g) << 16) | ((b) <<
8) | (a) ))
>
This is likely to involve undefined behaviour when r >= 128.  (I presume
you are ruling out int narrower than 32 bits or there are other problems
as well.)
>
 No, it's been miswritten. Which is what I mean about C's integer types being a source of bugs. That code does not look buggy, but it is.
 
#define bbx_rgb(r, g, b) bbx_rgba(r,g,b, 255)
#define bbx_red(col) ((col >> 24) & 0xFF)
#define bbx_green(col) ((col >> 16) & 0xFF)
#define bbx_blue(col) ((col >> 8) & 0xFF)
#define bbx_alpha(col) (col & 0xFF)
>
It might not be an issue (as col is opaque and unlikely to be an
expression) but I'd still write (col) here to stop the reader having to
check or reason that out.
>
#define BBX_RgbaToX(col) ( (col >> 8) & 0xFFFFFF )
>
#endif
>
The last macro is to make it easier to interface with Xlib, and has the
prefix BBX_ (upper case) indicating that it is for internal use by the bbx
library / system and not meant for user programs.
>
As a reader of the code, I made exactly the reverse assumption.  When I
see lower-case macros I assume they are for internal use.
>
  They're function-like macros. Iterating over an rgba buffer is very processor-intensive, and so we do haave to compromise sfatety for speed here. All function-like symbols bbx_ are provided by Baby X for users, all symbols BBX_ have that prefix to reduce the chance of collisions with other code.
 
In this little exchange, there have been several points where your code is unclear, inefficient, non-portable or downright buggy, purely due to your insistence in using an outdated version of C.
If you want BBX_RGBA to be a typedef for an unsigned 32-bit integer, write:
typedef uint32_t BBX_RGBA;
If you want bbx_rgba() to be a function that is typesafe, correct, and efficient (for any decent compiler), write :
static inline BBX_RGBA bbx_rgba(uint32_t r, uint32_t g,
uint32_t b, uint32_t a)
{
return (r << 24) | (g << 16) | (b << 8) | a;
}
If you want your colour types to be "opaque", as you claimed, make it a struct with inline accessor functions.
Use static inline functions instead of function-like macros and you don't need the extra parenthesis round things (and you don't need to justify to readers why they are not there).  You can use small letter names without running contrary to common conventions.
Your insistence on hobbling your choice of language shows through in the poor quality of the code - or at least, the missed opportunities to make the code better and safer for both you and your users.

Date Sujet#  Auteur
12 Jun 24 * "undefined behavior"?77DFS
12 Jun 24 +* Re: "undefined behavior"?39Barry Schwarz
12 Jun 24 i`* Re: "undefined behavior"?38DFS
13 Jun 24 i `* Re: "undefined behavior"?37Keith Thompson
13 Jun 24 i  `* Re: "undefined behavior"?36DFS
13 Jun 24 i   `* Re: "undefined behavior"?35Keith Thompson
13 Jun 24 i    `* Re: "undefined behavior"?34Malcolm McLean
13 Jun 24 i     +- Re: "undefined behavior"?1Ben Bacarisse
13 Jun 24 i     +* Re: "undefined behavior"?29bart
13 Jun 24 i     i+* Re: "undefined behavior"?22Malcolm McLean
13 Jun 24 i     ii+* Re: "undefined behavior"?2Chris M. Thomasson
14 Jun 24 i     iii`- Re: "undefined behavior"?1Malcolm McLean
14 Jun 24 i     ii`* Re: "undefined behavior"?19Ben Bacarisse
14 Jun 24 i     ii `* Re: "undefined behavior"?18Malcolm McLean
14 Jun 24 i     ii  `* Re: "undefined behavior"?17Ben Bacarisse
14 Jun 24 i     ii   +* Re: "undefined behavior"?13Malcolm McLean
14 Jun 24 i     ii   i+* Re: "undefined behavior"?4Richard Harnden
14 Jun 24 i     ii   ii`* Re: "undefined behavior"?3Malcolm McLean
14 Jun 24 i     ii   ii `* Re: "undefined behavior"?2bart
14 Jun 24 i     ii   ii  `- Re: "undefined behavior"?1Malcolm McLean
14 Jun 24 i     ii   i`* Re: "undefined behavior"?8Ben Bacarisse
15 Jun 24 i     ii   i `* Re: "undefined behavior"?7Malcolm McLean
15 Jun 24 i     ii   i  +- Re: "undefined behavior"?1Ben Bacarisse
15 Jun 24 i     ii   i  `* Re: "undefined behavior"?5David Brown
15 Jun 24 i     ii   i   `* Re: "undefined behavior"?4Richard Harnden
16 Jun 24 i     ii   i    +- Re: "undefined behavior"?1Ben Bacarisse
16 Jun 24 i     ii   i    `* Re: "undefined behavior"?2David Brown
16 Jun 24 i     ii   i     `- Re: "undefined behavior"?1Malcolm McLean
14 Jun 24 i     ii   `* Re: "undefined behavior"?3Chris M. Thomasson
14 Jun 24 i     ii    `* Re: "undefined behavior"?2Ben Bacarisse
15 Jun 24 i     ii     `- Re: "undefined behavior"?1Chris M. Thomasson
14 Jun 24 i     i`* Re: "undefined behavior"?6Keith Thompson
14 Jun 24 i     i +- Re: "undefined behavior"?1bart
14 Jun 24 i     i +* Re: "undefined behavior"?3David Brown
14 Jun 24 i     i i`* Re: "undefined behavior"?2Keith Thompson
15 Jun 24 i     i i `- Re: "undefined behavior"?1David Brown
14 Jun 24 i     i `- Re: "undefined behavior"?1Keith Thompson
13 Jun 24 i     `* Re: "undefined behavior"?3Keith Thompson
14 Jun 24 i      `* Re: "undefined behavior"?2Malcolm McLean
14 Jun 24 i       `- Re: "undefined behavior"?1Keith Thompson
12 Jun 24 +* Re: "undefined behavior"?15David Brown
13 Jun 24 i+* Re: "undefined behavior"?6Keith Thompson
13 Jun 24 ii+* Re: "undefined behavior"?2David Brown
14 Jun 24 iii`- Re: "undefined behavior"?1Keith Thompson
19 Jun 24 ii`* Re: "undefined behavior"?3Tim Rentsch
19 Jun 24 ii `* Re: "undefined behavior"?2Keith Thompson
22 Jun 24 ii  `- Re: "undefined behavior"?1Tim Rentsch
13 Jun 24 i`* Re: "undefined behavior"?8DFS
13 Jun 24 i +* Re: "undefined behavior"?4Ike Naar
13 Jun 24 i i`* Re: "undefined behavior"?3DFS
13 Jun 24 i i `* Re: "undefined behavior"?2Lew Pitcher
13 Jun 24 i i  `- Re: "undefined behavior"?1DFS
13 Jun 24 i `* Re: "undefined behavior"?3David Brown
14 Jun 24 i  `* Re: "undefined behavior"?2Keith Thompson
14 Jun 24 i   `- Re: "undefined behavior"?1David Brown
12 Jun 24 +* Re: "undefined behavior"?19Janis Papanagnou
13 Jun 24 i`* Re: "undefined behavior"?18Keith Thompson
13 Jun 24 i +* Re: "undefined behavior"?2Janis Papanagnou
13 Jun 24 i i`- Re: "undefined behavior"?1David Brown
13 Jun 24 i `* Re: "undefined behavior"?15David Brown
13 Jun 24 i  `* Re: "undefined behavior"?14DFS
14 Jun 24 i   `* Re: "undefined behavior"?13David Brown
15 Jun 24 i    +* Re: "undefined behavior"?11DFS
15 Jun 24 i    i`* Re: "undefined behavior"?10Keith Thompson
15 Jun 24 i    i `* Re: "undefined behavior"?9DFS
15 Jun 24 i    i  `* Re: "undefined behavior"?8Keith Thompson
15 Jun 24 i    i   `* Re: "undefined behavior"?7DFS
15 Jun 24 i    i    +* Re: "undefined behavior"?2Janis Papanagnou
15 Jun 24 i    i    i`- Re: "undefined behavior"?1DFS
15 Jun 24 i    i    +- Re: "undefined behavior"?1James Kuyper
15 Jun 24 i    i    +- Re: "undefined behavior"?1Keith Thompson
15 Jun 24 i    i    +- Re: "undefined behavior"?1bart
15 Jun 24 i    i    `- Re: "undefined behavior"?1David Brown
15 Jun 24 i    `- Re: "undefined behavior"?1David Brown
12 Jun 24 +- Re: "undefined behavior"?1Keith Thompson
13 Jun 24 +- Re: "undefined behavior"?1bart
13 Jun 24 `- Re: "undefined behavior"?1Bonita Montero

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal