Hello everyone, welcome to the second part of the programming tutorial.. If you haven't readed the first part, I strongly advise you to read it, it should be the previous file. So, when we've already covered the basics syntax, lets begin with some little harder concepts. For example, lets say you want to save something to a file. I chosed this specifically because alot of programming languages to it alot of different ways. So, lets see some of them! First lets start with C and C++. C has a similiar syntax as basic console printing. ``` FILE *file; char *filename = "output.txt"; file = fopen(filename, "w"); fprintf(file,"Hello, world\n"); fclose(file); ``` As you can see, its preety simple, just open the file, write something to it, close it and exit. Now, lets see how some more modern programming languages go. Lets see how C++ works, shall we? ``` std::ofstream ofs("output.txt"); ofs.write("Hello, world\n"); ofs.close(); ``` As you can see, C++ has it easily solved using classes. However, we could use something even more modern like C#! I am just gonna use a simple one liner for simplicity. `File.WriteAllText("output.txt", "Hello, world!\n");` So, thats how you do a thing like this, lets go deeper tho. What about using multithreading?.. well, you may ask what that even is. And thats a very good question. Multithreading basically means, basically your cpu has multiple threads(sometimes ever cores), which are like separate cpus inside the whole package. They all share same hardware interface, memory and clock, but all of them are kindof separate. By using multithreading, you can improve your program by extreme speeds, sometimes up to 32times. Its not all about speed tho, and threads can be also used for things such as recieving multiple tcp connections, etc. So, how to make one? Letme show you.. C++: void func(int arg1); std::thread t1(func, 10); t1.detach(); So, what have I done right here? I've simply first created a basic function, and then created an thread object(instance), that was then detached from the main thread. What does this all mean? Function, is like a separate part of your code, which you can use to organize your code, or as you can see in here, use it for other uses for example as threads. So, then I am creating an object. I would recommend my previous OOP tutorial to understand objects fully, but in simple terms, lets say you have a template, like a blueprint for a car. Then, you can create infinite amount of them, as long as you have the budget for it(memory, commonly heap), and time obviously(time). Each of these cars can have different color, little different details etc.(class members). It can also do things like drive, has functioning engine, which is about the same for every of these cars(the class functions), so we are creating `car` called thread, named t1 and we are passing some parameters with the function, which our `car` takes in mind when creating itself. The detach functions tells the cpu to continue with the thread on different thread(part of the silicon of the cpu), ideally another core. The cpu doesn't even have that many cores, but thats job for the gpu to have many cores, isnt it? Alright, back to the CPU programming, after we have multiple threads, whats next? Now, its propably a good time to talk about global variables. Global variables are variables in global scope, aka the root of your program, they commonly look like this: # includes int glob_var = 10; int main(); void funcs(){ int a = 5; } As you can see, the variable glob_var is not inside any function but rather out of everything. What does that mean? It means the variable can be accesses from anywhere, preety much anywhen. Atleast as long as the resources of the variable are active of course. So, why does everyone tell to not use these types of variables? Well, its complicated. First of all, these kinds of variables will be there for the entire lifespan of your program. This means that if you make like a big variable, and you program will be running in the background, it will never be freed, even if the variable itself is no longer needed. But even if it is freed, it will definetly error out. Because you should never, delete the resources of global variable before atleast adding a little warning up on it. Why? Well, if there is a global variable, someone will propably use it, since its easily accesible and they might need it for something. So, what is the solution? Depends on your usage, but eighter to use constants, or pointers to specific variables. How? Simply like this: const int glob_var = 10; // This cannot change, but good enough for some usages int* glob_var; We can also use defines, which are kindof like replacing it everywhere. You simply do #define 10 and then when you mention the in the code, the complier simply replaces it with its value, aka 10 in this case. What are pointers tho? Pointers are preety simple, and simpliest explanation is, its like an arrow in memory, showing location of something else in the memory. Lets say, the memory is right now 8x8 grid of data, and you can peek each one by specifying its index, so from 0 to 255, or from 1 to 256. Now pointer is like a sign in the cell, showing the index of a different cell. So lets say there is a cell in 64, containing the number 132, which means there is a pointer in the cell 64, that points to the memory location 132, where the glob_var is located. How do you say that the value is invalid tho? Well, simply by setting the pointer to zero. Why zero, why at the begining? Doesn't that make the cell zero uselless because you cannot point to it? Well in simple terms, yes, but its more complicated. What you need to know right now, is that the index zero commonly contains the dump of all VGA(video graphics array) modes from the BIOS(basic input output system) startup. This doesn't make it uselless for the bootloader tho, because it can deal with the memory without pointers or their validation. Now, could I get random programs memory by simply scanning the RAM? In theory, definetly, but realistically, it depends. On systems like unix, the programs commonly cannot access memory of other programs, but on other OS's its commonly not as limited. Even if you could do this tho, theres still one problem, and thats the memory size. The maximum size of the memory depends on your current computer, but lets say you have most memory you can have on a 32bit computer(8086). Maximum size for the 32bit unsigned integer is around 4.28bilion, and thats also the maximum you can archieve in bytes, so you would still have over 4bilion options, and thats alot of options for where could it be. This brings me to another point, and that is the memory types. You can have several of these, depending on what are your requirements, but the basic ones are signed and unsigned, with the bittage(size in bits) being 8, 16, 32 or newly even 64. The 8bit integer is commonly used to store text represented in encodings such as ascii or utf8. Then there is the 16bit one, which can commonly be used for various uses, such as a network port, because its the minimally sized int to store all ports one PC can have(assuming 1 normal(non enterprise) IPv4 interface). Then there is the 32bit one, which is commonly abused alot. People use it where they don't have to, thus wasting the memory and sometimes speed. People for example use it for loop in indexing when the maximum is definetly lower than 2 bilion even when they know so, but these errors can often automatically be resolved by complier optimizations, etc. Why isn't there a 4, 24, or 48bit integer then? Mainly because of confusion, but the 4bit integer has a valid reason for it. The thing with integers less than 1byte is that they still do take up that one byte, because you simply are not allowed to split a byte in memory. That doesn't mean people haven't used them in the past tho, and they had actually alot in older devices, such as old game consoles(NES), really old computers, etc. But in today standards when the memory size is measured in bilions, for most people 4bits is just not useful for any kindof memory save. Now, you may have understood already that programming is mainly about numbers, and you aren't wrong, but you also aren't completely correct, because in fact, programming is primarily, sometimes even only about numbers! Thats right, everything on your computer is just a number, just a data point. Even the blog you are currently reading. The CPU is reading i8's from the string, matching them up with the i64 int of the color of the font, with which its then being drawn on the canvas, then switching the framebuffers before doing that all over agin, 60times per second(likely), and constantly, util you turn off your PC, or maybe after you leave it on forever, before the power dies and the PC will slowly discharge all the capacitors before calmly, peacefully shutting down. And thats about it for now! Good luck until next time..