Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Compiler optimize malloc/free or new/delete pair into alloca

Is there any mature C/C++ compiler, capable of optimizing malloc/free (or new/delete) pairs info alloca? In other words, convert from heap-based memory to stack-based (ONLY for some limited cases).

This optimization may be allowed only for pair of malloc/free when both functions are in the same function (or even in the same block of {}), and free is called every time when malloc is called. Also, lets consider that pointer to malloced memory is not saved in some global variable.

So, will GCC/LLVM+clang/Intel Compiler convert such block of code:

{
   char *carray;
   carray = malloc(100);          // or malloc(N)
   // some string-like work with carray
   free(carray);
}

into

{
    char*carray;
    carray = alloca(100);  // or if(N<const1) carray=alloca(N);else carray=malloc(N)
    // the same work
    // nothing                       // or if(N>=const1) free(carray)
}

This conversion may be not very useful for every program, but I think, there may be some special compiler option.

PS (update1) We can limit our discussion only to cases when compiler Knows that malloc and free is from libc (stdlib)

like image 628
osgx Avatar asked Apr 28 '12 15:04

osgx


People also ask

What is the difference between New delete and malloc free?

The main difference between new and malloc is that new invokes the object's constructor and the corresponding call to delete invokes the object's destructor. There are other differences: new is type-safe, malloc returns objects of type void* new throws an exception on error, malloc returns NULL and sets errno.

Can we use delete with malloc?

Can I delete pointers allocated with malloc()? No! It is perfectly legal, moral, and wholesome to use malloc() and delete in the same program, or to use new and free() in the same program.

Can I use delete instead of free?

Both are used for same purpose, but still they have some differences, the differences are: delete is an operator whereas free() is a library function. delete free the allocated memory and calls destructor. But free() de-allocate memory but does not call destructor.


2 Answers

There's an off-shoot of LLVM called poolalloc that does this optimization. It's being maintained as part of SAFECode, and isn't in the mainline LLVM distribution.

It's described in Chris Lattner's PhD thesis and in this PLDI paper. The code is here.

like image 108
ohmantics Avatar answered Oct 22 '22 10:10

ohmantics


Technically, the compilers can optimize anything as long as they follow the As-If rule.
So, optimizing heap allocations to stack allocations would be possible but do to the compiler needs to be intelligent enough to probe the usage and determine that changing the allocation to stack won't affect the observable behavior of the program.

I am not aware of any compiler which does this.

like image 2
Alok Save Avatar answered Oct 22 '22 12:10

Alok Save