23
votes
Accepted
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 ...
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 ...
15
votes
Accepted
Custom implementation of `std::unique_ptr<T>`
First impressions are that this is pretty good code. Most of my comments will be nit-picks.
We include a lot of irrelevant headers. The unique_ptr type ...
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 ...
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 ...
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 ...
12
votes
My shared_ptr implementation
Not going to do a full review, because I don’t review code from old language versions (C++17 is now 2½ versions old), but there is one thing I’d like to point out that is language-version agnostic.
...
12
votes
I implemented FFT in C
double complex sub_even[N/2];
double complex sub_odd[N/2];
These are VLAs and they have the usual problem: it's easy to cause a stack overflow this way. No big ...
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.
...
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 ...
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.
...
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 ...
11
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., ...
11
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:
<...
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 my ...
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 ...
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 ...
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 ...
10
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 ...
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 ...
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 ...
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 ...
10
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
pointers × 410c++ × 263
c × 115
c++11 × 94
memory-management × 61
linked-list × 56
beginner × 38
reinventing-the-wheel × 33
strings × 31
array × 30
object-oriented × 17
c++17 × 16
c++14 × 14
template × 13
performance × 11
stack × 11
design-patterns × 10
tree × 10
c# × 9
algorithm × 9
raii × 7
rust × 6
queue × 6
vectors × 6
reference × 6