Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

safe function calls

This topic is 3821 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

i would like a some kind of function that would take a function pointer and several arguments then check if all (or just the first?) the arguments != NULL and then call the pointed function with those parameters... it would be nice to have it as general as possible... is that possible? it would be something like this SAFE_FUNCTION(SetValue, Pointer, Value); void SetValue(int* Pointer, Value) { *Pointer = Value; }

Share this post


Link to post
Share on other sites
Advertisement
If you are programming in C++, the solution is simple: don't use pointers. Use references instead: references cannot be null.

If you are restricted to using C, you could use assertions or Linus' trick:
void SetValue(int *dest, int src) {
assert(dest);
*dest = src;
}

void SetValue(int *dest, int src) {
if (!dest) return;
*dest = src;
}

Share this post


Link to post
Share on other sites
Yes. Use references.

Other way is:

template < class T >
class SafePtr
{
SafePtr(T * p)
{
if (p == NULL) throw // some exception
}

T * operator->() { return ptr; }
T & operator*() { return *ptr; }

private:
T * ptr;
};


Share this post


Link to post
Share on other sites
Quote:

If this is C++, use a reference. A reference cannot be NULL.



void Foo(int& a)
{
std::cout<<"This cannot be null(or can it?):"<<a<<std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
int* a;
a=NULL;
Foo(*a);
return 0;
}



Morale: C++ is fucked up.

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
Quote:

If this is C++, use a reference. A reference cannot be NULL.


*** Source Snippet Removed ***

Morale: C++ is fucked up.


Well, the C++ language says that code has undefined behaviour. References cannot be NULL. That code is meaningless, it is certainly not standard C++.

Share this post


Link to post
Share on other sites
        int* a;
a=NULL;
Foo(*a); // <-- here you are dereferencing NULL pointer, thus causing undefined
// behaviour. This means *anything* can happen.



Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
Morale: C++ is fucked up.


I'm pretty sure that a = NULL; *a; is also incorrect in C. The fact that C++ considers lvalues to be manipulable (in the form of reference variables) doesn't change anything about lvalues not being NULL in either C or C++.

Share this post


Link to post
Share on other sites
In theory you're all right of course, but in the real world bugs like that can(and most probably will) appear, and there's little you can do to defend against them. And protection against bugs is the primary reason we're using references anyway. My point was to show that references are far from bullet-proof. You certainly can't check a reference for nullity(well you can, but you shouldn't).

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
In theory you're all right of course, but in the real world bugs like that can(and most probably will) appear, and there's little you can do to defend against them.


Use moar references! Seriously, the less naked pointers you have in your code, the fewer places you risk dereferencing a NULL one.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!