Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - gerard

Pages: [1]
1
Ideas / Vectors in combination with semi-automatic memory management
« on: January 03, 2017, 07:05:20 PM »
I’ve been playing with runtime grow-able “arrays” (vectors), in combination with semi-automatic memory cleanup (RAII and “fat pointers”).

C has lots of memory issues such as use after free(), forgotten free() and no bounds checking in combination with pointer arithmetic. Most “higher level” languages deal with this with automated memory management, mostly GC -which is user friendly-, and in case of Rust it’s with the lifetime guarantees -which is pretty hard-. My idea is to use “fat pointers”, a couple of rules and two new types: vectors and slices.

This is how it works. All malloc’ed data is automatically free’d when the function goes out of scope. This can be enabled with using “fat pointers” and initialization of these fat pointers with zero. This means that when a function goes out of scope the only thing it has to do is check whether the pointer is zero or not and in the latter case call free(), at all “exit points” of the function (maybe with an under the hood “goto cleanup”). Except in the case when a function returns a vector of course. Function parameters are also excluded from automatic cleanup. Free() can still be used inside the function that has the vector variable, for instance to limit memory consumption, but not in other functions, to keep it clear. This is a pretty large distinction between C but at the same time it also reduces hard to find errors.

Well, this is the teaser. I have quite a lot of detail in my mind but not written it down yet. If you’re interested I will put it down, it’s probably a page or two/three so it could take a week or so.

2
General Discussion / Question: how are growable arrays implemented?
« on: December 29, 2016, 07:07:33 PM »
Just curious...

Pages: [1]