About the Variables in C++

Size of the objects

When the objects are initialized, stored, operated, and returned in C/C++, they need to utilize the memory of the computer. We can know the size of the object by using the operator called sizeof().

The sizes of some common objects are listed in the table below:

| Object Type | Typical size (bytes) | | :--- | ---: | | char | 1 | | short | 2 | | int | 4 | | long | 8 | | long long | 8 | | ----- | ----- | | float | 4 | | double | 8 | | long double | 16 |

Except for the float double and long double on the bottom, the objects on top of the table could be unsigned. The unsigned objects can shift the value range to the positive side. For example, the typical char can represent range [-128,127], the unsigned char can represent range [0,255]. The float types are positive already, so they do not have the unsigned type.

Unitialized Variable & Compiler Warnings

We might unitialize a variable but have not assigned a value to it, which would result in an error. We can use -Wall to raise the compiler warning. The compiling command would be g++ -Wall -o <name> <path.cpp>.

Array

The array used in C++ is old-fashioned. The syntax to define an array is: <type of variable in the array> <name of array>[num of variables] = {variables}. We can only have one type of element in the array. And, we need to define the number of elements in the array in advance. All is making the array in C++ less "playable".

Pointers and Memory

Any variable (object) in C++ has a designated place in memory to store it. We can play with that feature with the pointer. For example, we have a value of j: int j = 1234; And its address, or designated place, in memory is: &j which could be 0x7fff9eebe4ec We can have a pointer pj to point to that address: int * pj = &j Where pj == 0x7fff9eebe4ec, and *pj == 1234. Something cooler is that if we change the value of *pj after defining it as the pointer, the value of j would be changed as well.

Since memory is limited, sometimes we need to allocate it, with new, and deallocate it with delete. If we fail to deallocate the memory, it will result in a memory leak. Storing in a new pointer could be: double * data = new double[n_doubles]; (Storing the result in a new pointer) To delete it, or free the memory: delete [] data;

Vector

The vector in C++ is more like the list in python. #include <vector> was used to import it. The syntax is: std::vector<type of element> name_of_vector; // To define a vector name_of_vector.push_back(new_element); // like.append() in python name_of_vector.at(index); // To check the element with index name_of_vector.size(); // like len(list) in python

Typedef

If we want to define a specific type of object so that we can call it easier in C++, we can use typedef function. For example, I want to call the 3D array Atomcoord: typedef std::array<double, 3> AtomCoord; And I want it to be a vector so that I can add on it easier: typedef std::vector<AtomCoord> Coordinates; When I have a coordinate (1,2,3):

AtomCoord coord1 = {1.0, 2.0, 3.0};
Coordinates coords;
coords.push_back(coord1);

Last But Not Least

We learned a lot today. But we can now write the Monte Carlo Simulation, which was in Python before, in C++!!!