Skip to content

Commit cff1a8a

Browse files
robert-andrzejukColin Robertson
authored andcommitted
Inidicate thta code blocks contain C++ (MicrosoftDocs#206)
1 parent 1033c6f commit cff1a8a

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

docs/cpp/cpp-type-system-modern-cpp.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The concept of *type* is very important in C++. Every variable, function argumen
3636

3737
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.
3838

39-
```
39+
```cpp
4040

4141
int result = 0; // Declare and initialize an integer.
4242
double coefficient = 10.8; // Declare and initialize a floating
@@ -83,7 +83,7 @@ int maxValue; // Not recommended! maxValue contains
8383
## const type qualifier
8484
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.
8585

86-
```
86+
```cpp
8787

8888
const double PI = 3.1415;
8989
PI = .75 //Error. Cannot modify const variable.
@@ -111,7 +111,7 @@ PI = .75 //Error. Cannot modify const variable.
111111

112112
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:
113113

114-
```
114+
```cpp
115115

116116
int* pNumber; // Declare a pointer-to-int variable.
117117
*pNumber = 10; // error. Although this may compile, it is
@@ -123,7 +123,7 @@ int* pNumber; // Declare a pointer-to-int variable.
123123

124124
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:
125125

126-
```
126+
```cpp
127127

128128
int number = 10; // Declare and initialize a local integer
129129
// variable for data backing store.
@@ -143,7 +143,7 @@ int* pNumber; // Declare a pointer-to-int variable.
143143

144144
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();`
145145

146-
```
146+
```cpp
147147

148148
void someFunction() {
149149
unique_ptr<MyClass> pMc(new MyClass);
@@ -174,4 +174,4 @@ void someFunction() {
174174
## See Also
175175
[Welcome Back to C++](../cpp/welcome-back-to-cpp-modern-cpp.md)
176176
[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

Comments
 (0)