Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++ runtime always require malloc()?

I have a C++ application running bare-metal that I want to make as small as possible.

I am not using dynamic memory allocation anywhere. I am using no STL functions. I've also overridden all the varieties of "delete" and "new" with empty functions. Nonetheless, when I look at a sorted list of symbols I see that malloc() is still one of the largest items in my compiled binary. I could shrink my binary by about 25% if I could get rid of it.

Do C++ runtimes generally require malloc() for behind-the-scenes type work?

(I'm using Xilinx's fork of gcc for the Microblaze architecture, if that matters)

like image 794
Barry Gackle Avatar asked May 16 '15 04:05

Barry Gackle


1 Answers

Reliance of a program on malloc() can occur in both C and C++, even if the program doesn't directly use them. It is a quality of implementation matter for the compiler and standard library rather than a requirement by the standards.

This really depends on how the both the compiler startup code (code that sets things up so main() can be called) works and how standard library code is implemented.

In both C and C++, for example, startup code (in hosted environments) needs to collect information about command line arguments (possibly copy to some allocated buffer), connect to standard files/streams (like std::cout and std::cin in C++, and `stdout and stdin in C). Any of these things can involve dynamic memory allocation (e.g. for buffers associated with standard streams) or execute code that is not actually needed by the program.

like image 146
Peter Avatar answered Nov 14 '22 14:11

Peter