Relative Content

Tag Archive for c#memory-management

C# Construct managed object on unmanaged memory

I am trying to construct a managed object onto unmanaged memory allocated from Marshal.AllocHGlobal(). Currently, when I try to invoke the constructor delegate from reflection data, I get an exception that the object is the wrong type. I believe that if I could get a vftable pointer to the object and copy it over to the memory allocation, I could fool the language into invoking the constructor, but just getting a pointer to a static instance of the object is complicated already, and I don’t know if the vftable pointer is in the first 8 bytes let alone if I could even copy it. Is there some other way to do this without all of this crap?

issue erasing elements in a vector using C

I wanted to go “back to the basics” and try writing a C vector implementation.
It is using void* to store the data and i try to mimic the C++ counter part a little bit.

C Can I use data that I freed just after freeing it

I have a linked list node I want to code a function that frees node.
But order to do that I have to update next variable of previous node.
Can I access to previous_node->next (previous_node->next->next) just after freeing it?

How to track even dynamic memory allocation for a certain class?

I understand I can provide a custom allocator for most of the stl containers. If I record memory allocations in this custom allocator, then I can keep track of memory allocations. For example, with the help of MyAllocator in std::vector<T, MyAllocator<T>>, we can alway tell how much memory is consumed by this vector.