Using C, is there a safer way to allocate memory than using malloc? For example, is there a C equivalent of a smart pointer, or some handy function that someone has come up with that makes the process of allocation a little cleaner? I mean, sure, it's only one line anyway, but there must be a more concise way to get it done. I suppose the deallocation portion of this question is included in whether there is something similar to a smart pointer in C.
If you really feel C's standard memory management is too unsafe, you should avoid it by either switching to a a different programming language that does memory management for you or by using a garbage collector that also does memory management for you, for example Boehm's:
http://www.hpl.hp.com/personal/Hans_Boehm/gc/
Smart pointers help in C++ but still need awareness, i.e. when you build cyclic structures.
You could write your own garbage collector. If you can't find one or don't have that kind of time, then the answer is no. That's one of the major reasons that people use C++, the automated resource management is win beyond belief compared to malloc/free. If you can afford the performance of a GC, just move to a faster (to develop in) language. The only other solution is to go to C++, which neither requires a garbage collector nor manual memory management.
By using malloc and realloc, followed by extensive testing with Valgrind. Using a GC like Boehm's will bite you in the ass later. If you're using C, use C. One lib worth looking into is halloc.
The safest way to allocate memory is to simply learn how it works properly. If you want a safe language, use a safe one. Java has automated garbage collection, for one.
If you want to use C, your best bet is to embrace it in all its glory. You probably will be burnt at some point but, with pain, comes experience.
One thing I've always found useful for students is to think of every allocation as having one peice of extra information, the responsibility. You pass responsibility for a memory block around with the memory itself and whoever is the last man given the responsibility is the one that must free it.
In addition, no two pieces of code can have reponsibility at the same time. In other words, when you pass memory to another party, you are not allowed to touch it until it's given back.
The process of allocation seems enough clear: each malloc-ed block should be free-ed. The simplest next step (before GC) that comes into mind to me is to write a function that "tracks" your allocation, so that at the exit of your code/function you can free everything you need no more in a single function call (and of course another function allows to free single block as usual). This would add a little bit of memory usage, but once implemented properly would make it easier to free blocks allocated one by one in one single shot.
The usage would be something like:
MemPool *pool = mempool_create();
// use
char *t = mempool_alloc(pool, size);
// ...
char *b = mempool_alloc(pool, size2);
// ...
int *c = mempool_alloc(pool, xy * sizeof(int));
// free a single piece for whatever reason...
mempool_free(pool, b);
// ...
// when you finished with this "pool":
mempool_destroy(pool);
This should at least assure that everything allocated "in" the "pool" is free-ed with mempool_destroy
, so in the worst usage you could just alloc everything with the "pool" mechanism, and free everything when you don't need it anymore (e.g. at the exit of a function). (Likely "pool" is not an appropriate name)