Questions about Pointers and Structs

I have a couple general questions about pointers and a struct one I'm wondering if anyone can help me out on as I'm studying for a test. These are the last questions I need help with. Thanks for any help!

1.) What happens if you use a pointer that has not been assigned a value?
Ex. int *ptr; cout << *ptr;

2.) What happens if you try to use a pointer with a value of NULL?
Ex. int *ptr = NULL; cout << *ptr;

3.) What is the bigget potential problem using assignment (or copying ) structs or objects?
1) Like with all POD types if you don't initialize them their value will be undefined. It will just contain some garbage value that happen to be stored at that location in memory.

2) Same as with an uninitialized pointer, the behaviour is undefined so there is no guarantees about what will happen. The program might crash, it might print some garbage or it might knock on your door and sell you carpets.

3) I'm not sure.
For 3), a potential one would be if they have a pointer data member, as the pointer itself will be copied rather than the data pointed to, so freeing that memory can lead to odd (normally bad) results.
Er, almost.

1) Undefined. Anything could happen, but most likely an access violation for trying to access memory that doesn't belong to you (resulting in abnormal program termination). Or the aforementioned carpet sellers.

2) Undefined. On many systems you'll get a exception of some kind resulting in abnormal program termination, just like in 1.

3) Google around "deep copy vs shallow copy".
Topic archived. No new replies allowed.