is "return((void*)1)" safe?

Started by
47 comments, last by Nitage 14 years, 11 months ago
Hi, I have a function that returns a block of data depending on the arguments. Sometimes the functions doesn't return any data in which case the return value is set to NULL. However other times I just want to signal a special case without returning any data nor changing the number of arguments of my function. So would: return((void*)1); be safe? (knowing that the caller knows how to interpret this special case). Put in other words: return((void*)12345); is not safe because the same pointer value could be returned by a memory allocation function, but would the same memory allocation function ever return 1? Thanks!
Advertisement
Perhaps someone will chime in with something more helpful, but if you are going to use a sentinel, -1 would be much safer. That maps into kernel reserved space and can't possibly be a legitimate memory address.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
It depends on your platform, but it's a pretty bad idea even if it was "safe".
Quote:Original post by Promit
That maps into kernel reserved space and can't possibly be a legitimate memory address.

Again, that depends on your platform. On a PC, yes. On an embedded platform, it may not be true.
I've seen many libraries keep track of status/error codes from the last executed function.
[size="2"]I like the Walrus best.
Thanks for the clarifications to both of you!
Quote:Original post by SiCrane
Quote:Original post by Promit
That maps into kernel reserved space and can't possibly be a legitimate memory address.

Again, that depends on your platform. On a PC, yes. On an embedded platform, it may not be true.
In my defense, I tend to assume people aren't on embedded devices, because those things usually throw half the rulebook away, and shred another quarter. If you have to preface everything with "but embedded will screw you over", it gets rather tedious.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Could do something like this:

static int kDummy = 0;
void* kSpecialCase = &kDummy

void* value = MyFunction();
if( value == NULL )
{
// error
}
else if( value == kSpecialCase )
{
// special case
}
else
{
// normal
}
After reading this thread, I think that using dubious casts should be punishable offense.

So why not find a more reasonable solution?
Quote:Original post by Antheus
After reading this thread, I think that using dubious casts should be punishable offense.

In their defence, i think they said the code was generated be a decompiler, in which case the decompiler would not have any information on the class and method names.

ingramb's suggestion is the safest for returning a "special case" pointer, but i'm sure there are still better ways.
[Window Detective] - Windows UI spy utility for programmers

This topic is closed to new replies.

Advertisement