logoalt Hacker News

Why is the first C++ (m)allocation always 72 KB?

117 pointsby joelsikstoday at 9:27 AM25 commentsview on HN

Comments

anonymousiamtoday at 8:44 PM

So basically, before any of the code even runs, this environment begins by gobbling up more than the total RAM that most of my first computers had (SYM-1, IAMSAI-8080, Ferguson Big Board, Kaypro II, and CCS S-100 Z-80). All of these systems were 8-bit, with various RAM sizes from 8KB to 64KB. That was the maximum RAM available, and it was shared by the OS and the applications.

show 1 reply
pjmlptoday at 2:25 PM

This is compiler specific and cannot be generalised as C++.

show 3 replies
jebarkertoday at 5:49 PM

Reading this was a good reminder not to be intimidated by assumptions about complexity. (Without giving it much thought) I would have assumed that it would be hard to replace malloc for such fundamental applications as ls, but it's surprisingly simple.

show 3 replies
Joker_vDtoday at 1:03 PM

Huh. Why is this emergency pool not statically allocated? Is it possible to tune the size of this pool on libc++ startup somehow? Because otherwise it absolutely should've been statically allocated.

show 1 reply
throwaway2037today at 11:46 AM

I would like the see the source code for libmymalloc.so, however, I don't see anything in the blog post. Nor do I see anything in his GitHub profile: https://github.com/jsikstro

Also, I cannot find his email address anywhere (to ask him to share it on GitHub).

Am I missing something?

show 2 replies
cendynetoday at 8:45 PM

This was a fun little share. Thanks for writing it up!

aliveintucsontoday at 3:24 PM

I think you should read up on what "always" means.

znpytoday at 3:36 PM

> TLDR; The C++ standard library sets up exception handling infrastructure early on, allocating memory for an “emergency pool” to be able to allocate memory for exceptions in case malloc ever runs out of memory.

Reminds me of Perl's $^M: https://perldoc.perl.org/variables/$%5EM

In Perl you can "hand-manage" that. This line would allocate a 64K buffer for use in an emergency:

    $^M = 'a' x (1 << 16);