This is a post I’ve been meaning to write for a long time. I’ve had a couple of revelations when it comes to programming and thought that I should share those revelations. One of those revelations are connected to the memory management of c++ and the other is about data. Non specific data that is, but data nonetheless.

Memory management

One of the hardest things that I learned in c++ programming was the use of pointers. Now I understood early on that you allocated memory, but I never understood how it was done and what happens under the hood. I knew that you created an integer that pointed to an address in the computers memory, but it was a bit fuzzy how it worked. Later on we’ve learned about pointer pointers and that made me even more confused, what is a pointer to a pointer? I mean I understand the idea, just didn’t see how it could be utilized.

Anyway let’s just start from scratch and go from A to B. A computer nowadays have usually more than 4GB of ram, however for this case let’s just assume we’re not working in 64bit, which means that 32bit is the range of your memory. That means you have 2 in the power of 32 addresses to utilize, which is around 4294967296 integers. Out of those you don’t have that much memory to access, but in any case it’s there. In the old windows 32bit systems you didn’t see all of that memory either, but let’s just assume we have the whole range, with the exception of 0 which basically means you don’t point to anything. That however is one of the things I’m not sure of if you actually point to the address 0 and that it is interpreted by the OS to be an empty memory slot.

So to the revelation, I was looking at a game programming book that I bought for one of the courses and discovered that when you write:
int* num = new int;
The keyword new is actually an operator. You can overload that operator with your own structure if you want to, however you need to do that in the class that you are working with. From my understanding, however this might be wrong, a lot of the operators are something that you get as a bonus when creating a new class for instance, new being one of them, so in order to overload the operator new, you need to create a class which overrides the default new operator. Other operators like ++, — etc doesn’t do that, because in order for those to work the compiler needs to know exactly how it works, the class you have might have several numerical values, so in order to overload those you need to specify what you want to do when using the ++ or –. Then there might be a difference between post and prefix of those as well.

In any case, let’s say we have a class called Shape and for instance a class that inherits from that, that’s a base class that I’m planning on using for the game programming course that I’m currently attending.
class Cylinder : public Shape{
public:

void setRadius(float radius);
void setHeight(float height);
Vertice GetVertices();

private:

Vertice m_vertices[];

};
Anyway let’s say we have both the Shape and the Vertices class defined previously. So let’s say we assign a pointer for this class, we create a Cyllinder:
Cyllinder cyllinder = new Cyllinder;

How does the computer know how much memory to set aside? Well basically the new Cyllinder after the = sign is what does it for us. Basically you do a sizeof(Cyllinder) which returns the size of the class, you set aside the amount of memory needed for that class. Let’s say one Vertice is the same as 32bytes (8 floats for instance) it sets aside 32 bytes as a memory location for that one instance. Internally I believe it is the OS that keep track of all the memory locations, however what does that doesn’t really matter, what matters is that once the program runs and you have a memory location activated it won’t disappear until two things happen either that the program closes or secondly that you delete the memory location that you have reserved. If you don’t do the latter and you keep creating objects, you get a memory leak, actually you have a theoretical leak if you never delete anything even if you just create something once.

Pointer pointers

To illustrate this I made some really nice programming art:

drawing

What I want to show with the above illustration is that when you are using a pointer into a function or a method, the pointer that you send it is copied. The function / method then gets a new address. It points to the same address but the vec in the void Myfunc(Vec* vec) is copied from whenever you send it in. Same way as the objects are copied when you are sending in an int or another object. That’s why it’s common to use pointers to send in large data, because all you do is copying a 4 byte integer value instead of a potentially very large object. Using reference instead just refer the memory address you had in the start, not copying it and thus you get direct access to the pointer you send in.

Another example is to use a simple method and send in an int reference (int& ), it is a pointer to the integer you send in.

Data

Last bit is about data, however I have touched into the subject more heavily than I initially had thought, but in any case. I worked with someone in a project a while back and this one simple sentence made me understand more about programming in that one sentence. The phrasing I don’t recall exactly but it was along these lines: Programming is just about passing data from one point to another and handle the data that you get. Seeing all the information that you want to use as data makes it much easier. Because it makes it easier to think where the data should go, where’s the logical route it should take. How can you efficiently handle the data and what do you want to do with the data you get and have.

It’s all about creating a data flow, or a pipeline if you like. That’s why it is at least for me important to know how the data flows in the computer and how you can change the data, send it back and use it for later.

To mention one example, that was also a discovery for me. When reading binary data from a file for instance you read in chars. What you do however is turning those chars into something that you can use for a specific purpose. Let’s take an image as an example, you read in what kind of image, what colors the different pixels have, then you tell the hardware through layers of abstraction what pixels to light up, how much and what color they should have, this you do by reading in each of the characters, then depending on image you divide them up into different data types and coordinates. Let’s say the structure of a pixel is this:

struct Pixel{
short r,
short g,
short b,
short a,
short x,
short y
};

You would read in 12 bytes at a time, which is the size of a Pixel and assign them to a coordinate. Or let’s say you automatically fill from 0 to xmax then continue down one line further and that you save space by not including that information. Point is, you read in characters and assign them to a type that fits and that data is automatically arranged in the form you want, because you have instructed the computer to read the data in that manner and then present that information on the screen. Now it might not work exactly in that manner, but it is as I see it close enough to see that even a picture is in fact just data broken down into basic types, it is how you manipulate and change that data that affects the result you get.

Anyway this is how I have understood all of this, so if I’m way off base, please let me know 🙂