Jump to content
  • Advertisement
Sign in to follow this  
floatingwoods

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

This topic is 3486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I've seen many libraries keep track of status/error codes from the last executed function.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!