Using C, is there a safe way to allocate memory by using malloc? For example, is it equal to a smart pointer, or some handy function that has come with someone, making the process of allocation clear slightly? I mean, sure, this is just one line, but there must be a more concise way to accomplish this. I think the delocution of this question is similar to a smart pointer in Part C
The process of allocation is quite clear: Each Molok ed block should be free-ed. The simplest next step (before GC), which comes to my mind, is to type a function that "tracks" your allocation, so that when you exit the code / function, you can fulfill the need for a function call without any (And of course, another function normally allows a single block to be freed). This will add a little memory usage, but once it is implemented properly it will be easy to block the blocks allocated to a single shot free.
The usage will be:
MemPool * pool = mempool_create (); // use char * t = mempool_alloc (pool, size); // ... char * b = mempool_alloc (pool, size 2); // ... int * c = mempool_alloc (pool, xy * sizeof (int)); // Free for a single piece for whatever reason ... mempool_free (pool, b); // ... // When you end with this "pool": mempool_destroy (pool); It should at least be convinced that "everything" is allocated free with "code" mempool_destroy in "pool", so in the worst use you Only you can assign everything with the "pool" mechanism, and whenever you do not need it, all for free (for example, if you exit a function). (Probably "pool" is not an appropriate name)
Comments
Post a Comment