Giter Club home page Giter Club logo

Comments (3)

mtrebi avatar mtrebi commented on July 20, 2024

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.

stickyfingies avatar stickyfingies commented on July 20, 2024

@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.

mtrebi avatar mtrebi commented on July 20, 2024

Cool! Keep me updated and tell me if you need more help 👍

from memory-allocators.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.