Skip to main content
The 2024 Developer Survey results are live! See the results
69 votes
Accepted

Implementing realloc in C

The code leaves a bad first impression because of how it is formatted. In particular, the extra whitespace between the return type and the function name, and that between the variable's type and its ...
Cody Gray - on strike's user avatar
52 votes
Accepted

C++ string operators

Don't include unnecessary headers We're not using <vector> or <stdio.h>, so drop them. We only need ...
Toby Speight's user avatar
  • 77.3k
29 votes

C++ Vector Clone

Issues Your vector assumes that the type being stored has a default constructor. arr = new element[size]; Also this is really inefficient for expensive to ...
Loki Astari's user avatar
  • 95.5k
28 votes
Accepted

Zero-initializing large dynamically allocated arrays of double

So my first hunch when I read your question is: Okay this guy is making a complicated solution to a simple problem based on a hunch that the trivial solution is "grossly inefficient" ...
Emily L.'s user avatar
  • 16.6k
21 votes
Accepted

Taking arbitrary length input in C

A simple improvement to improve performance by reducing the number of reallocs is to start with a decently sized buffer and when it fills up, grow it by a multiple of the size. For example, you could ...
mleyfman's user avatar
  • 5,170
19 votes
Accepted

C++ Doubly Linked List Implementation

Not a bad first crack at a linked list. Here's some things that are obvious issues to me: Your getters and setters for list_node_generic aren't really ...
StoryTeller - Unslander Monica's user avatar
19 votes
Accepted

Customization of memory class C++

Try to be more consistent with naming I'm seeing camelCase, PascalCase and snake_case all mixed together. Pick one style and stick with it. Furthermore, I see redundant things in names like ...
G. Sliepen's user avatar
  • 58.8k
17 votes

Macro for allocation in C

p = malloc(n*sizeof *p); This is dangerous if n gets large, because the multiplication could overflow. After the overflow, too little memory has been allocated but ...
jpa's user avatar
  • 381
17 votes
Accepted

Dynamic array implementation using C++

First of all, there is a major problem with your class: it does not respect the rule of three/five/zero: it has a non trivial destructor that destroys memory allocated in constructor it neither ...
Serge Ballesta's user avatar
16 votes
Accepted

C++ class for aligning objects on the stack

Silent bug: Bad move constructor Your move constructor will perform a copy because your parameter is aligned_type const&&; you cannot move from a ...
user2296177's user avatar
  • 3,543
16 votes
Accepted

Weapon and Hero classes

This is a design issue, and there's no single answer. We need to consider what's important about a Weapon. Is it value-like, or is its identity important? The ...
Toby Speight's user avatar
  • 77.3k
15 votes

Implementing realloc in C

Generally, malloc, realloc and free are all part of the same library. One of the things ...
Neil's user avatar
  • 301
15 votes

Weapon and Hero classes

@Toby Speight describes how it should probably be done. But I want to describe what is happening in your case so that you understand the concept of copy construction and assignment. The problem is ...
Loki Astari's user avatar
  • 95.5k
15 votes

malloc() and free() for Linux with system calls

Names beginning with _ are reserved for the implementation; I'm surprised that -Wpedantic doesn't help with this! Include what ...
Toby Speight's user avatar
  • 77.3k
15 votes
Accepted

C++ dynamic array implementation

Welcome to C++, and welcome to Code Review. C++ memory management is, as you probably have realized, tough and error-prone. There are many things that can easily go wrong. Assuming that no exception ...
L. F.'s user avatar
  • 9,570
15 votes

Implementation of a two-dimensional array in C

You are doing too many allocations. The usual way of implementing multidimensional array is to allocate one continuous buffer and map n-dimensional coordinate to a 1-dimensional one. You then provide ...
slepic's user avatar
  • 5,477
15 votes
Accepted

malloc() and free() implementation

Documentation: Be more definite in your descriptions. Does the null pointer returned on requesting zero bytes indicate a failure? In that case, shorter and unambiguous: "Returns a pointer to the ...
Deduplicator's user avatar
  • 19.3k
14 votes
Accepted

C++ Vector Clone

First of all, lets answer the questions you have: Yes, no, maybe. Some people do it one way, some the other. Personally, I prefer splitting declarations and definitions up so that I have an overview ...
Ben Steffan's user avatar
  • 5,228
14 votes
Accepted

Implementation of a two-dimensional array in C

Enable more warnings, and use a memory checker: ...
Toby Speight's user avatar
  • 77.3k
13 votes

Implementing realloc in C

You are checking if malloc succeeded before calling memcpy, but not before calling memset. ...
D. Jurcau's user avatar
  • 3,010
13 votes
Accepted

Dynamic memory management for a class hierarchy of geometric shapes

Dynamic Memory Management Am I doing it right in terms of memory menagment? You should rather use smart pointers to store the Shape instances in the vector: <...
πάντα ῥεῖ's user avatar
13 votes
Accepted

Python's .split() implemented in C

Minor stuff ... Allocate to the object, not type The below is easier to maintain. ...
chux - Reinstate Monica's user avatar
12 votes

Zero-initializing large dynamically allocated arrays of double

In the 99.9% of cases, your architecture will use IEEE floats/doubles, and using calloc() is ideal for both simplicity and speed. calloc() is far more efficent when allocating large zeroed memory ...
Max Filoli's user avatar
12 votes

C++ string operators

Don't include what you don't use. You only use <string.h> and <stdio.h>. While including ...
Deduplicator's user avatar
  • 19.3k
12 votes
Accepted

Yet another implementation of malloc() in C

Design. The trouble with your design is that you have to search a list. Where none of the sizes may match. Even if there is a block in the list of the correct size you have to search through all the ...
Loki Astari's user avatar
  • 95.5k
12 votes

malloc() and free() implementation

Macros The implementation uses a lot of macros that would be better as functions/constants, or even open-coded. This one in particular is problematic: ...
Toby Speight's user avatar
  • 77.3k
11 votes
Accepted

C++ String class

You claim that your String is faster than std::string I do not believe this claim. Please post your benchmark code. ...
Emily L.'s user avatar
  • 16.6k
11 votes

Implement `Dispose` method in C#

I would suggest that you read these pieces from MSDN: Basic Dispose Pattern, Implementing a Dispose Method Some things that immediately stand out from your example: Replace your ...
Lachlan Stewart Kidson's user avatar
11 votes

C++ string operators

Memory leak here (in operator+): c = new char[strlen(newArray)]; this->c = newArray; because there is no difference ...
jvb's user avatar
  • 899

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