Showing posts with label memory. Show all posts
Showing posts with label memory. Show all posts

Tuesday, January 15, 2013

A note on garbage value, memory allocation & variable auto intialization

// variable are global
#include <iostream>
using namespace std;

    int x,y,z,c,x2,x3;
    //y = x;
    //z = y;       // This gives error
    //c = z;

    int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;

int main()
{
    y = x;
    z = y;
    c = z;
   
    cout << "\n" ;
    cout << " value at x2: " << x2 << endl;
    cout << " value at x3: " << x3 << endl;
    cout << " value at  x: " << x << endl;
    cout << "Address of x: " << &x << endl;
    cout << " value at  y: " << y << endl;
    cout << "Address of y: " << &y << endl;
    cout << " value at  z: " << z << endl;
    cout << "Address of z: " << &z << endl;
    cout << " value at  c: " << c << endl;
    cout << "Address of c: " << &c << endl;
   
    cout << "\n\n";
    cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
    cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
    cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
    cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
    cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
    cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
    cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
    cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
    cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
    cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
    cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
    cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
    cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
    cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
    cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
    cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
    return 0;
}


OUTPUT:

 ----------------------------------------------------------------------------------------------
// variable initialization all in main not global
#include <iostream>
using namespace std;

int main()
{
    int x,y,z,c,x2,x3;
    y = x;
    z = y;
    c = z;
   
    cout << " value at x2: " << x2 << endl;
    cout << " value at x3: " << x3 << endl;
    cout << " value at  x: " << x << endl;
    cout << "Address of x: " << &x << endl;
    cout << " value at  y: " << y << endl;
    cout << "Address of y: " << &y << endl;
    cout << " value at  z: " << z << endl;
    cout << "Address of z: " << &z << endl;
    cout << " value at  c: " << c << endl;
    cout << "Address of c: " << &c << endl;
   
    int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;
    cout << "\n\n\n " << endl;
   
    cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
    cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
    cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
    cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
    cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
    cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
    cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
    cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
    cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
    cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
    cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
    cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
    cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
    cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
    cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
    cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
   
    return 0;
}


OUTPUT:

 ----------------------------------------------------------------------------------------------
 // variables in function
#include <iostream>
using namespace std;

void test()
{
    int x,y,z,c,x2,x3;
    y = x;
    z = y;
    c = z;
   
    cout << " value at x2: " << x2 << endl;
    cout << " value at x3: " << x3 << endl;
    cout << " value at  x: " << x << endl;
    cout << "Address of x: " << &x << endl;
    cout << " value at  y: " << y << endl;
    cout << "Address of y: " << &y << endl;
    cout << " value at  z: " << z << endl;
    cout << "Address of z: " << &z << endl;
    cout << " value at  c: " << c << endl;
    cout << "Address of c: " << &c << endl;
   
    int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;
    cout << "\n\n\n " << endl;
   
    cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
    cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
    cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
    cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
    cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
    cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
    cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
    cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
    cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
    cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
    cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
    cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
    cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
    cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
    cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
    cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
}

int main()
{
    test();
    return 0;
}


OUTPUT:

-----------------------------------------------------------------------------------------------
// variable in class ; using function ; object on STACK
#include <iostream>
using namespace std;

class Demo{
    public:
        int x,y,z,c,x2,x3;
        int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;  
   
    void test()
    {
        y = x;
        z = y;
        c = z;
       
        cout << "\n" ;
        cout << " value at x2: " << x2 << endl;
        cout << " value at x3: " << x3 << endl;
        cout << " value at  x: " << x << endl;
        cout << "Address of x: " << &x << endl;
        cout << " value at  y: " << y << endl;
        cout << "Address of y: " << &y << endl;
        cout << " value at  z: " << z << endl;
        cout << "Address of z: " << &z << endl;
        cout << " value at  c: " << c << endl;
        cout << "Address of c: " << &c << endl;
   
        cout << "\n\n";
        cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
        cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
        cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
        cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
        cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
        cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
        cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
        cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
        cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
        cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
        cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
        cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
        cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
        cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
        cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
        cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
    }
};

int main()
{
    Demo d;
    d.test();
    return 0;
}


OUTPUT:

-----------------------------------------------------------------------------------------------
// variable in class ; using function ; object on HEAP
#include <iostream>
using namespace std;

class Demo{
    public:
        int x,y,z,c,x2,x3;
        int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;   
   
    void test()
    {
        y = x;
        z = y;
        c = z;
       
        cout << "\n" ;
        cout << " value at x2: " << x2 << endl;
        cout << " value at x3: " << x3 << endl;
        cout << " value at  x: " << x << endl;
        cout << "Address of x: " << &x << endl;
        cout << " value at  y: " << y << endl;
        cout << "Address of y: " << &y << endl;
        cout << " value at  z: " << z << endl;
        cout << "Address of z: " << &z << endl;
        cout << " value at  c: " << c << endl;
        cout << "Address of c: " << &c << endl;
   
        cout << "\n\n";
        cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
        cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
        cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
        cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
        cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
        cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
        cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
        cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
        cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
        cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
        cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
        cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
        cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
        cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
        cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
        cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
    }
};

int main()
{
    Demo *d = new Demo();
   
    d->test();
   
    delete d;
    d = 0;
    return 0;
}


OUTPUT:

Sunday, September 23, 2012

A note on Stack and Heap

  • Stack-memory and Heap-memory are physically the same.
  • The RAM may be used as stack memory when running one program and later used as heap memory when running some other program.
  • The difference is in how they are used.
Difference between Stack vs Heap memory:
 

Stack:
  • Often a function or method calls another function which in turn calls another  function etc.
  • The execution of all those functions remains suspended until the very last function returns its value.
  • All the information required to resume the execution of these functions is stored on the stack.
  • In particular, local variables are stored on the stack.
  1. Local variables are often stored for short amounts of time while a function/method block uses them to compute a task.
  2. Once a function/method has completed its cycle, the space on the stack used by all local variables is freed.
  • This chain of suspended function calls is the stack, because elements in the stack (function calls) depend on each other.
  • The stack is important to consider in exception handling and thread executions.

Heap:
  • The heap is simply the memory used by programs to store global variables. or All global variables are stored in heap memory.
  • Element of the heap (variables) have no dependencies with each other and can always be accessed randomly at any time.
  • All variables dynamically created by the program with "new()" or "malloc()" or similar commands are also stored on the heap.
  • In some programming languages, all instances of an object, including all the attributes of that instance, are stored on the heap (java).
  • In those programming languages, local variables of a function that have object type are implemented as creating the new object on the heap, and storing a reference to that object in the local variable, which is on the stack.
  • When that function exits, the heap memory used by each local variable that has object is freed, and then all the stack used by that stack is freed.

Similarties:
  • Both are used for providing memory to a program at run time.
  • both reside in main-memory (RAM)
  • both are datastructure (used to organised data)