Now I am wondering what people thought malloc was doing :) If you didn't know it called the OS to change the virtual memory mapping for your process, what _did_ you think it was doing?
-
-
Replying to @cmuratori @beast_pixels
I just didn't know. It was a black box to me, I knew that it gives me more virtual memory, but I didn't know how it does this. Once I tried to read the source code of malloc, but it is so complicated, I couldn't find the exact place where the magic was happening... :(
1 reply 0 retweets 1 like -
Replying to @JustSlavic @beast_pixels
Not sure if you're on Windows or Linux, but here is a Linux example: https://github.com/bminor/glibc/blob/ff417d40178b7363b08516091f74c0b6615456ee/malloc/malloc.c#L2502 … It has a whole macro dance it does to make syscalls, but this is the actual syscall it will end up doing: https://man7.org/linux/man-pages/man2/mmap.2.html …
1 reply 1 retweet 4 likes -
So every time you call malloc, it looks to see if it has some memory it can give you already. If it doesn't, it will eventually call mmap, which asks the OS for more virtual memory to be mapped for your process. Then malloc takes that new memory, and gives you some of it.
2 replies 0 retweets 6 likes -
The same thing happens when you free. The CRT will just say "thanks" and keep that memory around to give to the next person who calls "malloc", presumably using some heuristic where if there is a lot of freed memory, eventually call the OS unmap call to give some back to the OS.
1 reply 0 retweets 3 likes -
Replying to @cmuratori @beast_pixels
That clears so many things! Thank you very much! I only don't understand why this is not common knowledge... I even feel a little embarrassed.
2 replies 0 retweets 5 likes -
I want to ask one more thing: if you don't use CRT, don't you have to make your own, which will just repeat a lot of stuff in CRT, that is really complicated (it surely looks complicated when I look at it), and probably make more bugs/inefficiencies?
3 replies 0 retweets 0 likes -
Replying to @JustSlavic @beast_pixels
It depends on the context. Writing your own file layer is very simple and almost always better than the CRT. It is much easier, for example, to call CreateFile() and WriteFile() on Windows and get good results than it is to deal with fopen/fwrite.
1 reply 0 retweets 5 likes -
Memory management is a different story. I would say you do have to be at a certain level of expertise in order to move to your own memory scheme. But, once you are, it is _vastly_ better than calling malloc.
1 reply 0 retweets 5 likes -
Hey Casey, as someone that is trying to learn his way through computers and has only recently discovered you, thanks for the free high quality lessons such as this. You rock!
1 reply 0 retweets 7 likes
You're very welcome! Good luck with your endeavors :)
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.