You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp/cpp-type-system-modern-cpp.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ The concept of *type* is very important in C++. Every variable, function argumen
36
36
37
37
The following example shows some simple variable declarations with some descriptions for each. The example also shows how the compiler uses type information to allow or disallow certain subsequent operations on the variable.
38
38
39
-
```
39
+
```cpp
40
40
41
41
int result = 0; // Declare and initialize an integer.
42
42
double coefficient = 10.8; // Declare and initialize a floating
@@ -83,7 +83,7 @@ int maxValue; // Not recommended! maxValue contains
83
83
## const type qualifier
84
84
Any built-in or user-defined type may be qualified by the const keyword. Additionally, member functions may be `const`-qualified and even `const`-overloaded. The value of a `const` type cannot be modified after it is initialized.
The first thing that you should know is declaring a raw pointer variable will allocate only the memory that is required to store an address of the memory location that the pointer will be referring to when it is dereferenced. Allocation of the memory for the data value itself (also called *backing store*) is not yet allocated. In other words, by declaring a raw pointer variable, you are creating a memory address variable, not an actual data variable. Dereferencing a pointer variable before making sure that it contains a valid address to a backing store will cause undefined behavior (usually a fatal error) in your program. The following example demonstrates this kind of error:
113
113
114
-
```
114
+
```cpp
115
115
116
116
int* pNumber; // Declare a pointer-to-int variable.
117
117
*pNumber = 10; // error. Although this may compile, it is
The example dereferences a pointer type without having any memory allocated to store the actual integer data or a valid memory address assigned to it. The following code corrects these errors:
125
125
126
-
```
126
+
```cpp
127
127
128
128
int number = 10; // Declare and initialize a local integer
However, it is easy to forget to delete a dynamically-allocated object- especially in complex code, which causes a resource bug called a *memory leak*. For this reason, the use of raw pointers is strongly discouraged in modern C++. It is almost always better to wrap a raw pointer in a [smart pointer](../cpp/smart-pointers-modern-cpp.md), which will automatically release the memory when its destructor is invoked (when the code goes out of scope for the smart pointer); by using smart pointers you virtually eliminate a whole class of bugs in your C++ programs. In the following example, assume `MyClass` is a user-defined type that has a public method `DoSomeWork();`
145
145
146
-
```
146
+
```cpp
147
147
148
148
voidsomeFunction() {
149
149
unique_ptr<MyClass> pMc(new MyClass);
@@ -174,4 +174,4 @@ void someFunction() {
174
174
## See Also
175
175
[Welcome Back to C++](../cpp/welcome-back-to-cpp-modern-cpp.md)
176
176
[C++ Language Reference](../cpp/cpp-language-reference.md)
177
-
[C++ Standard Library](../standard-library/cpp-standard-library-reference.md)
177
+
[C++ Standard Library](../standard-library/cpp-standard-library-reference.md)
0 commit comments