Saturday, 26 July 2014

sizeof operator


//findreferencesize.C

#include<iostream>
using namespace std;


using namespace std;
struct student
{
        static int i;
        static int j;
}s;



int *x = new int(11);
int*  &j = x;
cout << "x: " << x << endl;
cout << "&x: " << &x << endl;
cout << "j: " << j << endl;
cout << "&j: " << &j << endl;
delete(x);
x = NULL;
cout << "x: " << x << endl;
cout << "&x: " << &x << endl;
cout << "j: " << j << endl;
cout << "&j: " << &j << endl;
cout << "size " << sizeof(&j) << endl;
cout << "size " << sizeof(x) << endl;
cout << "size " << sizeof(s) << endl;
cout << "size " << sizeof(s.i) << endl;
static int q;
cout << "size " << sizeof(q) << endl;

    return 0;
}
result:
x: 0x7f9359c000e0
&x: 0x7fff52cbcae0
j: 0x7f9359c000e0
&j: 0x7fff52cbcae0
x: 0
&x: 0x7fff52cbcae0
j: 0
&j: 0x7fff52cbcae0
size 8
size 8
size 1
size 4
size 4


//findsize.C

#include<iostream>
using namespace std;

struct bstree{
    static int info;
    bstree *left;
    bstree *right;
}obj;


int main()
{
    cout << "sizeof int is :" << sizeof(int) << endl;
    cout << "sizeof struct member pointer: " << sizeof(obj.left) << endl;
    cout << sizeof( bstree) << endl;

return 0;
}
result:

sizeof int is :4
sizeof struct member pointer: 8
16

//nonstatic.C 
#include<iostream>
using namespace std;


struct bstree{
    int info;
    bstree *left;
    bstree *right;
}obj;

int main()
{
    cout << "sizeof int is :" << sizeof(int) << endl;
    cout << "sizeof struct member pointer: " << sizeof(obj.left) << endl;
    cout << sizeof( bstree) << endl;

   cout << "static in size: ";

    static int x;
    cout << sizeof(x) << endl;


return 0;
}
result:

sizeof int is :4
sizeof struct member pointer: 8
24
static in size: 4
NOTE:C Program does allow storage type in structure, Above programs are in C++.
below is the code written in C.

//structsize.c

#include <stdio.h>
#include <stdlib.h>



struct node {
        static int val;
};

int main()
{
    struct node obj;

    printf("%d", sizeof(obj));

    return 0;
}

result:
 type name does not allow storage class to be specified.


//notallowed_initialization.c
#include<stdio.h>

int main()
{
     struct node
    {
        char name[] = "C skills";
        int  rank = 200;
    };

    struct node *ptr;
    printf("%d ", ptr->rank);
    printf("%s", ptr->name);
    getchar();
    return 0;
}
result:
Compiliation error
Reason:
When we declare a structure or union, we actually declare a new data type suitable for our purpose. So we cannot initialize values as it is not a variable declaration but a data type declaration.

//staticinstruct.c
#include<stdio.h>
struct node
{
    int m;
    static int n;
};

int main()
{
    printf("%d", sizeof(struct node));
    return 0;
}
    return 0;
}
result:
Compiliation error
Reason:
In C, struct and union types cannot have static members. In C++, struct types are allowed to have static members, but union cannot have static members in C++ also.

//sizeofstrucunion.c
#include<stdio.h>
int main()
{
    struct { 
          short s[5];
          union { 
               float y; 
              long z; 
         }u; 
    } t;

   printf("%d\n", sizeof(t.u));

   printf("%d\n", sizeof(t));

   printf("%d", sizeof(long));
   return 0;
}
result:
8
24
8
Reason: long is 8 bytes. short is 2 bytes. Hence short s[5] is 5 *2 which is 10, but here size is allocated based on alignment or padding for short s[5] in terms of long z which is 8bytes .
i.e multiples of 8. When we declare a union, memory allocated for union is equal to memory required for largest member of it, and all members share this same memory space. Hence for short s[5] with padding the size allocated 16 bytes, thus totaling to 16 + 8 =24.

//structasmember.c

#include<stdio.h>
int main()
{
     struct node
    {
        int x;
        struct node next;
    };

   struct node temp;
       temp.x = 10;
       temp.next = temp;
       printf("%d", temp.next.x);

  return 0;
}
result:structasmember.c|48|error: field has incomplete type 'struct node'|
Compiliation error

A structure cannot contain a member of its own type because if this is allowed then it becomes impossible for compiler to know size of such struct. Although a pointer of same type can be a member because pointers of all types are of same size and compiler can calculate size of struct




No comments:

Post a Comment