Questions tagged [memory-management]

Memory management is the act of managing computer memory by allocating portions of memory to programs as well as freeing memory so that it can be re-used.

Filter by
Sorted by
Tagged with
8 votes
5 answers
5k views

Macro for allocation in C

As we all know, the syntax of allocating memory is a bit clunky in C. The recommended way is: int *p; int n=10; p = malloc(n*sizeof *p); You can use ...
klutt's user avatar
  • 903
5 votes
2 answers
329 views

Two dimensional gaussian image generator in C

This is a follow-up question for Image Processing Median Filter in C. I am attempting to create a two dimensional gaussian image like below in C. The formula is as follows. The experimental ...
JimmyHu's user avatar
  • 3,984
31 votes
1 answer
3k views

Yet another 'any' class implementation, named 'some'

This is a follow-up of 'any' class implementation. After posting my answer, I kept working on the code towards parameterizing with respect to memory management as I had promised. I ended up in some ...
iavr's user avatar
  • 2,274
27 votes
1 answer
20k views

Efficient smart pointer implementation in C++

The idea behind this is mainly educational but I might even consider using it in reality if turns out to be good. Here's my first try at implementing smart pointers: ...
mmirzadeh's user avatar
  • 411
10 votes
3 answers
7k views

Simple Pool Allocator

This is a simple pool allocator, based (to at least some degree) on a previous answer. The basic idea is pretty simple: allow a user to allocate objects of some type quickly (in relatively large ...
Jerry Coffin's user avatar
  • 33.5k
8 votes
2 answers
19k views

STL Stack Implementation

I implemented std::stack from the STL for deeper understanding of the language and memory since I still am only a beginner. I implemented the stack using a singly ...
Scholar's user avatar
  • 83
8 votes
3 answers
941 views

Non-fragmenting pool allocator

This is a fairly straightforward object allocator/factory, which sets up a stack of freed nodes as a linked list in the unused part of the storage. I would appreciate any and all feedback you may have....
user avatar
8 votes
1 answer
2k views

Fast Native Memory Manipulation in VBA

I've been quite annoyed lately by the fact that the CopyMemory API (RtlMoveMemory on Windows and ...
Cristian Buse's user avatar
8 votes
1 answer
2k views

Huffman Coding library implemented in C

I have written a small library for C that implements the Huffman coding algorithm as outlined in David Huffman's paper on Minimum-Redundancy Codes, and a small test program to implement it. The ...
jess's user avatar
  • 1,378
6 votes
4 answers
8k views

Texture managing

I'm new to C++ and SDL, and I've written a Texture manager class whose purpose is to help me manage sprites and other textures. I have a ...
MattMatt's user avatar
  • 593
4 votes
0 answers
691 views

A fixed-size dynamic array

Background I implemented the historic std::dynarray according to the specification under the name dynamic_array in C++17. ...
L. F.'s user avatar
  • 9,456
4 votes
2 answers
373 views

Reusable storage for array of objects

My goal is to have a memory pool non-template class that is used to store arrays of objects. The same memory pool object must be reusable for a different array (difference size, different type and/or ...
Oersted's user avatar
  • 299
4 votes
4 answers
952 views

Image Processing Median Filter in C

I am attempting to perform Median Filter with size 3 x 3 in C language. The function MedianFilter33 has been implemented as follows. The experimental implementation ...
JimmyHu's user avatar
  • 3,984
3 votes
1 answer
51 views

Reusable storage for array of objects V2

Here is a follow up on Reusable storage for array of objects, taking into account the provided answers. The following code should be compliant at least with gcc, <...
Oersted's user avatar
  • 299
2 votes
1 answer
561 views

Contiguous memory allocation using first-fit algorithm

This project I'm dealing with will read the size of free memory segments and size of processes from a text file and then will try to allocate a memory partition for each process using the first-fit, ...
Serkan Gün's user avatar
16 votes
1 answer
57k views

Alternative to setInterval and setTimeout

JavaScript's setTimeout() and setInterval() are evil and not precise: Both functions have a delay of a varying quantity of ...
cocco's user avatar
  • 303
13 votes
3 answers
7k views

Structure to ByteArray Extension

I have a need to turn various structures into byte arrays to be sent over serial port to another machine. I created generic extensions to turn any structure into a byte array and from a byte array ...
RubberDuck's user avatar
  • 30.8k
13 votes
4 answers
394 views

Bare-bones string library

After years of criticizing others, I've finally found the time and worked up the courage to polish up one of my bits of code and solicit criticisms of my own. This is a simple dynamic-string library ...
Corbin's user avatar
  • 10.6k
12 votes
2 answers
2k views

Implementation of a lock-free fixed-sized allocator

This question now has a follow-up: Implementation of a lock-free fixed-sized allocator - follow-up - with commented code I've tried implementing a lock-free fixed-size allocator while trying to ...
cr_oag's user avatar
  • 565
11 votes
5 answers
2k views

Static factory function and lifetime

I'm trying to teach myself C++ at the moment, after years of C# and other managed languages. The class in question is a Level in a game, and the idea is to ...
Michael Stum's user avatar
11 votes
1 answer
113 views

Longest lines using priority queue

Inspired by a comment on this question, I thought I'd exercise my C skills by implementing something like it using a priority queue. The idea is to be able to invoke the program like this: ./...
Edward's user avatar
  • 65.3k
10 votes
4 answers
3k views

Simple key-value store in C, take 2

Followup to Simple key-value store in C. Response to previous reviews You may want to hide the KVSstore structure Done. Add a comparison function pointer to KVSstore. If this pointer is null, ...
Dagg's user avatar
  • 4,531
10 votes
3 answers
57k views

Implementing an ArrayList

I implemented ArrayList functionality in C as follows: ...
Amr Ayman's user avatar
  • 853
10 votes
1 answer
233 views

C++ imitation of glibc malloc(), free()

I wrote a C++ simulator that performs algorithms for glibc malloc() and free(). The core logic is basically the same, with the ...
frozenca's user avatar
  • 1,693
8 votes
2 answers
2k views

Leak detection simple class

Basic idea is to use a Class, with static methods to add and remove references in a static vector, that keeps track of these references, and check that vector upon exit. The class is detecting ...
User's user avatar
  • 93
7 votes
2 answers
16k views

Simple key-value store in C

I needed a simple key-value store. Here's what I came up with. Usage Use kvs_create and kvs_destroy to create stores and clean ...
Dagg's user avatar
  • 4,531
7 votes
1 answer
2k views

Hashing a SecureString using Cryptography Next Generation

Goal: I needed to calculate the salted hash of a password, where the password is stored as a SecureString. There doesn't seem to be much .NET framework support for SecureString. In particular, none ...
jnm2's user avatar
  • 403
7 votes
2 answers
9k views

Simple C++ object pool

I am studying computer science and just for the sake of practise I thought of implementing an object pool. This is my first implementation, it is simple and works correctly, but I would love any ...
amc176's user avatar
  • 345
7 votes
2 answers
545 views

A lightweight version of std::shared_ptr<T>

I wrote a simple lighter version of std::shared_ptr<T> called res_ptr<T>. ...
ALX23z's user avatar
  • 2,253
6 votes
1 answer
612 views

Locating the largest Collatz Sequence with memoization

Inspired by this question I wrote a memoized form of the collatz sequence calculator. The idea was to cache the sequence lengths for as many sequences as possible, from 0 to some value. I managed to (...
Der Kommissar's user avatar
6 votes
1 answer
254 views

Pool allocator for a scripting language parser

At the moment I'm writing a parser and interpreter for a custom scripting language, to learn more about the basic concepts involved and to get more familiar with tools like ...
glampert's user avatar
  • 17.1k
5 votes
3 answers
2k views

Are these memory-allocation wrapper functions kosher with all C compilers?

I have never been entirely comfortable using malloc() and free() directly. For one thing, 99% of the time that I would like to ...
Todd Lehman's user avatar
5 votes
3 answers
486 views

Inheriting from std::auto_ptr to support deletion of allocated arrays in C++98/C++03

[EDIT: Thanks for your opinions. I think this topic is discussed enough. Since std::auto_ptr is removed from C++17, I decided to drop the idea of inheriting from <...
Gergely Tomcsányi's user avatar
5 votes
2 answers
1k views

Python algorithm mimicking mark/sweep garbage collection

I don't have much experience with Python and would love some feedback on my implementation of this program. I am given a file with an integer representing a number of memory-cells, followed by a list ...
user3776749's user avatar
5 votes
2 answers
3k views

Adding two BigInts using linked lists in C

While trying to learn more about linked lists, I thought I should try the exercise of reading two BigInts from an input file and then adding them up. My strategy was to store the two numbers in two ...
sc_ray's user avatar
  • 1,213
5 votes
1 answer
4k views

Vigenère cipher in C

For my second major project in C, I decided to write an implementation of the Vigenère cipher in C. My program uses command line options (optarg) and can read from both a file or from a string ...
jess's user avatar
  • 1,378
5 votes
4 answers
3k views

ADT stack with a dynamic array

I'm starting to learn about data structures and coding in C. The stack is the simplest of all data structures so I'm trying to implement an ADT stack with dynamically allocated array in C. This ...
diwakar_wagle's user avatar
4 votes
1 answer
72 views

simple shared pointer implementation in single threaded environment

Update: The code below has some obvious problems, and it is not allowed to revise code in the original question, so I have to post a new question, the revised code is here. ...
frank's user avatar
  • 85
4 votes
3 answers
416 views

Simple shared pointer implementation in a single threaded environment (revised)

std::shared_ptr does not stop you from having the same resource managed by multiple control-blocks (and thus independent sets of shared-pointers), even though it is ...
frank's user avatar
  • 85
4 votes
1 answer
895 views

Template double ended queue (deque) implementation using dynamic allocation C++

I have written my own deque class to get a better grasp of C++ pointers and memory management. The following code is part of a larger 2D isometric game I am developing. It compiles and runs fine using ...
TOM__'s user avatar
  • 105
4 votes
2 answers
2k views

Memory management for Red-Black Tree

I've written this program yesterday, and I was reminded why I dreaded C++ before turning to Java: pointers and associated terms (like destructors, initializations and copy/move constructors) are ...
Stefan Stanković's user avatar
4 votes
1 answer
1k views

Iterative Red-Black Tree (dynamic stack)

I've completely reworked the previous code, posted here, so that it is a bit more correct regarding dynamic memory management, and also a bit more true to the object-oriented paradigm. ...
Stefan Stanković's user avatar
3 votes
1 answer
2k views

Generic Graph Abstract Data Type Implementation in C++

Today I tried to code up a generic graph abstract data type in C++. Precisely, I implemented the adjacency list data structure for a generic graph type. I think it seems to work with different type ...
AnkitSablok's user avatar
3 votes
3 answers
168 views

Singly Linked List in pure C

I recently learned about linked lists and here is my first try to write one in C. I know it's a lot of code, but I hope someone takes a look. Any tips memory allocation and improvements are welcome. ...
LastSecondsToLive's user avatar
2 votes
0 answers
40 views

Reusable storage for array of objects V4

Here is a thirdfollow up on Reusable storage for array of objects, Reusable storage for array of objects V2 and Reusable storage for array of objects V3, taking into account the provided answers. The ...
Oersted's user avatar
  • 299
2 votes
2 answers
2k views

Using std::vectors to store textures

I need to write a TextureManager class which stores Texture objects; the problem is I'm using an ...
MattMatt's user avatar
  • 593
2 votes
0 answers
35 views

Fit memory allocation [duplicate]

The program will read size of free memory partitions and size of processes from a text file and then will try to allocate a memory partition for each process using the first-fit algorithm. Input ...
bro's user avatar
  • 21
2 votes
2 answers
961 views

Creating an optimized, fully functional TextureManager in SDL

After I've discussed pointer semantics with Loki Astari previously, I finally managed to code the TextureManager class using auto_pointers. But the problem is that ...
MattMatt's user avatar
  • 593
2 votes
1 answer
1k views

Vigenere Cipher in C

I'm going through CS50 and one of the problem sets was to implement a vigenere cipher. I have had some previous classroom experience with C before CS50, but still consider myself a beginner. The ...
lg43's user avatar
  • 23
2 votes
1 answer
496 views

Force ConcurrentDictionary in a Singleton Registry to Collect Removed Items/Space

I have the following class which is used in a WebApi AspNetCore application as a singleton instance to store Strategy objects (reference type). The class is ...
MoonKnight's user avatar