Comments (3)
Hi Cellman123!
First of all, I am glad you liked the article and that it helped you! C/C++ can be really tricky, specially at the beginning. So, don't worry about asking questions, we all were beginners at some point and it's extremely good to ask things you don't understand.
I think you understanding of memory allocators is good. Just to complete it, I would say that the REAL purpose of memory allocators is to allocate memory to fit the dynamic needs of your program. That is to say, data that you can't know in advance how much memory will need, so you cannot put that in the stack. In order to do this, we use the heap and memory allocations/deallocations to ask/return memory blocks. Said that, custom allocators try to improve performance of memory allocation/deallocation by working with an internal structure that better fit our data needs.
So, all allocate methods of my allocators return a pointer. You had trouble using this pointer because it's type is actually void *
. void pointers are a little bit special because they can point to anything. This effectively means that you cannot (directly) deference them because the compiler doesn't know its type. To get the value the pointer points to is up to the programmer to remember its type. This means that, if you allocate some memory and you get back a void pointer, is up to you to know the real data this pointer points to so that you can access its value:
void * voidPtr = myStack->Allocate(4, 4); // You allocate your int and get the void ptr after allocation
int * intPtr = (int *) voidPtr; // You remember it was an int, so you cast it back
int intValue = *intPtr; // Now you can deference the int ptr
So, the only thing you were missing was the cast to your-data-type ptr.
Now maybe you are wondering, why void pointers? This is so annoying. And you are totally right. Even I agree with you. But, while the usage of void pointers is ugly, dangerous and tricky it can be really useful in some situations. For example, in my case it allows me to allocate objects without having to worry about its type. (Maybe templates would be a solution here, but I did use void pointers on purpose to match the C implementation of malloc and free).
Here there's more info about void pointers, casts and allocations that may help you.
Maybe it was a too long explanation to just say that you missed the cast to your type from void pointer...Hehe
Hope this helps!
Mtrebi.
from memory-allocators.
@mtrebi, thank s so much for your explanation!
It all makes sense now... Maybe I'll try to make a templated implementation so you (the programmer) don't have to keep track of so many types and pointers along the way :)
Thanks again, -cellman123
from memory-allocators.
Cool! Keep me updated and tell me if you need more help 👍
from memory-allocators.
Related Issues (20)
- Can you share rest ppt of Dynamic memory ? HOT 2
- FreeListAllocator :: FindBest() question HOT 1
- I made mistake, sorry
- Bug in Free List Allocator on 32bit systems HOT 3
- Build errors HOT 3
- why use virtual method HOT 1
- find a small bug here HOT 2
- Make AllocationHeader::padding size_t
- StackAllocator is not performant and wastes memory on padding
- Optimize PoolAllocator initialization by adding an Offset variable?
- Found a bug in StackAllocator
- using malloc on custom memory allocator HOT 1
- Can't compile the code. HOT 4
- Question about FreeListAllocator HOT 2
- add simple example HOT 1
- Error while compiling in MSVC HOT 3
- consider adding tests HOT 1
- AllocationHeader is not copied from stack to the memory position HOT 2
- Potential infinite loop caused by linked list node pointing to itself HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from memory-allocators.