Skip to content

Commit cb27c3f

Browse files
committed
cpp: improve format of new a bit
1 parent 494c324 commit cb27c3f

2 files changed

Lines changed: 53 additions & 65 deletions

File tree

cpp/algorithm.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ int main() {
114114
int v3[]{5, 5, 1, 2, 3};
115115
std::copy(std::begin(v1) + 1, std::end(v1) - 1, std::begin(v2) + 2);
116116
assert(std::equal(std::begin(v2), std::end(v2), std::begin(v3)));
117+
118+
// Pointer usage.
119+
auto p1 = &v1[0];
120+
auto p2 = &v2[0];
121+
auto p3 = &v3[0];
122+
std::copy(p1 + 1, p1 + 4, p2 + 2);
123+
// Nah. Why.
124+
// https://stackoverflow.com/questions/26909429/stdbegin-and-stdend-not-working-with-pointers-and-reference-why
125+
//std::copy(std::begin(p1) + 1, std::begin(p1) + 4, std::begin(p2) + 2);
117126
}
118127

119128
/* Therefore, a more elegant class approach that uses exact same syntax as arrays. */

cpp/new.cpp

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,26 @@
1-
/*
2-
# Dynamic memory
3-
4-
C++ replaces C's malloc and free with new and delete.
5-
6-
It is very rare to need explicit dynamic allocation. Always use:
7-
8-
std::vector<int> is(n)
9-
10-
instead of:
11-
12-
int *is = new int[n];
13-
14-
and `vector<int>(n)` is as efficient and much more flexible than `new int[N]`.
15-
16-
Dynamic allocation is a dangerous, and it may lead to hard to debug memory leaks.
17-
18-
Always encapsulate dynamic allocation inside class constructors,
19-
and free it the destructor, like `std::vector` does.
20-
21-
Never use C style malloc on C++ code. `new` works better with classes.
22-
23-
# new
24-
25-
Allocate dynamic memory.
26-
27-
Throw `std::bad_alloc` in case of error.
28-
29-
# realloc
30-
31-
There is no direct replacement to realloc or calloc as of C++11
32-
http://stackoverflow.com/questions/3482941/how-do-you-realloc-in-c
33-
*/
1+
/* # new
2+
*
3+
* # delete
4+
*
5+
* malloc and free in C++.
6+
*
7+
* As of C++11, you should almost never use new and delete, only:
8+
*
9+
* - containers like std::vector
10+
* - smart pointers like unique_ptr + make_unique
11+
*
12+
* # realloc
13+
*
14+
* There is no direct replacement to realloc or calloc as of C++11
15+
* https://stackoverflow.com/questions/3482941/how-do-you-realloc-in-c
16+
*/
3417

3518
#include "common.hpp"
3619

3720
int main() {
38-
/*
39-
Basic usage with proper error checking.
40-
*/
21+
// Basic usage with proper error checking.
4122
{
42-
int* ip;
23+
int *ip;
4324
try {
4425
ip = new int[5];
4526
} catch(std::bad_alloc& ba) {
@@ -49,25 +30,24 @@ int main() {
4930
delete[] ip;
5031
}
5132

52-
/*
53-
# delete
54-
55-
Free memory allocatedby `new`.
56-
57-
Just like C `free`:
58-
59-
- deleting a `NULL` does nothing.
60-
- deleting any other pointer twice can lead to memory corruption
61-
- deleting a pointer which was not dynamically allocated can lead to memory curruption
62-
63-
Destructor of object pointed to is called.
64-
65-
A common technique is to set a pointer to `NULL` after it is deleted,
66-
to avoid deleting a pointer twice:
67-
<stackoverflow.com/questions/4190703/is-it-safe-to-delete-a-null-pointer>
68-
69-
An even better techinque may be to use smart pointers and containers.
70-
*/
33+
/* # delete
34+
*
35+
* Free memory allocated by `new`.
36+
*
37+
* Just like C `free`:
38+
*
39+
* - deleting a `NULL` does nothing.
40+
* - deleting any other pointer twice can lead to memory corruption
41+
* - deleting a pointer which was not dynamically allocated can lead to memory corruption
42+
*
43+
* Destructor of object pointed to is called.
44+
*
45+
* A common technique is to set a pointer to `NULL` after it is deleted,
46+
* to avoid deleting a pointer twice:
47+
* https://stackoverflow.com/questions/4190703/is-it-safe-to-delete-a-null-pointer
48+
*
49+
* An even better technique may be to use smart pointers and containers.
50+
*/
7151
{
7252
delete (int*)NULL;
7353
delete[] (int*)NULL;
@@ -90,16 +70,16 @@ int main() {
9070
{
9171
callStack.clear();
9272
NoBaseNoMember* cp = new NoBaseNoMember;
73+
// Same.
9374
//NoBaseNoMember* cp = new NoBaseNoMember();
94-
//SAME
9575
assert(callStack.back() == "NoBaseNoMember::NoBaseNoMember()");
9676

9777
cp->method();
9878

9979
callStack.clear();
10080
delete cp;
81+
// Calls destructor.
10182
assert(callStack.back() == "NoBaseNoMember::~NoBaseNoMember()");
102-
//calls destructor
10383
}
10484

10585
{
@@ -141,11 +121,10 @@ int main() {
141121
//ip was not allocated after delete!
142122
}
143123

144-
/*
145-
# calloc
146-
147-
An analogue effect to calloc can be attained with *value-initialization*.
148-
149-
http://stackoverflow.com/questions/808464/c-new-call-that-behaves-like-calloc
150-
*/
124+
/* # calloc
125+
*
126+
* An analogue effect to calloc can be attained with *value-initialization*.
127+
*
128+
* http://stackoverflow.com/questions/808464/c-new-call-that-behaves-like-calloc
129+
*/
151130
}

0 commit comments

Comments
 (0)