470,849 Members | 1,240 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

polymorphic class have diferent size on different os

class B
{
public :
void f() = 0 ;
};

class D : public B
{
public :
void f()
{

}
};

int main()
{
D d ;
int s = sizeof(d);
return 0 ;
}

now value of s will be different on windows and unix , why so ?

Jun 29 '06 #1
5 968
mangesh wrote:
class B
{
public :
void f() = 0 ;
This isn't valid, virtual keyword is missing. };

class D : public B
{
public :
void f()
{

}
};

int main()
{
D d ;
int s = sizeof(d);
return 0 ;
}

now value of s will be different on windows and unix , why so ?

Who says they should be the same?

--
Ian Collins.
Jun 29 '06 #2

mangesh wrote:
class B
{
public :
void f() = 0 ;
virtual void f() = 0;
};

class D : public B
{
public :
void f()
{

}
};

int main()
{
D d ;
int s = sizeof(d);
return 0 ;
}

now value of s will be different on windows and unix , why so ?


Unix is a 64 bit OS and your windows is probably 32 bit.
Regardless, a real programmer must not write code that depends on the
platform beneath.
Hence: what you see is expected and inconsequential.

Jun 29 '06 #3
mangesh wrote:

now value of s will be different on windows and unix , why so ?


There's not even any guarantee that s will be the same value on
different implementations within UNIX or Windows.

Jun 29 '06 #4
mangesh posted:

now value of s will be different on windows and unix , why so ?

Three reasons:
(1) The amount of bytes consumed by primitive types is implementation-
specific.
(2) The amount and placement of padding within an object is
implementation-specific.
(3) The way in which virtual functions are implemented is
implementation-specific.

--

Frederick Gotham
Jun 29 '06 #5
Hi ,
thanks for detailed reply .

Regards ,
Mangesh Sawant .

Frederick Gotham wrote:
mangesh posted:

now value of s will be different on windows and unix , why so ?

Three reasons:
(1) The amount of bytes consumed by primitive types is implementation-
specific.
(2) The amount and placement of padding within an object is
implementation-specific.
(3) The way in which virtual functions are implemented is
implementation-specific.

--

Frederick Gotham


Jun 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by verec | last post: by
7 posts views Thread by Mr. Ed | last post: by
7 posts views Thread by James Fortune | last post: by
12 posts views Thread by Bob | last post: by
5 posts views Thread by FefeOxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.