Skip to main content
23 votes
Accepted

Implementing a Stack with Templates and Smart pointers

Readability Please put a space after the include and before the < ...
Loki Astari's user avatar
  • 95.4k
23 votes
Accepted

My implementation for std::unique_ptr

Overall Ohhhhh. Is it well-formed? It compiles, so yes. i.e. does it follow common C++ standard and patterns (for example, should private members be declared before public ones? Personally I think ...
Loki Astari's user avatar
  • 95.4k
23 votes
Accepted

C++11 smart pointer 'library'

I'll hit the red flags first, and then review the details. ...
Quuxplusone's user avatar
  • 19.4k
18 votes
Accepted

Array Dynamic resize in heap

If you had tagged this code as C, it would have been acceptable. Since you tagged it as C++, it's horrible. Instead of writing your own swap function, there's ...
Roland Illig's user avatar
  • 21.3k
17 votes
Accepted

String comparison using pointers

You don't need pointers to character pointers at all: ...
coderodde's user avatar
  • 28.9k
16 votes
Accepted

Minimum of two floating-point numbers

Versatility The pointer version is less versatile. If the two values come from expressions like in min_value(x + 13, 2 * y), you can't use the pointer version, as ...
Ralf Kleberhoff's user avatar
14 votes

Trim leading/trailing space in a string

There are indeed a few things that can be improved. Simplify your argument There is no need to take a char**, a simple char* is ...
Matthieu M.'s user avatar
  • 5,277
12 votes
Accepted

Singly Linked List implementation C++

I like this. #ifndef LINKEDLIST_H #define LINKEDLIST_H But it is not very unique. If used in a big project this may clash with other guards. I usually include ...
Loki Astari's user avatar
  • 95.4k
12 votes
Accepted

C - Learning Linked Lists, Pointer Manipulation - Store some ints, print and free memory

Naming It's unconventional to name a type with all-uppercase - we normally reserve those names for preprocessor macros, to warn readers that they need treating with care. Avoid such names for ...
Toby Speight's user avatar
  • 77.3k
11 votes
Accepted

My linked list implementation in C

I suggest you rename link to next which is a more conventional name for the next linked list node. ...
coderodde's user avatar
  • 28.9k
11 votes
Accepted

Finding first recurring letter in each string of a given set

Better names The variable names are too short to be meaningful. It's common to use i as an index in for-loops, but ...
Zeta's user avatar
  • 19.3k
11 votes

C++11 smart pointer 'library'

Let's have a look at some examples where it fails. Rule of Three You have not correctly over written the assignment operator. ...
Loki Astari's user avatar
  • 95.4k
11 votes

malloc in main() or malloc in another function: allocating memory for a struct and its members

Looking at the performance, the two versions should perform just about identically. The second version has one less call/return, which can save a couple of CPU cycles, but if you have it multiple ...
1201ProgramAlarm's user avatar
11 votes
Accepted

Using smart pointers to construct Binary Search Tree

Answers to your questions Am I using std::unique_ptr correctly here? Using std::move and get? Yes, you are using those correctly. Is there any way for me to make ...
G. Sliepen's user avatar
  • 58.7k
11 votes
Accepted

"Smart" (ish) pointer in C

Disclaimer: The next few lines are my opinion as a C programmer so take it with a grain of salt. As a C programmer, I will never use this. The language is verbose enough already due to its lack of ...
Ajinkya Kamat's user avatar
10 votes

Singly Linked List implementation C++

don't do this: using namespace std; This will create a lot of namespace collisions. Writing std:: where you need to isn't ...
ratchet freak's user avatar
10 votes
Accepted

Graceful thread shutdown with std::move

volatile bool thread_shutdown = false; This is definitely not correct. volatile is not for thread safety. What you want here ...
indi's user avatar
  • 12k
10 votes
Accepted

Binary Search Tree implementation using smart pointers

please do not using namespace std; why has Bst::findmin no prefix _? why do you use ...
Cornholio's user avatar
  • 911
10 votes
Accepted

malloc in main() or malloc in another function: allocating memory for a struct and its members

In C, initialization and destruction should be done at the same level of abstraction. This is important because it defines who is responsible for the memory. There are two good ways to follow this ...
sudo rm -rf slash's user avatar
10 votes
Accepted

Hunt the Wumpus

Many of the member functions of Room can be const since they don't modify the object data (e.g., ...
1201ProgramAlarm's user avatar
10 votes

Hunt the Wumpus

Legacy C-style headers These: #include <stdio.h> #include <stdlib.h> #include <time.h> are deprecated. Instead, use the C++-style headers: <...
S.S. Anne's user avatar
  • 1,725
10 votes
Accepted

C pointer based growable stack

I suggest that you do more error checking: malloc or realloc may fail. I can initialize the stack with negative capacity. I can ...
L. F.'s user avatar
  • 9,570
10 votes

Minimum of two floating-point numbers

Some beautifying I think first < second ? first : second is a tiny bit more readable. First comes first. Double problems Floating points are tricky. What will ...
Pavlo Slavynskyy's user avatar
9 votes

Traversing an array with a pointer to the array

It's not terrible as it stands, but the code could be improved. Here are some ideas on how to do so. Minimize work within the loop If we redefine ptrLastElement ...
Edward's user avatar
  • 66.5k
9 votes
Accepted

Implementing a binary tree in C++ using "std::unique_ptr"

On raw and smart pointers First, I'll address your specific question, about using a raw pointer to refer to nodes in the tree. I believe you're doing exactly the right thing here, because smart ...
Toby Speight's user avatar
  • 77.3k
9 votes
Accepted

C++ Shared_Ptr implementation

Overview Templated classes "normally" should provide all the code at the point of usage. As a result you should probably not have a separate shared.cpp. Though some people put the ...
Loki Astari's user avatar
  • 95.4k
9 votes

Implementation of itoa which allocates the string

Counting the digits doubles the amount of work, and the division/modulo is indeed very hard work for the CPU. You know the maximum size of the resulting string, because it would be the largest or most ...
JDługosz's user avatar
  • 11.4k
9 votes

Left Shift/ Right Shift an array in C

You know, C and C++ are different languages. You used both tags, and your code is confused as to which it wants to be. It appears to use C syntax, library calls, and ways of doing things; but it ...
JDługosz's user avatar
  • 11.4k
9 votes

"Smart" (ish) pointer in C

It isn't possible to implement smart pointers sensibly in C because it lacks "RAII". It would be possible to implement a garbage collector by multi-threading, but one of the benefits of ...
Lundin's user avatar
  • 4,690

Only top scored, non community-wiki answers of a minimum length are eligible