I would recommend two books, one on C++20 and the other on C++23, by the same authors. The books have github repos, one I got the range formatting workaround from. Beginning C++20: From Novice to Professional https://link.springer.com/book/10.1007/978-1-4842-5884-2 Beginning C++23: From Beginner to Pro https://link.springer.com/book/10.1007/978-1-4842-9343-0 Yes, they are eBooks, I have the C++20 book in a dead tree edition. And find it very flimsy and hard to read compared to the eBook. I own both books and really recommend getting both. Each book cover much of the same material with examples and exercises of the other. The C++20 book has many of the examples/exercises that use modules in non-module form. |
Have you all ... |
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_EXTENSIONS False) |
$ g++ --version g++ (GCC) 11.4.1 20230605 (Red Hat 11.4.1-2) |
I strongly suggest that you get in the habit of always writing 'correct' code - even for test or learning purposes. |
I have never experienced either of the two above not working but since it is being told to me by the collective experience i believe it. i would be more appreciative if i saw it firsthand even so. is there a known formula for a cout, maybe through some overexaggerated algorithm that will show the const char* expiration before the cout usage? |
|
|
================================================================= ==24377==ERROR: AddressSanitizer: heap-use-after-free on address 0x604000000010 at pc 0x7f186529e7f1 bp 0x7ffcc07c86e0 sp 0x7ffcc07c7ea0 READ of size 2 at 0x604000000010 thread T0 #0 0x7f186529e7f0 (/usr/local/lib64/libasan.so.8+0x637f0) #1 0x402a15 in process_order(char const*, char const*) (/tmp/1711383839.624552/a.out+0x402a15) #2 0x4027bf in main (/tmp/1711383839.624552/a.out+0x4027bf) #3 0x7f18642f782f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f) (BuildId: b5381a457906d279073822a5ceb24c4bfef94ddb) #4 0x4028e8 in _start (/tmp/1711383839.624552/a.out+0x4028e8) 0x604000000010 is located 0 bytes inside of 47-byte region [0x604000000010,0x60400000003f) freed by thread T0 here: #0 0x7f1865310b18 in operator delete(void*, unsigned long) (/usr/local/lib64/libasan.so.8+0xd5b18) #1 0x40a71a in MyAddress::operator char const*() (/tmp/1711383839.624552/a.out+0x40a71a) previously allocated by thread T0 here: #0 0x7f186530fc18 in operator new(unsigned long) (/usr/local/lib64/libasan.so.8+0xd4c18) #1 0x4085ee in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) (/tmp/1711383839.624552/a.out+0x4085ee) SUMMARY: AddressSanitizer: heap-use-after-free (/usr/local/lib64/libasan.so.8+0x637f0) Shadow bytes around the buggy address: 0x603ffffffd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x603ffffffe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x603ffffffe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x603fffffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x603fffffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x604000000000: fa fa[fd]fd fd fd fd fd fa fa 00 00 00 00 00 07 0x604000000080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x604000000100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x604000000180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x604000000200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x604000000280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb ==24377==ABORTING |
Have you all become familiar with std::format and have you warmed up to it |
std::cout << std::format("Combined: {:*<+14.4f}, {:+#09x}\n", pi, 314); |
|
|
Combined: +3.1416*******, +0x00013a |
seeplus wrote: |
---|
Moving from the syntax used with printf() to that used with std::format() is easy. :) |
I have to commit it to memory at some point |
Where have you first been exposed to this problem? Is this something that is in a C++ introductory book and in what section? Is this something in a more advanced book? |
int main(void)
is evidence to me that the coder has a C background. If one is curious about that, the use of void
as a parameter has never been a requirement for C++. Oh, if you see void main(void)
get out the fire extinguisher right away!!new
and delete
was promoted a lot, but now we have better tools like smart pointers, and RAII is been promoted more. RAII is Resource Acquisition Is Initialization, and it basically means acquire the resource in the constructor and release it in the destructor. So your question about printing things is a great example of the way things have changed.seeplus wrote: |
---|
the excellent (but now very dated) Sutter (Exceptional C++ series) and Meyers (Effective C++ series) sets of books. |
using namespace hell;
). One can think about the features of the new standards, just like one could of the old features.
|
|
cl /EHsc /W4 /Ox /std:c++17 /utf-8 hello.cpp
clang++ -Wall -Wextra -Werror -pedantic-errors -O3 -std=c++17 hello.cpp
/EHsc
, which just means “do exception handling the normal way”, and /utf-8
on MSVC 17 and earlier, meaning “program source files are UTF-8”./W4
for MSVC and -Wall -Wextra -Werror -pedantic-errors
on GCC and Clang. If I want to be a little more lax while debugging, I’ll drop to /W3
or leave off the -Werror
.-O3
.-std=c++latest
to get the best of whatever the folks at MS have managed to implement so far.-o hello
option. That’s /Fe:hello.exe
with MSVC, but you don’t usually have to specify that.-Idirname
otpions, and a myriad of other things, but that usually suffices.-c
.