I pass a pointer to my function but if this is not initialized I get, obviously, segmentation fault. I could solve this porblem by assigning NULL
to that pointer when declared (or, in general, before passing it to my function), but if I wanted my funtion to detect that pointer is not initialized?
Asked
Active
Viewed 306 times
1

Luigi2405
- 677
- 2
- 7
- 12
-
3No, there is no way (other than assigning `NULL`) to determine is a pointer has a valid value. – pmg Jul 31 '20 at 14:01
-
3Not possible. Initializing to NULL is what should be done. – Eugene Sh. Jul 31 '20 at 14:01
-
3Why would you even want that? Initializing pointers to null is exactly the way that is foreseen to ensure that a pointer does not point to anything valid. – Jens Gustedt Jul 31 '20 at 14:04
-
Please explain why intialising to NULL is not an option. Without that explanation the question it unclear. – Yunnosch Jul 31 '20 at 14:06
-
1I am not defending him, but had similar issue years ago where I developed a function that was used by someone else, and that someone else did not initialize pointer neither was it set to null. It took a lot of going back and fort to fix that issue. There used to be way of checking if pointer is in range of memory given to the program, idk if it is still possible, but that will not prevent you from overwriting your own code and causing segfault anyways. – Jul 31 '20 at 14:23
1 Answers
2
The common way is to always initialize pointers to NULL when you first declare them or after freeing them so that whenever you have to manipulate a pointer, if it's equal to NULL
, you know that you have to allocate them and populate their data.

Mouradif
- 2,666
- 1
- 20
- 37