Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

IMO the way to make C++ fun is to:

    1. Build and run it with a shell script (because the build systems do suck)
       Dependencies may complicate this, but you can still link with them with a shell script
       And use Unix – I started with C++ on Windows, and that sucks (also mentioned in the article)

    2. Turn on address sanitizer in development, which makes it memory safe – you get a Python-like
       stack trace on errors instead of undefined behavior
Example:

    $ echo 'int main() { return 42; }' > foo.cc

    $ c++ -fsanitize=address -o foo foo.c && ./foo; echo $?
    42
I use an actual editor, and unit tests, but essentially shell is my REPL for C++. It’s easier to figure out that way.

Newer features like constexpr have subtle rules, so it’s easier to just try it (even though I’ve used C++ for many years). I run all the tests with Clang too.

---

Example with ASAN:

    $ echo 'int main() { char buf[1]; buf[1] = 42; }' > error.cc                                    

    $ c++ -fsanitize=address -o error error.cc && ./error; echo $?

    ==118199==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffcd94eb431 at pc 0x56217647520f bp 0x7ffcd94eb400 sp 0x7ffcd94eb3f8
    WRITE of size 1 at 0x7ffcd94eb431 thread T0
        #0 0x56217647520e in main (/home/andy/git/oilshell/oil/error+0x120e)
        #1 0x7f7928446249 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
It’s not too hard to learn to read the output, and then basically you can go nuts like C++ is Python. For a small program, the edit/run cycle will be extremely fast, like Python.

The silent undefined behavior is a big barrier to learning, and this removes most of it. (You can also pass -fsanitize=undefined for UBSAN, which finds other bugs, but many fewer IME)

ASAN is built into compilers; you don’t need to install anything. A bare Debian or BSD system has all this good stuff :-)

(copy of lobste.rs comment)



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: