views:

169

answers:

3

How can i write an if statement which tells to the program if the pointer is assigned or not?

WRONG example

if (*badpdr[0]==0);

??

+5  A: 

You mean if (badptr==NULL) ?

Note that you have to initially set your pointer to NULL when you define it or when you "unassign it" (e.g., you delete the object it refers to). Otherwise, it will contain junk and this test would fail because the value would not be a 0.

You can also do the comparison to 0 instead of NULL, there's already enough arguments on SO which is the correct form so I won't repeat them.

Uri
Isn't NULL more C++?
Simon
Though to be fair, in C it's you own task to make sure that badptr is NULL. So initialize it to NULL (if you don't initialize it to something else) and follow up a delete statement with ptr = NULL.However, this is something you should so anyway, so there's no real downside there.
Jasper
@Simon: I'm actually not sure, been many many years since I wrote/taught ANSI-C, and my mind was clouded by C++ (though admittedly, it's been many years since I wrote any C++ either :)
Uri
jim mcnamara
@Simon: If I'm not mistaken, NULL is actually cleaner in C than in C++ (though I don't do much C, so it's hear-say)
Jasper
I really wanted to avoid starting the NULL vs. 0 fight again.
Uri
@Simon: No; quite the reverse. It is defined in the C standard library headers, and its use is discouraged in C++.
Clifford
@Clifford: But of course C++0x has it that `nullptr` should be used instead.
Jon Purdy
You guys make me happy that I'm a Java developer these days :)
Uri
but i am not happy since my lecturer is not a java developer :D
gkaykck
also when i point a NULL with my mouse in visual studio, it shows up a tip, #define NULL 0, so actually 0 seems better
gkaykck
Clifford
If i see NULL, i know it's pointer.
Nyan
@Clifford, I think defining NULL as (void*)0 is for variable argument functions. execl("path","arg","foo","bar",NULL); will work correctly only if NULL is defined as (void*)0.
Nyan
@Clifford,nope i am not compiling my code as c++
gkaykck
+3  A: 

char *ptr=NULL;

initalize the pointer to NULL; then later on you can check if it is NULL to see if it is valid before you try to deference it.

jim mcnamara
A: 

You can install signal handler for SIGSEGV and then try to dereference the pointer - if you find yourself inside signal handler, pointer wasn't assigned (or points to invalid memory address, which basically is the same thing)

However there could be the situations when unassigned pointer points to valid address that belongs to process memory space - in this case there will be no SIGSEGV raised.

qrdl
Would a downvoter be so kind to explain the downvote? You may not like the answer for whatever reason but it isn't wrong.
qrdl
If you use a signal handler and try to dereference the pointer and the pointer is to an area outside of the memory that is available, it will throw a magical exception which is only allowed to be thrown once. Then, when you later try to allocate memory, the allocator will be looking for that exception to see if it needs to increase the address space and won't see it, and things won't work right. Note that all of the previous text is slightly off, since I'm writing it from my rather hazy memories of an article which explained why it is a bad idea to try to validate memory via dereference.
Brian
@Brian Which exceptions are you talking about? The question is about C, not C++, and C doesn't have any exceptions. I wrote about signal, not exception, and signal is a very different thing. I think you have mixed up two absolutely different concepts.
qrdl
Guard page exceptions. See http://msdn.microsoft.com/en-us/library/aa366549(VS.85).aspx . They aren't c++ exceptions. And they are used to control the growth of the stack, so you *are* using them.
Brian
I see, you are from Windows side of the fence. Anyway, there are no exceptions in C, and if Microsoft made up something and it doesn't work, downvote Microsoft. I was talking about `SIGSEGV`, which is POSIX, not Microsoft.
qrdl
@qrdl: I consider it generally a bad idea to dereference a pointer just to see if it's valid. If you don't know what you're pointing to, I'd rather you crash or rewrite your program. And Guard page exceptions are not C++ nor (nonexistent) "C exceptions", but they are still exceptions...or at least Microsft calls them that in their documentation in at least one place, as do other people. Words can have more than one meaning. And since C doesn't have exceptions, there is no confusion over whether it means language exceptions :)
Brian
@qrdl: As an aside, although POSIX does not provide an interface for guard pages, it does not prohibit them, either. Windows is not the only OS which makes use of guard pages; it's very common to use them to guard the stack.
Brian
+1 to negate -1 for a very interesting way to test a pointer, although obviously not one I would recommend in a real program. Can't say I would have thought of this. At this point there are more than enough comments debating the merit of this answer to set it back to 0.
wuputah