1622055540
Let’s talk about all the complex things that go into actually executing simple C++ code. We’re going to talk about memory, compilers, assembly code, and more, so buckle down and let’s see how this executes.
1. We create a new stack frame by assigning our stack frame register, S=0.
2. We allocate 16 bytes of memory on the stack for A1. An int has 4 bytes, char has 1 byte, and double has 8 bytes. Each field needs to be aligned as a multiple of its field size. For example, ints can only be defined at an address that’s a multiple of 4. Basically, the compiler optimizes the alignment of the fields so that they can be read into memory with one read. Computers read in 32 or 64 bits of memory at a time (depending on your computer’s system) starting at multiples of 32 or 64 addresses, so you save an extra read by aligning the memory. If this concept isn’t too familiar, take a look at https://fresh2refresh.com/c-programming/c-structure-padding/.
3. The constructor for A1
is invoked on S+0 and turns the allocated memory into useful bits. Before the constructor, the memory is just garbage.
4. For cout << A1.c
, memory address S+8 is fetched from memory and stored in register r1. The assembly would roughly look like READ S+8, r1
. All operations, including loading and storing, are done through registers. You can’t just print out memory S+8, instead, you need to load that into a register and then print out the register.
5. Next, we create a new stack frame so that we can call foo
. We do this by saving the current S on the stack (prev_S) and updating the S register to 16 because we’ve currently used 16 bytes of memory in our previous stack for storing A1
.
6. Next, we need to load in the arguments for foo
on the stack. We push the constant 3 onto the stack, push S+0, $3
.
7. We also need to push a pointer to A1
to the stack, push S+4, &A1
(in real assembly you need to load memory for &A1
into a register and then push it to the stack). Why would we want to push &A1
to the stack if it isn’t an argument for foo
? Well, since the actual instructions for foo
are shared among all A objects, foo
by itself doesn’t have access to the specific A object that called it. So, the compiler will implicitly pass the address of the A1
object to foo
, so foo
can run on this specific A object. This is why we have the “this” pointer in C++ classes. The “this” pointer is implicitly passed to all methods and implicitly put before each field access or method call within the class.
8. Now that our stack frame and arguments are all set up, we call foo
by moving the program counter (pc) to the address of the foo
instructions. a pc is a special register that points to the current instruction line. Simply moving the pc to the start foo
makes the computer execute foo
.
9. When foo
finishes, it will write the return value of the function to a special register, EAX. Also, the S got set to its prev_S=0 because we’re done with the foo
function. Note that the things on the stack weren’t actually cleared, but rather the S, was just moved back to its previous position.
10. Now, we cout
out the EAX.
11. Our main stack is about to end which triggers the destructors of all objects that have been created. A1
’s destructor is invoked and the program exits.
#assembly #stack #c++ #computer
1624240146
C and C++ are the most powerful programming language in the world. Most of the super fast and complex libraries and algorithms are written in C or C++. Most powerful Kernel programs are also written in C. So, there is no way to skip it.
In programming competitions, most programmers prefer to write code in C or C++. Tourist is considered the worlds top programming contestant of all ages who write code in C++.
During programming competitions, programmers prefer to use a lightweight editor to focus on coding and algorithm designing. Vim, Sublime Text, and Notepad++ are the most common editors for us. Apart from the competition, many software developers and professionals love to use Sublime Text just because of its flexibility.
I have discussed the steps we need to complete in this blog post before running a C/C++ code in Sublime Text. We will take the inputs from an input file and print outputs to an output file without using freopen
file related functions in C/C++.
#cpp #c #c-programming #sublimetext #c++ #c/c++
1597937354
If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends. The following displays some of the issues:
The below code generates no error since a print function can take any number of inputs but creates a mismatch with the variables. The print function is used to display characters, strings, integers, float, octal, and hexadecimal values onto the output screen. The format specifier is used to display the value of a variable.
A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a non-negative integer in the range [0 to 4294967295]. The signed integer is represented in twos-complement notation. In the below code the signed integer will be converted to the maximum unsigned integer then compared with the unsigned integer.
#problems-with-c #dicey-issues-in-c #c-programming #c++ #c #cplusplus
1590587580
In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.
C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.
#c #c# #c++ #programming-c
1589816580
In this article, we’ll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let’s look at how to use this function, using some simple examples.
#c programming #c++ #c #c#
1589791833
C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.
The goal of this course is to provide you with a working knowledge of C++. We’ll start with the basics, including syntax, operators, loops, and functions. This Course will explain you how to use data structures and create your own Functions. This Course will show you the details of the powerful object and template systems so you can create useful classes and objects.
Youtube channel: ProgrammingKnowledge - https://www.youtube.com/watch?v=_SH1T3y_D7o
#c #c# #c++ #programming-c