pkirk25 said:
If it fails, optimization goes out the window and I pass by value.
My impression is that when it fails, you can face hours of misery.
No, you just need to realise that an important part of programming consists
of answering the following questions:
1) what external resources do I need, to get the job done?
2) can I get them?
3) what should I do if I can't? Are there alternatives? Or should I just
give up? How do I fail gracefully?
So - let's say you're trying to open a file, supposedly given by the user on
argv[1]. Do you fopen(argv[1], "r"); ? Nope, not until you've checked that
argc > 1. And what if it isn't? No point in going on without data. Should
you look for data on stdin instead? Or should you ask the user to use stdin
to provide a filename?
Okay, so you need some memory to do - well, whatever. Let's just imagine a
situation where you can't have it, for whatever reason. So - would it be
okay to have two blocks, each half the size? Would that work for you? If
so, you have a possible failure recovery strategy (because an allocator may
not be able to give you one contiguous lump of N bytes, but may have
several lumps that are >= N/2 bytes in size).
That's one possible recovery strategy. There are others. Which ones you go
for depend on what you're doing, what your situation is.