News for hackers. Stuff that matters.
Re:Popular Myths about C++, Part 1 by Symmetry on Tuesday December 09, 2014 @09:03AM (#8722702)
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 <stdio.h>. 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 by linuxlizard on Tuesday December 09, 2014 @09:09AM (#8722732)
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 by kabdib on Tuesday December 09, 2014 @09:42AM (#8722928)
Rule #1 of systems programming: "Never check for an error that you don't know how to handle." This is, of course, a joke, but it has a kernel of truth.

I've worked on production code where, if allocation fails, the system fails, too, and restarts. The allocator never returns. There's usually some kind of helper system watching for the failure, which can take a dump or at least a stack trace.

The theory is that any attempt at recovery is going to be a disaster; not well tested, and probably doomed in the face of memory pressure anyway. On 64 bit systems the memory ceiling is essentially infinite, but reaching the point where we start to swap is horrible and a pyrrhic victory at best, so instead let's do a controlled crash and log enough information so we can analyze and fix the actual problem (which is a misunderstanding of load and resources, or maybe a bug or an attack where we're trying to allocate twenty gazillion bytes).

That's one philosophy. Another (which was prevalent on the Mac in the 80s, in user applications) is to have a reserve; you test the crap out of the code and give it enough memory in some kind of emergency mode to succeed at getting back to a normal state, or at least quitting in an orderly fashion (ditch dynamically loaded code, fonts, whatever) so that the user's work isn't totally lost.

There's a lot to be said for both designs, and of course these don't cover the case when you're writing library code that can be used by anyone, where reliably returning failure to the caller is important. Here you don't have a choice other than to check and be robust.

So three, yes, three philosophies.

Re:Popular Myths about C++, Part 1 by linuxlizard on Tuesday December 09, 2014 @10:39AM (#8723306)
I worked on some safety critical systems in C. The philosophy was treat the code like a little person running around. The person (the code) always wanted to know where the fire escape was. The fire escape always needed to be very easy to use and very reliable. Every function had little fire escape handler to clean up on error. Higher level functions had to decide how to put out the fire (speaking metaphorically here but we really could have started fires) or to raise the alarm to a higher level.

At the very top level, the only thing we could do was save a log then reset (least worst option). Too many resets too fast and we'd take ourselves offline and scream for a human (again, least worst option).

Re:Popular Myths about C++, Part 1 by ExpiredLink on Tuesday December 09, 2014 @09:20AM (#8722797)
> Drives me batty to see articles where malloc() is happily assumed to always succeed.

... as in Linux.

Re:Popular Myths about C++, Part 1 by ycombobreaker on Tuesday December 09, 2014 @09:27AM (#8722843)
Use `ulimit` from the shell, or invoke `setrlimit`, and you will see malloc fail.
Re:Popular Myths about C++, Part 1 by linuxlizard on Tuesday December 09, 2014 @10:47AM (#8723357)
Memory leaks in Firefox/Chrome might belie that notion.

I've had Python throw MemoryError at me while I was doing some video processing with NumPy. 8GB RAM, 64GB swap. I wasn't careful with my refs and I confused Python's garbage collection (memory leak).

Re:Popular Myths about C++, Part 1 by mikeash on Tuesday December 09, 2014 @09:39AM (#8722912)
Malloc will never fail on Linux due to running out of RAM. However, it will fail for other reasons, such as running out of address space. You can easily run out of address space in 32-bit processes, since you only have 4GB of address space, or less. It's much harder in 64-bit processes, but since you can allocate vast chunks of memory without having them be backed by RAM, you can accomplish it there as well.
Re:Popular Myths about C++, Part 1 by linuxlizard on Tuesday December 09, 2014 @10:33AM (#8723268)
While address space is huge, RAM isn't infinite. Our embedded Linux runs out of memory quite often. Or if the swap space fills up on a server with a disk.

I've spent many hours chasing corrupted memory due to someone not checking malloc() fail.

I chased code that boiled down to assert( ptr=malloc() ); Guess what happens when NDEBUG is defined?

Re:Popular Myths about C++, Part 1 by antimagic on Tuesday December 09, 2014 @11:09AM (#8723511)
It's not so much that malloc can fail, but that you're likely to get whacked by OOM before you run out of address space on linux. To get malloc to return a NULL, you would have to be allocating more than 4Gb of memory on a 32 bit system, the system has to have no swap, and you have to not have used any of the memory between the last physically available memory, and the memory allocated by the chunk that takes you over the 4Gb limit. Anything else gets you SIGKILLed.
Re:Popular Myths about C++, Part 1 by mikeash on Tuesday December 09, 2014 @11:17AM (#8723561)
Swap has nothing to do with it. Getting malloc to fail is easy:

    malloc(-1);
With address space fragmentation, the requested size can be a lot smaller and still fail. With overcommit, you can allocate a ton of memory without using up any RAM.
Re:Popular Myths about C++, Part 1 by jerf on Tuesday December 09, 2014 @10:43AM (#8723336)
"You can easily run out of address space in 32-bit processes, since you only have 4GB of address space, or less."

Ah, 21st Century problems.

Re:Popular Myths about C++, Part 1 by linuxlizard on Tuesday December 09, 2014 @10:50AM (#8723380)
I know, right!? Kids these days. Sheesh. :-)
Re:Popular Myths about C++, Part 1 by fsloth on Tuesday December 09, 2014 @02:50PM (#8725210)
If malloc fails for trying to allocate a few miserable bytes I would claim the game is over. Why would he need a fallback strategy, anyway? The program is tiny and really, really doesn't do anything important.

Matters of taste I suppose, but I've always felt that for tutorial and proof of concept code it's better to skip unnecessry details.

Not every line of code lives in a production environment...

Re:Popular Myths about C++, Part 1 by CountHackulus on Tuesday December 09, 2014 @09:41AM (#8722924)
snprintf isn't a standard function. There's certain platforms that don't have it. I know, it happened to me on z/Linux.
Re:Popular Myths about C++, Part 1 by vonmoltke on Tuesday December 09, 2014 @10:21AM (#8723196)
snprintf is a standard function, but only as of C99.