Author Topic: 2 byte value into 2 separate 1 byte values  (Read 9439 times)

0 Members and 1 Guest are viewing this topic.

Online gf

  • Super Contributor
  • ***
  • Posts: 1169
  • Country: de
Re: 2 byte value into 2 separate 1 byte values
« Reply #50 on: July 20, 2021, 08:00:55 pm »
sizeof(char) returns 2 on DSP43.
never trust that "char" is 1 byte

That absolutely violates the C standard -- sizeof(char) is *defined* to be 1.
Not 1 byte. Just 1. Whatever size a char is, is the unit you measure other things in.

Mostly correct, but not exactly.

sizeof operator
Quote
...
Returns the size, in bytes, of the object representation...
...

sizeof(char) is indeed alwas 1 (byte), but a byte (in terms of C) is not necessarily required to have a size of eight bits (for instance if the smallest addressable unit of memory in a particular computer has a different size).

Arithmetic types
Quote
...
1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
...
Note: this allows the extreme case in which bytes are sized 64 bits, all types (including char) are 64 bits wide, and sizeof returns 1 for every type.
...

[ cppreference.com is admittedly not the official C standard document, but I'd be surprised if the latter woud say anything different. ]
« Last Edit: July 20, 2021, 08:04:42 pm by gf »
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14464
  • Country: fr
Re: 2 byte value into 2 separate 1 byte values
« Reply #51 on: July 20, 2021, 09:48:11 pm »
Yes.
C defines the byte like so:
Quote
byte
addressable unit of data storage large enough to hold any member of the basic character
set of the execution environment
NOTE 1 It is possible to express the address of each individual byte of an object uniquely.
NOTE 2 A byte is composed of a contiguous sequence of bits, the number of which is implementation-
defined.  The least significant bit is called the low-order bit; the most significant bit is called the high-order
bit.

But sizeof(char) returning 2 would indeed be completely non-compliant.

Now of course one may question this definition of "byte" used in the C standard, as it's otherwise widely recognized as a word made of 8 bits, but the definition is given and still clear.
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 1209
  • Country: pl
Re: 2 byte value into 2 separate 1 byte values
« Reply #52 on: July 20, 2021, 11:00:44 pm »
2. In some situations, when strict aliasing rules are in effect, memcpy is the only safe way to transfer multi-byte POD structure and preserve layout.
Not only memmove (or the old memcpy). Any access through a char* and (un)signed versions is always valid, as those are an exception to the aliasing rules.(1) And I doubt that exception is likely to disappear, as language’s internal consistency depends on it. Otherwise all the guarantees WRT byte representation would become meaningless.

sizeof(char) returns 2 on DSP43.
never trust that "char" is 1 byte
I believe everyone here assumes the compiler works properly, as not making that assumption adds a whole new level of complexity that can’t even be dealt with without specifying the exact situation and compiler.
____
(1) 9899:2011 6.5§7
People imagine AI as T1000. What we got so far is glorified T9.
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: 2 byte value into 2 separate 1 byte values
« Reply #53 on: July 21, 2021, 06:40:10 am »
I believe everyone here assumes the compiler works properly, as not making that assumption adds a whole new level of complexity that can’t even be dealt with without specifying the exact situation and compiler.

The Taxas Instrument one? Yes, for the DSP43 it works good, just you have to deal with some quirks. It took me over a week to understand why my imported code didn't work as expected, then I found that sizeof(char) returns 2, and ... frankly it was more frustrating than shocking: you cannot change the toolchain, you cannot modify it, and you find that in the meanwhile the managers in the US have already approved the toolchain for the job (without knowing anything  :palm: ), so you cannot say "hey? it violates this and that ... do ... do you hear me? ... ", who cares? You have been hired for the job, now you have to deal with it, so you can only push your C-quirks under a layer of assembly and go on.

C can be frustrating, this is the typical example.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: nz
Re: 2 byte value into 2 separate 1 byte values
« Reply #54 on: July 21, 2021, 06:47:57 am »
C can be frustrating, this is the typical example.

That is *not* C.

It might look very like C, but it's not C.
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: 2 byte value into 2 separate 1 byte values
« Reply #55 on: July 21, 2021, 06:54:33 am »
Quote
specifying the exact situation and compiler

that's why our projects all come with a project builder, which checks if the installed toolchain and ecosystem is qualified for the job

Code: [Select]
# b

   firmware-5.128(ppc64.le/f2)

0) clean
1) configure
2) compile
3) analyze
4) ICE@192.168.1.21
5) misc

name: firmware-5.128-64bit-f2
image: elf
note: engineering version
qualified_host: passed
qualified_toolchain: passed

All the binaries of the toolchain have a certificate, you cannot alter a single bit of them, and "qualified" means someone has tested and checked them, and signed a certificate under his/her name. This is also why I couldn't "hack" or change the Texas Instruments' toolchain.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: 2 byte value into 2 separate 1 byte values
« Reply #56 on: July 21, 2021, 07:04:18 am »
In 25 years of experience, the only honest company that said "my partner's tool is not C but C-like" was Motorola, back in 1996, when ICC11 was released for a MIT project.
90% of its syntax was C-like, but
- pointers were different and limited
- structures and unions were guaranteed to not be stuffed or padded
- etc
I remember they warned the users, inviting them to carefully read the manual, with a quick section which briefly summarized the most important differences

Well done  :-+
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 1209
  • Country: pl
Re: 2 byte value into 2 separate 1 byte values
« Reply #57 on: July 21, 2021, 09:23:29 am »
then I found that sizeof(char) returns 2
And that is a bug. sizeof is defined to return the number of char elements that hold its argument. Therefore sizeof(char) can by definition be 1 and 1 only. If a compiler, claiming to be a C compiler, fails to properly undestand valid C code, it’s a bug. Period.
People imagine AI as T1000. What we got so far is glorified T9.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14464
  • Country: fr
Re: 2 byte value into 2 separate 1 byte values
« Reply #58 on: July 21, 2021, 04:16:25 pm »
C can be frustrating, this is the typical example.
That is *not* C.
It might look very like C, but it's not C.

Yep indeed.
That's kind of funny. As we said in other threads, C users tend to consider C as something you don't need to learn - and thus largely ignore standards - but some tool vendors (fortunately, not many) do the exact same.

Poor C... so mistreated. :(
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8172
  • Country: fi
Re: 2 byte value into 2 separate 1 byte values
« Reply #59 on: July 21, 2021, 04:24:54 pm »
No compiler is completely perfect and bug-free but they still can be called "C compilers".

But indeed sizeof(char)==2 is so elemental issue breaking such important base assumption behind the C language that it will have serious consequences in most codebases, and will make most developer's brain spin around. This needs a lot of working around.

So yes, you can assume sizeof(char)==1 by default. Such exceptions to this rule are super special, likely with that would be the only compiler in the world that does that. Don't waste your time always questioning everything, you need to be able to trust some basic things like table salt being table salt when the package says so, you don't send it to a lab every time you fix something to eat just in case this particular table salt batch accidentally ships some poison which happens to taste like table salt.
 
The following users thanked this post: DiTBho

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14464
  • Country: fr
Re: 2 byte value into 2 separate 1 byte values
« Reply #60 on: July 28, 2021, 04:37:17 pm »
No compiler is completely perfect and bug-free but they still can be called "C compilers".

Vendors can call their compilers however they like.
If they are not clearly stating which C standard they are compliant with (or they do, but they are actually not compliant, which is way worse), then I'll either not use the compiler, or if I have no choice, I'll treat it as a compiler for a language that I don't know, and will thoroughly read the manual.
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 151
  • Country: us
Re: 2 byte value into 2 separate 1 byte values
« Reply #61 on: July 28, 2021, 06:14:38 pm »
sizeof(char) returns 2 on DSP43.

I find it hard to believe. Any reference to the compiler docs?
 

Online PlainName

  • Super Contributor
  • ***
  • Posts: 6837
  • Country: va
Re: 2 byte value into 2 separate 1 byte values
« Reply #62 on: July 28, 2021, 07:16:31 pm »
Quote
... and will thoroughly read the manual.

How quaint  ;)

Won't be long until that becomes "and watched half a youtube video" out of necessity. :(
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf