How to check if my pointer value has been set yet....

Started by
3 comments, last by johnnyBravo 20 years, 6 months ago
say i got a pointer like so : int * number; how do i check if "number" hasnt been: number= new int [4] yet?
Advertisement
int * number = NULL;if(number == NULL)   cout << "Not been set yet!";else   cout << "number has been set";


[edited by - _the_phantom_ on October 11, 2003 2:04:26 AM]
the_phantom is correct.

If you declare a pointer, then it initially points to nothing. A pointer that points to nothing is called a ''NULL'' pointer. So to check whether a pointer actually points to something, you simply check whether that pointer is NULL or not, like the_phanton did.
quote:Original post by chacha
If you declare a pointer, then it initially points to nothing. A pointer that points to nothing is called a ''NULL'' pointer. So to check whether a pointer actually points to something, you simply check whether that pointer is NULL or not, like the_phanton did.

Careful there. When you declare a pointer, it doesn''t initially point to NULL. It points to some random memory location that is most likely outside of your program''s memory area (IIRC, on MSVC debug it''s usually 0xC4C4C4C4) and thus will make the program crash if accessed. That''s why it''s important to always initialize your pointers to NULL (or 0) when declaring them.

How appropriate. You fight like a cow.
quote:Original post by Sneftel
quote:Original post by chacha
If you declare a pointer, then it initially points to nothing. A pointer that points to nothing is called a ''NULL'' pointer. So to check whether a pointer actually points to something, you simply check whether that pointer is NULL or not, like the_phanton did.

Careful there. When you declare a pointer, it doesn''t initially point to NULL. It points to some random memory location that is most likely outside of your program''s memory area (IIRC, on MSVC debug it''s usually 0xC4C4C4C4) and thus will make the program crash if accessed. That''s why it''s important to always initialize your pointers to NULL (or 0) when declaring them.

How appropriate. You fight like a cow.

Yes, thanks for correcting me Sneftel.

This topic is closed to new replies.

Advertisement