Wednesday, October 12, 2022

Digest for comp.lang.c++@googlegroups.com - 9 updates in 2 topics

Frederick Virchanza Gotham <cauldwell.thomas@gmail.com>: Oct 12 03:57PM -0700

Since I started programming in C/C++ back in the early 2000's, I never encountered a compiler with CHAR_BIT anything other than 8.
 
Well just now in the past 15 minutes, I was trying to get the code for the 'base58' algorithm to compile for the Texas Instruments F2809 microcontroller using the 'cl2000' compiler.
 
At first I was puzzled as to why 'sizeof(long)' was coming back as 2. Of course my first assumption here was that 'long' was 16-Bit (even though the Standard says that a 'long' must be at least 32-Bit). So I figured that this compiler was just non-standard-compliant on a few issues.
 
After a little messing around, I realised that CHAR_BIT is 16.
 
I looked through the compiler manual and found this paragraph:
 
"By ANSI/ISO C definition, the sizeof operator yields the number of bytes required to store an object. ANSI/ISO further stipulates that when sizeof is applied to char, the result is 1. Since the TMS320C28x char is 16 bits (to make it separately addressable), a byte is also 16 bits. This yields results you may not expect; for example, size of (int) = = 1 (not 2). TMS320C28x bytes and words are equivalent (16 bits). To access data in increments of 8 bits, use the __byte() and __mov_byte() intrinsics described in Section 7.6"
 
So there you go: There are C++ compilers in use today in the year 2022 with CHAR_BIT something other than 8.
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Oct 12 04:01PM -0700

On 10/12/2022 3:57 PM, Frederick Virchanza Gotham wrote:
 
> I looked through the compiler manual and found this paragraph:
 
> "By ANSI/ISO C definition, the sizeof operator yields the number of bytes required to store an object. ANSI/ISO further stipulates that when sizeof is applied to char, the result is 1. Since the TMS320C28x char is 16 bits (to make it separately addressable), a byte is also 16 bits. This yields results you may not expect; for example, size of (int) = = 1 (not 2). TMS320C28x bytes and words are equivalent (16 bits). To access data in increments of 8 bits, use the __byte() and __mov_byte() intrinsics described in Section 7.6"
 
> So there you go: There are C++ compilers in use today in the year 2022 with CHAR_BIT something other than 8.
 
At least is should not be less than eight?
JiiPee <kerrttuPoistaTama11@gmail.com>: Oct 12 11:54PM +0300

... also, how to you guys do this kind of situation:
 
int sum = 0;
for(int i = v.size() - 1; i >= 0; --i)
sum += i;
 
..so assuming we cannot change the type of i.
 
again types different, so would you do:
for(int i = static_cast<int>(v.size()) - 1; i >= 0; --i)
?
 
there are alot of place where vectors size() is assigned to an int. So
you always use static cast there?
JiiPee <kerrttuPoistaTama11@gmail.com>: Oct 12 11:55PM +0300

On 12/10/2022 23:54, JiiPee wrote:
 
> int sum  = 0;
> for(int i = v.size() - 1; i >= 0; --i)
>     sum  += i;
 
forgot to add that v is std::vector<int>.
Paavo Helde <eesnimi@osa.pri.ee>: Oct 13 12:27AM +0300

12.10.2022 23:48 JiiPee kirjutas:
 
> wherever this cast can be done? And what if CalcLength() can in some
> code places return a large number also? So we cannot just blindly change
> the return type to "short" to solve this.
 
Why on earth are you having local variables of type short?
 
I would fix this ASAP either by s/short/auto/ or s/short/size_t/.
scott@slp53.sl.home (Scott Lurndal): Oct 12 09:27PM


>This gives a warning: "warning, assigning size_t to short".
>I know this can be fixed:
>a = static_cast<short>(v.size());
 
so declare 'a' as size_t. Problem fixed.
Keith Thompson <Keith.S.Thompson+u@gmail.com>: Oct 12 02:30PM -0700


> but if we have hundreds of those lines, how would you fix this? Place
> a static cast in all of them? Of create some helper funktion to do
> this?
 
Why is `a` defined as a short and not as a size_t?
 
If there's a very good reason that `a` *needs* to be a short, it makes
sense to consider some kind of cast. If not, just make it a size_t.
 
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */
Paavo Helde <eesnimi@osa.pri.ee>: Oct 13 12:37AM +0300

12.10.2022 23:54 JiiPee kirjutas:
> ?
 
> there are alot of place where vectors size() is assigned to an int. So
> you always use static cast there?
 
int is a wrong type here. A correct one would be std::ptrdiff_t.
 
Anyway, even if I were convinced that int would be large enough in all
imagined circumstances, I still wouldn't use static_cast here, but e.g.
boost::numeric_cast, or at least my own debug_cast, which resolves to
boost::numeric_cast in debug builds and static_cast in release builds.
 
Note: boost::numeric_cast will throw an exception in case my reasoning
was wrong, which is a very good thing.
Ben Bacarisse <ben.usenet@bsb.me.uk>: Oct 12 11:26PM +0100

>> for(int i = v.size() - 1; i >= 0; --i)
>>     sum  += i;
 
> forgot to add that v is std::vector<int>.
 
... and presumably you meant sum += v[i]; or the loop is pointless!
 
And why run the indexes backwards? It might be needed for some vectors
of floating-point numbers, but not for int.
 
It seems that a lot of peculiar choices have been made in the code base.
 
--
Ben.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: