Hacker News new | past | comments | ask | show | jobs | submit login

Hehe, I once had a manager, who was also a programmer, asking me about the difference between new and malloc. He wasn't testing me, I was working at the company. His question was something like "Isn't it that if you use new you get virtual memory but with malloc you don't get virtual memory?"



...be aware that in Windows, there are two main ways to allocate memory: HeapAlloc(), which comes from your process heap and is equivalent to Posix malloc(), and VirtualAlloc(), which comes from the kernel and is the equivalent to allocating anonymous pages with mmap(). (Windows malloc() usually wraps HeapAlloc().)

So he's not necessarily completely wrong. If he's used to a system where the default C++ allocator uses VirtualAlloc() but malloc() still uses HeapAlloc(), then referring to memory allocated from VirtualAlloc() as 'virtual memory' makes sense, in a specialist weird-windows-terminology kind of way.

('Virtual memory', here, having nothing to do with virtual memory in the Linux sense.)


In Windows (as in most OS:es), any memory you allocate in user space is virtual, end of story.

Ok, not really end of story (I just said that because it's mostly true). You can allocate physical memory from user space but that is for very specific purposes and your general purpose CRT allocator (whereas new or malloc) will not be doing that.

Also (last I looked) the MSVC CRT "new" uses malloc.


Was he even a C programmer? I don't know if it matters if a Ruby or Java programmer knows new vs malloc.


Well, he had somehow managed to write an awful lot of C++. Most of the code base at that company was in old school C++ (huge monolithic classes). Since he was one of the founders I can only assume C++ was his main language.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: