You are not logged in.

#1 2012-04-05 00:32:24

gobeav3rs297
Member
From: Portland Oregon
Registered: 2007-11-06
Posts: 60

validating valid pointers in C

Hi,

Other than checking if pointer is NULL, is there any other proven methods of checking a pointer passed as parameter into a function call to make sure it's valid before we start dereference it and use it.

-Vincent

Offline

#2 2012-04-05 00:52:40

krigun
Member
From: Norway
Registered: 2005-06-06
Posts: 122
Website

Re: validating valid pointers in C

Unfortunately there is just no way of knowing if it is safe to dereference a pointer. That is why you should always initialize pointers to zero, and do the same when you have deleted the pointer. I've had some really nasty bugs when forgetting to do so.

Offline

#3 2012-04-05 08:24:15

lunar
Member
Registered: 2010-10-04
Posts: 95

Re: validating valid pointers in C

You cannot verify pointer integrity locally within a function.  However, there are tools that can catch some pointer errors statically or during runtime.  valgrind is a popular one.

Offline

#4 2012-04-06 13:06:03

Blµb
Member
Registered: 2008-02-10
Posts: 224

Re: validating valid pointers in C

Also, it's generally a better idea to simply make it a precondition that pointers passed to a function are valid.
If for some reason you cannot do that (and I'm 99.9% sure you can), then your only other choice for a runtime validation is to change your program so that you use memory from a memory pool you manage yourself.

For this topic: consider the correctness of your program.
As soon as you start requiring fancy checks and special cases, you're most likely doing something wrong, or have taken a bad design choice.


You know you're paranoid when you start thinking random letters while typing a password.
A good post about vim
Python has no multithreading.

Offline

Board footer

Powered by FluxBB