-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptr.cpp
More file actions
45 lines (37 loc) · 799 Bytes
/
ptr.cpp
File metadata and controls
45 lines (37 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
*
*
* @name ptr.cpp
*
* @version 1.0
* @date 12/28/2014 (09:15:37 PM)
* @revision none
* @compiler gcc
*
*
* @author P. Di Giglio (github.com/pdigiglio), <p.digiglio91@gmail.com>
* @company
* @brief
*
* Example usage:
* @code
* @endcode
*
*
*/
#include <iostream>
int main ( ) {
int *a = new int(3);
std::cout << a << " = " << *a << std::endl;
delete a;
// why can I still use the pointer?
std::cout << a << " = " << *a << std::endl;
*a = 1;
std::cout << a << " = " << *a << std::endl;
a = nullptr;
// why can I delete it _again_ (even if it's an invalid address)?
delete a;
// segmentation fault
std::cout << a << " = " << *a << std::endl;
return 0;
}