Monday, November 2, 2020

Digest for comp.lang.c++@googlegroups.com - 25 updates in 5 topics

olcott <NoOne@NoWhere.com>: Nov 02 10:31AM -0600

Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27026.1 for x86
requires the following header or bool function_name() will not compile:
#include <stdbool.h>
 
I am guessing the this is simply a Microsoft screw up because online
documentation seems to indicate that bool is a primitive data type in
recent standards for the C programming language.
 
 
--
Copyright 2020 Pete Olcott
 
"Great spirits have always encountered violent opposition from mediocre
minds." Einstein
Bonita Montero <Bonita.Montero@gmail.com>: Nov 02 05:35PM +0100

Am 02.11.2020 um 17:31 schrieb olcott:
> I am guessing the this is simply a Microsoft screw up because online
> documentation seems to indicate that bool is a primitive data type in
> recent standards for the C programming language.
 
AFAIK bool isn't a native type in C but you have to include <stdbool.h>.
In C++, bool is a native type.
olcott <NoOne@NoWhere.com>: Nov 02 10:39AM -0600

On 11/2/2020 10:35 AM, Bonita Montero wrote:
>> recent standards for the C programming language.
 
> AFAIK bool isn't a native type in C but you have to include <stdbool.h>.
> In C++, bool is a native type.
 
Native Data Types
Special type: bool (has values true and false)
 
https://www.cs.fsu.edu/~lacher/lectures/Output/cppreview/slide02.html
 
--
Copyright 2020 Pete Olcott
 
"Great spirits have always encountered violent opposition from mediocre
minds." Einstein
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 02 05:48PM +0100

On 02.11.2020 17:35, Bonita Montero wrote:
>> recent standards for the C programming language.
 
> AFAIK bool isn't a native type in C but you have to include <stdbool.h>.
> In C++, bool is a native type.
 
The built-in type in C is called `_Bool`.
 
<stdbool.h> adds the `bool` alias, as well as `true` and `false`.
 
 
Cheers,
 
- Alf
olcott <NoOne@NoWhere.com>: Nov 02 10:50AM -0600

On 11/2/2020 10:48 AM, Alf P. Steinbach wrote:
 
> <stdbool.h> adds the `bool` alias, as well as `true` and `false`.
 
> Cheers,
 
> - Alf
 
I have been a C programmer since K&R was the standard when did they make
this change?
 
--
Copyright 2020 Pete Olcott
 
"Great spirits have always encountered violent opposition from mediocre
minds." Einstein
Mike Terry <news.dead.person.stones@darjeeling.plus.com>: Nov 02 04:50PM

On 02/11/2020 16:31, olcott wrote:
 
> I am guessing the this is simply a Microsoft screw up because online
> documentation seems to indicate that bool is a primitive data type in
> recent standards for the C programming language.
 
bool is not a native type in C.
 
<https://en.wikipedia.org/wiki/C_data_types#Boolean_type>
 
There is a native type _Bool. (Note "true" and "false" are not native
in C either - you must assign an integral type - assigning a non-zero
integer results in a stored _Bool value of 1.)
 
Mike.
Bonita Montero <Bonita.Montero@gmail.com>: Nov 02 05:55PM +0100


> Native Data Types
> Special type: bool (has values true and false)
> https://www.cs.fsu.edu/~lacher/lectures/Output/cppreview/slide02.html
 
That refers to C++ ("1. Basic C++: A Review - 2 of 21").
Joe Pfeiffer <pfeiffer@cs.nmsu.edu>: Nov 02 09:56AM -0700


> I am guessing the this is simply a Microsoft screw up because online
> documentation seems to indicate that bool is a primitive data type in
> recent standards for the C programming language.
 
The draft I've got of the C18 standard (N2346) has the following:
 
7.18 Boolean type and values <stdbool.h>
1 The header <stdbool.h> defines four macros.
2 The macro
bool
expands to _Bool.
 
(and then goes on to define true and false).
 
_Bool, on the other hand, is a keyword and a "real" type.
 
I expect (someone who knows better is welcome to correct me) the reason
it was done this way instead of simply defining a new type "bool" was to
avoid breaking old code that had its own definition of bool.
Bonita Montero <Bonita.Montero@gmail.com>: Nov 02 06:05PM +0100

> 2 The macro
> bool
> expands to _Bool.
 
You don't need C18 for this. This features are included in C99.
olcott <NoOne@NoWhere.com>: Nov 02 11:07AM -0600

On 11/2/2020 10:50 AM, Mike Terry wrote:
> in C either - you must assign an integral type - assigning a non-zero
> integer results in a stored _Bool value of 1.)
 
> Mike.
 
If bool is not a native type in C then they must have changed this at
some point after K&R C. Exactly when was this changed?
 
--
Copyright 2020 Pete Olcott
 
"Great spirits have always encountered violent opposition from mediocre
minds." Einstein
Bonita Montero <Bonita.Montero@gmail.com>: Nov 02 06:08PM +0100

>> integer results in a stored _Bool value of 1.)
 
> If bool is not a native type in C then they must have changed this at
> some point after K&R C. Exactly when was this changed?
 
bool has never been a native Type in any version of C.
olcott <NoOne@NoWhere.com>: Nov 02 11:08AM -0600

On 11/2/2020 11:05 AM, Bonita Montero wrote:
>>            bool
>>        expands to _Bool.
 
> You don't need C18 for this. This features are included in C99.
 
So then C99 changed the K&R standard of bool as a native type?
 
--
Copyright 2020 Pete Olcott
 
"Great spirits have always encountered violent opposition from mediocre
minds." Einstein
olcott <NoOne@NoWhere.com>: Nov 02 11:11AM -0600

On 11/2/2020 11:08 AM, Bonita Montero wrote:
 
>> If bool is not a native type in C then they must have changed this at
>> some point after K&R C. Exactly when was this changed?
 
> bool has never been a native Type in any version of C.
 
Florida State University university says that it is a native type now:
 
Native Data Types
Special type: bool (has values true and false)
 
https://www.cs.fsu.edu/~lacher/lectures/Output/cppreview/slide02.html
 
 
--
Copyright 2020 Pete Olcott
 
"Great spirits have always encountered violent opposition from mediocre
minds." Einstein
Bonita Montero <Bonita.Montero@gmail.com>: Nov 02 06:13PM +0100

> Native Data Types
> Special type: bool (has values true and false)
> https://www.cs.fsu.edu/~lacher/lectures/Output/cppreview/slide02.html
 
Look at the bottom: "1. Basic _C++_: A Review".
Bonita Montero <Bonita.Montero@gmail.com>: Nov 02 06:14PM +0100

>> You don't need C18 for this. This features are included in C99.
 
> So then C99 changed the K&R standard of bool as a native type?
 
bool isn't a native type in any C-standard, even not in K&R C.
olcott <NoOne@NoWhere.com>: Nov 02 11:25AM -0600

On 11/2/2020 11:13 AM, Bonita Montero wrote:
>> Special type: bool (has values true and false)
>> https://www.cs.fsu.edu/~lacher/lectures/Output/cppreview/slide02.html
 
> Look at the bottom: "1. Basic _C++_: A Review".
 
OK I am convinced. I have switched to C++ such a long time ago that I
forgot this difference between C and C++.
 
The C99 standard for C language supports bool variables. Unlike C++,
where no header file is needed to use bool, a header file "stdbool.h"
must be included to use bool in C.
 
https://www.geeksforgeeks.org/bool-in-c/
 
--
Copyright 2020 Pete Olcott
 
"Great spirits have always encountered violent opposition from mediocre
minds." Einstein
Joe Pfeiffer <pfeiffer@cs.nmsu.edu>: Nov 02 10:35AM -0700


>> - Alf
 
> I have been a C programmer since K&R was the standard when did they
> make this change?
 
The oldest copy of he standard I have is N1256, for C99. _Bool and
stdbool.h are in that standard.
"Öö Tiib" <ootiib@hot.ee>: Nov 02 06:32AM -0800

On Sunday, 1 November 2020 15:59:12 UTC+2, Mr Flibble wrote:
 
> The friendship is needed as i_logger::flush is protected and used by logger::flush:
 
> for (auto& copy : copies())
> copy->flush(aMessage);
 
But derived class has already access to protected members of its base?
Frederick Gotham <cauldwell.thomas@gmail.com>: Nov 02 06:23AM -0800

This is a follow-up to "Fat Binary - MS-Windows and four Linux" which I
multi-posted on 24 October 2020 to three newsgroups:
 
comp.lang.c, comp.lang.c++, comp.unix.programmer

You can see it here on comp.lang.c++:
 
https://groups.google.com/forum/#!topic/comp.lang.c++/uL2o62Ngk7c
 
Two days prior I posted "Making a Fat Binary for Linux and Mac", which
you can see here on comp.lang.c:
 
https://groups.google.com/forum/#!topic/comp.lang.c/Qev_btMilNI
 
I have now made a fat binary that contains 6 executable programs:
 
* 16-Bit MS-DOS (for 8088 processors and above)
* 32-Bit MS-Windows (which runs on x86_64 too)
* Linux arm32
* Linux arm64
* Linux x86_32
* Linux x86_64
 
For the Microsoft Windows executable file, I went with a 32-Bit
executable because it will run on both 32-Bit and 64-Bit versions of
MS-Windows.
 
I used the shareware "Pacific C" compiler to build the 16-Bit DOS
version, and originally I was going to embed this 16-Bit program as the
"MZ stub" inside the "PE" file for the Win32 program, however it was
simpler to keep the two programs separate and to use the method
described on the following webpage to embed a binary in a DOS batch file,
I used the "H2B" method that creates a COM program for printing hex as
binary:
 
http://www.massmind.org/techref/dos/binbat.htm
 
So here's how my 'combined Linux / DOS / Windows' script looks now. Note
that each line begins with four spaces. This script will run as an 'sh'
shell script on Linux, and will run as a batch file on MS-DOS and Windows.
 
[BEGIN SCRIPT]
:; if [ -z 0 ]; then #
@echo off
goto :MICROSOFT
fi
 
# Linux shell script starts here
 
# x86 32-Bit = i386, i686
# x86 64-bit = x86_64
# arm 32-Bit = arm
# arm 64-Bit = aarch64 aarch64_be armv8b armv8l
 
arch=`uname -m`
 
case ${arch} in
i386|i686)
skip=AAAAskipAAAA
count=AAAAcountAAA
;;
x86_64)
skip=BBBBskipBBBB
count=BBBBcountBBB
;;
arm|armv7l)
skip=CCCCskipCCCC
count=CCCCcountCCC
;;
aarch64|aarch64_be|armv8b|armv8l)
skip=DDDDskipDDDD
count=DDDDcountDDD
;;
*)
exit 1
;;
esac
 
tmpfile=$(mktemp /tmp/fatbinary_${arch}.XXXXXXXXXX)
dd if="$0" of=${tmpfile} bs=1 skip=${skip} count=${count} > /dev/null 2>&1
chmod +x ${tmpfile}
${tmpfile} "$@"
retval=$?
rm -rf "$tmpfile"
exit ${retval}
# Microsoft batch file starts here
 
:MICROSOFT
 
VER | FIND "Windows" > NUL
IF ERRORLEVEL 1 GOTO DOS_16_BIT
 
REM Script for Windows 95 and above starts here (32-Bit)
 
REM The next 6 lines are just for creating a temporary file
:loop
set /a y$$=%random%+100000
set y$$=temp%y$$:~1,2%.%y$$:~-3%
if exist "%temp%\%y$$%.exe" goto loop
set "y$$=%temp%\%y$$%"
set "tempfile=%y$$%.exe"
 
findstr /v /c:" " "%~f0" > %tempfile%
%tempfile% %1 %2 %3 %4 %5 %6 %7 %8 %9
set "finalerrorcode=%ERRORLEVEL%"
del %tempfile%
exit /b %finalerrorcode%
 
:DOS_16_BIT
 
REM Script for MS-DOS v6.22 and below starts here (16-Bit)
 
IF EXIST %0.BAT %0.BAT %1 %2 %3 %4 %5 %6 %7 %8 %9
REM The next 6 lines are to create the DOS COM program "hex to binary"
ECHO Bj@jzh`0X-`/PPPPPPa(DE(DM(DO(Dh(Ls(Lu(LX(LeZRR]EEEUYRX2Dx=>%TEMP%\H2B_@!7$.COM
ECHO 0DxFP,0Xx.t0P,=XtGsB4o@$?PIyU WwX0GwUY Wv;ovBX2Gv0ExGIuht6>>%TEMP%\H2B_@!7$.COM
ECHO ?@}I{uNWEF~NFAa_Lj@KLtH]~CEvEFIKbAa_wN@SuNS`{ECCttasae~BHM>>%TEMP%\H2B_@!7$.COM
ECHO AcjFnvnHAwrvx[}gIKDw??Frt\gqj~{?s?csIsqo{O_KtBve{Sx{nB{Eu@>>%TEMP%\H2B_@!7$.COM
ECHO fq`tkfk?e@oKCA_?_E@?WxAs?agBwRjnLK?s@w`G`LKLAcyA?@xAsZpk`L>>%TEMP%\H2B_@!7$.COM
ECHO ~KxlqLct@vAc_A_yBJ@xAGZp?o?sBXq`LR@xUrFQt=A_E?B?~rB?~r0>>%TEMP%\H2B_@!7$.COM
 
%TEMP%\H2B_@!7$ " ::dos16bit" < %0 > %TEMP%\S256@!7$.EXE
DEL %TEMP%\H2B_@!7$.COM
 
%TEMP%\S256@!7$.EXE %1 %2 %3 %4 %5 %6 %7 %8 %9
DEL %TEMP%\S256@!7$.EXE
EXIT
REM The exit command won't work so must use goto with a bad label
GOTO NO_SUCH_LABEL
[END SCRIPT]
 
The Linux part of the above script has "\n" line endings, whereas the
Microsoft part of the above script has "\r\n" line endings. See the
first link at the end of this post to get the original file which also
has the hex values for the 16-Bit DOS program at the end.
 
As for the program for which I chose to make a fat binary, I went with a
sha256 digest calculator, using code by Alain Mosnier from repository:
https://github.com/amosnier/sha-2
 
I have turned Mosnier's code into a single-source-file C program that
calculates the sha256 digest of its first command line argument. For the
C code, see the second link at the end of this post.
 
I compiled this C program 6 times and got the 6 following binaries:
 
-rwxr-xr-x 1 root root 7842 Nov 2 12:15 prog_dos16bit.exe
-rwxr-xr-x 1 root root 17422 Oct 24 11:10 prog_mswin_x86_32.exe
-rwxr-xr-x 1 root root 5464 Oct 24 13:50 prog_x86_32
-rwxr-xr-x 1 root root 10216 Oct 24 13:50 prog_x86_64
-rwxr-xr-x 1 root root 5528 Oct 24 13:50 prog_arm32
-rwxr-xr-x 1 root root 10080 Oct 24 13:50 prog_arm64
 
(See download links 3-8 at the end of this post)

The DOS program is concatenated to script as hex values:
 
cat prog_dos16bit.exe | xxd -p \
| awk '{ print \" ::dos16bit \" $s \"\\r\" }' >> script.txt
 
and I concatenated the 5 other binaries as follows:
 
cat script.txt prog_mswin_x86_32.exe prog_x86_32 prog_x86_64 \
prog_arm32 prog_arm64 > ftsha256.bat
 
The result is my fat binary with size 75 kilobytes:
 
-rwxr-xr-x 1 root root 75016 Nov 2 13:52 ftsha256.bat
(See download link 9 at the end of this post)

This file can be executed at the command line on any PC like this:
 
ftsha256.bat This is my message

And it prints the digest:
 
3311b7c0bd91b6c73a38212de8ade31c51910f17480ad212ed2b9798a35b7747

I have verified it works on:
* MS-DOS 6.22
* MS-Windows XP 64-Bit
* Linux arm32
* Linux arm64
* Linux x86_64
 
It should also work on Linux x86 32-Bit, let me know if someone tries
it. It might need more work on the ASCII/ANSI versions of MS-Windows
that came between Windows 3.11 and Windows 2000, I haven't tried it so
I'm not sure.
 
I'm gonna take a break from this for a while, but in the future I might
iron out the ASCII versions of MS-Windows, and then later add in
binaries for Apple MacOS.
 
Links:
 
1: http://virjacode.com/experimental3/script.txt
2: http://virjacode.com/experimental3/sha256.c
3: http://virjacode.com/experimental3/prog_dos16bit.exe
4: http://virjacode.com/experimental3/prog_mswin_x86_32.exe
5: http://virjacode.com/experimental3/prog_arm32
6: http://virjacode.com/experimental3/prog_arm64
7: http://virjacode.com/experimental3/prog_x86_32
8: http://virjacode.com/experimental3/prog_x86_64
9: http://virjacode.com/experimental3/ftsha256.bat
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Nov 02 01:53PM

On Sun, 1 Nov 2020 07:38:25 -0800 (PST)
> speculative, as cosmology was once entirely speculative, but with
> cosmology, we now have evidence that supports some ideas but
> not others. No doubt understanding will advance.
 
Let us not confuse artificial intelligence on the one hand, which by
most definitions we already have, in the sense of machines which can
adapt their behaviour according to past events or patterns, and which
can interract sensibly with human beings in an apparently intelligent
way, and artificial consciousness on the other hand, which we do not
appear to have and which in any event we have as yet no means of
testing for.
 
There are plausible arguments that in humans and the higher animals
consciousness is an emergent property (or illusion, take your pick)
behind which determinism lurks, but that is not to say that the
self-generated states implied by consiousness are always benign, as
witnessed by the behaviour that can arise from the psychoses and other
disorders in human minds. It seems probable that in any conscious
system the possibility of malign disorders will always exist.
 
That is not to say that complex but non-conscious artificial
intelligence cannot also be capable of malign behaviour.
red floyd <no.spam.here@its.invalid>: Nov 01 11:38PM -0800

On 10/31/2020 12:10 AM, Bonita Montero wrote:
>> std::getline(std::istringstream(some_text), str);
 
> std::string str( "***" );
> ... is shorter and faster.
 
And if some_text is a std:string containing multiple lines of text, that
was read from an external source?
Bonita Montero <Bonita.Montero@gmail.com>: Nov 02 07:54AM +0100

>> ... is shorter and faster.
 
> And if some_text is a std:string containing multiple lines of text, that
> was read from an external source?
 
The given example was a static string.
red floyd <no.spam.here@its.invalid>: Nov 02 01:21AM -0800

On 11/1/2020 10:54 PM, Bonita Montero wrote:
 
>> And if some_text is a std:string containing multiple lines of text, that
>> was read from an external source?
 
> The given example was a static string.
 
And then my follow up was a generic std::string.
Bonita Montero <Bonita.Montero@gmail.com>: Nov 02 10:58AM +0100

>> The given example was a static string.
 
> And then my follow up was a generic std::string.
 
But that's another issue.
But if you want to have it that way; this should be much faster:
 
#include <iostream>
#include <string>
#include <algorithm>
 
using namespace std;
 
int main()
{
string strA( "hello\nworld" );
string strB( strA.substr( 0, find_if( strA.begin(), strA.end(), [](
char c ) { return c == '\n'; } ) - strA.begin() ) );
cout << strB << endl;
}
Paavo Helde <myfirstname@osa.pri.ee>: Nov 02 03:42PM +0200

02.11.2020 11:58 Bonita Montero kirjutas:
> char c ) { return c == '\n'; } ) - strA.begin() ) );
>     cout << strB << endl;
> }
 
And this is shorter and not slower:
 
#include <iostream>
#include <string>
 
int main() {
std::string strA("hello\nworld");
std::string strB(strA, 0, strA.find('\n'));
std::cout << strB << "\n";
}
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: