is "return((void*)1)" safe?
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!
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.
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.
Quote:Original post by SiCraneIn 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.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.
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
}
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?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement