Re: if allocate() returned -1 on failure...

new topic     » goto parent     » topic index » view thread      » older message » newer message

In cases like this, I typically refer to the principle of least surprise:

Wikipedia said...

For those parts of the system which cannot be adjusted to the peculiarities of the user, the designers of a systems programming language should obey the "Law of Least Astonishment." In short, this law states that every construct in the system should behave exactly as its syntax suggests. Widely accepted conventions should be followed whenever possible, and exceptions to previously established rules of the language should be minimal.

In short: "a thing should do what I expect it to do." Although the corollary may be more important here: "a thing should not do what I would not expect it to do."

In my experience, functions that return a memory address almost universally return zero or null on error. Or if the language supports it, they throw an exception of some kind.

I think the historical and/or low-level reason for this is that memory addresses are typically stored as unsigned pointer-aligned integers, making zero the lowest possible value.

An alternative might be to return either a valid memory address (positive) or an error number (negative), and maybe zero represents an invalid result or "undefined" behavior.

Personally, I would not change it.

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu