C++ and Data Types
Chronicles of C++ Memory Management - Part 1
First things first - thanks for being a subscriber and Happy New Year! Hope the 2023 will bring everything that previous one failed to deliver :)
I announced a bit earlier that I want to do a deep-dive into all the ways you can deal with memory in C++. Long-term idea is to explore HOW is memory allocated, how we can influence that, WHY would we do it and, finally, discuss intricacies between different environments (with a main focus on Windows, obviously). There seems to be A LOT to unfuddle here, and lots that I want to cover, but as usual I want to start with common ground and make sure we are all on the same page. Hence, opening the series with the very basics themselves:
This might be quite boring and common knowledge for seasoned devs, but a good refresher lesson for those of us who kept offloading memory management to somebody else. We kind of forget that at lowest level, all our code is about moving bits around and doing some arithmetic over them.
C++ hides the intricacies of moving those bits around by providing a bit of an abstraction layer over it, while simultaneously adding some runtime stuff that help us write more human-readable code. Fun fact that I read recently, which is something that everybody knows but obviously we forget it quickly - "as far as machine is concerned, there is no such thing as 'write line' to output". From machine's POV, you are just moving set of bits (representing your text) from one place (e.g. RAM) to another place (e.g. output device, like Monitor). But it's still all about bits!
C++ does offload a lot from your shoulders, but still requires you to specify how many bits you want to use. And this makes sense because you gain most efficiency if you are in the driver's seat. The moment you offload this to a bookkeeping agency (e.g. a Garbage Collector), you are effectively adding another layer and additional bookkeeping job. Somebody has to nurture those bits and ensure that ones that are not needed any more are 'deleted'.
That's where the Data Types jump in. They provide a more human-readable interpretation of 'how many bits' do I want to deal with. So you still need to think about sizes (yes, size matters) but at least in more humane way (e.g. Integers, Floating point numbers, Characters, etc.). Additionally, and this is a fun bit (pun intended) of C++ history - it's primary intention was to be "C with Classes" (according to author, at least). So C already gave you a better framework to play with Bits, but C++ wanted to allow you to instill behavior behind them.
Anyway, this is a first article of a Series to follow. Next one will deal with "where do I store my bits", discussing popular topic of Stacks, Heaps and other funny made-up places :)
Thanks for reading Bitesized Engineering! Subscribe for free to receive new posts and support my work.