Live data from Hacker News

Popular Myths about C++, Part 1

isocpp.org

21–30 of 144 posts

Re: Popular Myths about C++, Part 1

#21

Good luck trying to understand C++ without C. The numerous C++ traps and pitfalls will just look mad to you. BTW, 'multi-paradigm' is an oxymoron. Not even Scala uses that concept any more.

What exactly do you need to know about C, that is not part of C++, to understand C++? Genuine question.

why do pointers exist if you have references? why does printf exist? what is everything in the cstdlib?

Re: Popular Myths about C++, Part 1

#22
What sort of C programmer wouldn't use snprintf to do the formatting? If the C++ program can use std::string then it's only fair to let the C program import . And honestly printf is so much nicer to use than iostreams I've always considered the relative ease of putting together a formated string one of C's strengths relative to C++.

Re: Popular Myths about C++, Part 1

#23

"Finally, which version is likely to be the most efficient? Yes, the C++ version, because it does not have to count the argument characters and does not use the free store (dynamic memory) for short argument strings." I just compiled the C code and the C++ code using: gcc -std=c99 -Wall -Wextra test.c -o test g++ -std=c++11 -Wall -Wextra test.cpp -o test According to valgrind: C: total heap usage: 1 allocs, 1 frees,…

Try it with -stdlib=libc++, since that's what will control the behavior of std::string.

Ooh. Nice:

  total heap usage: 0 allocs, 0 frees, 0 bytes allocated

Re: Popular Myths about C++, Part 1

#24

Good luck trying to understand C++ without C. The numerous C++ traps and pitfalls will just look mad to you. BTW, 'multi-paradigm' is an oxymoron. Not even Scala uses that concept any more.

What exactly do you need to know about C, that is not part of C++, to understand C++? Genuine question.

I think what ExpiredLink1 meant is what millstone says here [1], which I took to mean that although you don't need to know C before learning C++, psychologically it may help to know about C (i.e., that a language called C existed before C++ and that C++ is designed to be mostly backwards compatible with C), so that C++'s design choices don't seem as weird.

[1] https://news.ycombinator.com/item?id=8722655

Re: Popular Myths about C++, Part 1

#25

What sort of C programmer wouldn't use snprintf to do the formatting? If the C++ program can use std::string then it's only fair to let the C program import . And honestly printf is so much nicer to use than iostreams I've always considered the relative ease of putting together a formated string one of C's strengths relative to C++.

What sort of C programmer wouldn't test for malloc() failing.

Drives me batty to see articles where malloc() is happily assumed to always succeed.

Re: Popular Myths about C++, Part 1

#27
post #14

Earlier quoted context omitted.

The articles promise is actually terribly dubious. Firstly, in the real world, name+'@'+domain is likely to exceed the capacity for any SSO anyway. Even the example composition, which is 21 chars, may resort to heap allocation under some implementations. It's a bad solution to depend on it for performance. Secondly, as you discovered, the current GNU stdlibc++ implementation of std::string will always allocate for sh…

That's the good thing about C. With C++, you don't know what is being allocated, where. With C, it is explicit. You know what's happening if you call malloc. And this compose function could easily be written in C to take the "addr" as an argument to be filled, rather than provide it as a return value. Then it can be entirely stack allocated. The caller would need to make sure that the "addr" array was large enough of…

On the other hand, I think one of the good things about C++ is that you have a choice. If you wish to write code that explicitly allocates memory for a string of characters you can. You can even wrap it up in a class to make it easy to use.

Re: Popular Myths about C++, Part 1

#28

"Finally, which version is likely to be the most efficient? Yes, the C++ version, because it does not have to count the argument characters and does not use the free store (dynamic memory) for short argument strings." I just compiled the C code and the C++ code using: gcc -std=c99 -Wall -Wextra test.c -o test g++ -std=c++11 -Wall -Wextra test.cpp -o test According to valgrind: C: total heap usage: 1 allocs, 1 frees,…

Your comparison entails the fallacy of using compiler specific behavior for what the standard doesn't specify.

Re: Popular Myths about C++, Part 1

#29
post #14

"Finally, which version is likely to be the most efficient? Yes, the C++ version, because it does not have to count the argument characters and does not use the free store (dynamic memory) for short argument strings." I just compiled the C code and the C++ code using: gcc -std=c99 -Wall -Wextra test.c -o test g++ -std=c++11 -Wall -Wextra test.cpp -o test According to valgrind: C: total heap usage: 1 allocs, 1 frees,…

The articles promise is actually terribly dubious. Firstly, in the real world, name+'@'+domain is likely to exceed the capacity for any SSO anyway. Even the example composition, which is 21 chars, may resort to heap allocation under some implementations. It's a bad solution to depend on it for performance. Secondly, as you discovered, the current GNU stdlibc++ implementation of std::string will always allocate for sh…

Right. Also, the C version would be cleaner using the printf family of functions. Idiomatic C would use snprintf and accept a buffer. If you insist on returning a heap-allocated object, you could use asprintf, which is not universal but is common. Either way it's a one-liner.

Re: Popular Myths about C++, Part 1

#30

What sort of C programmer wouldn't use snprintf to do the formatting? If the C++ program can use std::string then it's only fair to let the C program import . And honestly printf is so much nicer to use than iostreams I've always considered the relative ease of putting together a formated string one of C's strengths relative to C++.

What sort of C programmer wouldn't test for malloc() failing. Drives me batty to see articles where malloc() is happily assumed to always succeed.

> Drives me batty to see articles where malloc() is happily assumed to always succeed.

... as in Linux.

Post reply on HN