Stack vs Heap

In C and C++, the memory assigned to a program in typical architecture is divided into four parts:

  1. Code(Text): set of program instructions.
  2. Global/Static: For global variables and static variables.
  3. Stack: Information of function calls and local variables are stored in this part. Memory assigned to the stack depends on the compiler design.Example with code
    #include <iostream>
    using namespace std;
    
    int sum(int a,int b){
    return a+b;
    };
    
    int main()
    {
    int x = 5, y = 6;
    cout << "Sum of two numbers: " << sum(x,y) << endl;
    return 0;
    }
    

    In the above example, a stack is empty before the execution of program but when program starts executing and now the top of a stack is local variables (i.e. x and y) and after that top of the stack is cout instruction which calling to the sum function and now the top of the stack is sum function. The computer only executes the top of the stack instruction and when a top of the stack instruction is finished then the control of execution of the program is transfer to the below instruction (which is now the top of the stack because instruction on the top of this instruction is finished). When the stack is empty it means a program is executed.

    Note: If our stack is going beyond the reserved memory of the stack and our program exhausted all the memory of the stack called stack overflow and in this case, our program is crashed. For eg. bad recursion and our programs goes infinite loop.

  4. Heap (free store of memory): Allocating large amount of memory then we have a heap or giving the memory of the array at run-time. The programmer can totally control to how much memory can use from the heap and the size of the heap varying according to system time.
    To use dynamic memory of heap in C++ we use new and delete operators.Code:

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int *p;
        // Allocating memory of size int in heap using new operator
        p = new int;
        *p = 10;
        cout << "Value of p: " << *p << endl;
        delete p;
        return 0;
    }
    

Learning and challenging days

Today I made a few bash scripts which read the data from the file and then process that data in a meaningful way by using Grass software. I have also learnt stuff related to contour by Grass software.

One interesting thing happened today

When I have given my bash script to Amarjeet and he runs my bash script as sh myscript.sh and it gave an error “Unexcepted token error [[” but on my laptop was working fine. On my laptop I run my bash script as ./myscript.sh and then after some time I found the answer that why script gives an error on Amarjeet’s laptop. Actually, he runs my script in sh shell whereas I have written my script according to the bash shell and then I told to Amarjeet to run my bash script as bash myscript.sh or as ./myscript.sh.

Daily diary

Today, I tried to draw dimensions of a part by using the Draft workbench of FreeCAD. However, I can draw the dimensions of through coding but many things that I have to keep in mind like page offset and position of three vectors etc.

I also solved one program of “Roy and Profile Picture” (https://www.hackerearth.com/problem/algorithm/roy-and-profile-picture/) in C++. Here is the solution of the above problem.
https://github.com/amrit3701/Programs/blob/master/roy_and_profile_picture.cpp

Daily diary

Today, I didn’t worked so much but I solved one problem related the Arch module present in the FreeCAD on the Experimental server. Actually, when I imported Arch module in command line FreeCAD freecadcmd it gave a error “Exception while processing file: drawing.FCMacro [‘module’ object has no attribute ‘updateLocale’]”. It was the bug presented in the FreeCAD command line 0.14 version because in 0.14 version Arch module depended on GUI and in command line FreeCAD, GUI is not present. Then, I upgraded the command line FreeCAD to 0.16 version and after this my problem was solved.

Compilied Watertank project

Today, I compiled Water tank project code and trying to open .g format file in FreeCAD by converting .g format file to other format but unable to open it.

Other files include brep-files for objects and thumbnail of drawing. BesidesFreeCAD’s own file format, files could be exchanged by the following file formats: DXF, SVG (Scalable Vector Graphics), STEP, IGES, STL (STereoLithography), OBJ (Wavefront), DAE (Collada), SCAD (OpenSCAD), IV (Inventor) and IFC. (https://en.wikipedia.org/wiki/FreeCAD)

Output of Water tank project after compilation:

watertank