Showing posts with label Size. Show all posts
Showing posts with label Size. Show all posts

Wednesday, 19 January 2011

'sizeof' a class

Continuation from last week. What is the sizeof of a class? Wrote a simple program as follows:


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

class
A
{

int
a;
};


class
B
{

int
b;
int
someFunc(void) {return b;}
};


class
C
{

void
someFunc1(void) {};
void
someFunc2(void) {};
};


class
D : public A
{

void
someFunc(void) {};
};



int
main()
{

cout<<"Size of class A = "<<sizeof(A)<<endl;
cout<<"Size of class B = "<<sizeof(B)<<endl;
cout<<"Size of class C = "<<sizeof(C)<<endl;
cout<<"Size of class D = "<<sizeof(D)<<endl;
}



The output is as follows:

After finishing this, I found this interesting article here.

Wednesday, 12 January 2011

Checking the 'sizeof' doubts

I noticed sometime back that one of my programs behaved unexpectedly in certain scenarios which was traced to an incorrect use of sizeof. As a result, I made a small program to make sure that my understanding of sizeof is correct. Here is the program:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

int
main()
{

char
a[]="";
cout<<"Size of a[] = "<<sizeof(a)<<endl;

char
b[]=" ";
cout<<"Size of b[ ] = "<<sizeof(b)<<endl;

char
c[10];
cout<<"Size of c[10] = "<<sizeof(c)<<endl;

char
* d;
cout<<"Size of d = "<<sizeof(d)<<endl;

char
* e = c;
cout<<"Size of e = "<<sizeof(e)<<endl;

char
f[] = "123456";
cout<<"Size of f = "<<sizeof(f)<<endl;

char
g[] = {'1','2','3','4','5','6'};
cout<<"Size of g = "<<sizeof(g)<<endl;

return
0;
}



The output is as follows:
A lot of interviewers love the sizeof questions.

Have you come across any interesting sizeof problems? Please feel free to add them in comments.

Wednesday, 5 May 2010

The size_t confusion

Apparently there could be a lot of confusion with regards to size_t. In 'C' the size_t was defined as an unsigned integer. 'C++' defines it as unsigned integer as well but it has a namespace of std around it. Some compilers may want #include<cstdlib> to use std::size_t.

In any case, it can cause quite some confusion among programmers as to why it is used and what is the size of it. Instead we can #define a type, give it a meaningful name and use it accordingly. Here is my simple example:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>
#include<cstdlib> //Used for std::size_t ... on some compilers

#define u16 unsigned short
#define u32 unsigned //integer is implicit

int
main()
{

size_t size = sizeof(size_t);
std::cout << "Size of size_t is " << size << std::endl;

std::size_t size2 = sizeof(std::size_t);
std::cout << "Size of std::size_t is " << size2 << std::endl;

size_t size3 = sizeof(u16);
std::cout << "Size of u16 is " << size3 << std::endl;

size_t size4 = sizeof(u32);
std::cout << "Size of u32 is " << size4 << std::endl;

return
0;
}





The output is as follows: