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:

No comments:

Post a Comment