-
August 9th, 2004, 06:45 AM
#1
sizeof(class) question
Hi, i've got this class
template<class T> class pointer
{
private:
T *p;
};
and i get 4 if i do sizeof(pointer).
if i add a static member data, it doesn;t count. why is it that? is it because there's only one copy of that atribute for all the instances of the class?
then, if i add a virtual function (or 2, or 3), i get 8 if i do sizeof(pointer). why is that?
thanks for your help!
-
August 9th, 2004, 06:59 AM
#2
As far as i know,the size of class is 4 becoz it gives the size of pointer member variable.But when u add a virtual member fucntion,its sets up the vtable ptr and hence its showing 8(4 for pointer variable +4 for this vtable pointer)..But if u add more than 1 virtual member function,still the size would be 8bytes only...
Poorni
Life is like riding a bicycle To keep ur Balance u have to keep riding.
-
August 9th, 2004, 07:03 AM
#3
kfaday, from my experience from programming in C and C++, ALL pointers are made up of 4 bytes, that includes pointers to functions, classes, integers, strings, integer arrays, you name it . Or atleast that's what the sizeof function returns whenever you try to input one of those pointers into it.
Last edited by YourSurrogateGod; August 9th, 2004 at 07:07 AM.
Reason: forgot to add something...
-
August 9th, 2004, 07:27 AM
#4
Originally Posted by YourSurrogateGod
kfaday, from my experience from programming in C and C++, ALL pointers are made up of 4 bytes
In fact, a number of bytes where any pointer is placed depends on machine architecture...
on some architectures the size of a pointer to function != the size of a pointer to any data type
"UNIX is simple; it just takes a genius to understand its simplicity!"
-
August 9th, 2004, 08:26 AM
#5
Originally Posted by dimm_coder
In fact, a number of bytes where any pointer is placed depends on machine architecture...
on some architectures the size of a pointer to function != the size of a pointer to any data type
Good point, I have an intel. I've never tried it in on an AMD machine or anything else.
-
August 9th, 2004, 08:52 AM
#6
Originally Posted by YourSurrogateGod
Good point, I have an intel. I've never tried it in on an AMD machine or anything else.
32 bit AMD processors have the Intel-like architecture in what concerns interaction with the OS layer - Intel x86 architecture..
and for these ones U are right when talking about 4 bytes, for 64 bit processors (recent intels, AMD64) - 8 bytes.
When talking about different processor architectures I've meant things like alfa, mips, ppc, sparc..., old PDP machines, etc.
Last edited by dimm_coder; August 9th, 2004 at 09:15 AM.
"UNIX is simple; it just takes a genius to understand its simplicity!"
-
August 9th, 2004, 09:09 AM
#7
I think it's high time that I got an AMD machine and started playing around with it .
-
August 9th, 2004, 09:39 AM
#8
Originally Posted by kfaday
if i add a static member data, it doesn;t count. why is it that?
That's because static data members are not part of the classes memory layout (since they are shared between all instances).
Originally Posted by kfaday
then, if i add a virtual function (or 2, or 3), i get 8 if i do sizeof(pointer). why is that?
As poorni already said: The additional 4 bytes are for the vtable pointer, which is added as soon as you add the first virtual function.
-
August 9th, 2004, 10:22 AM
#9
Originally Posted by YourSurrogateGod
Good point, I have an intel. I've never tried it in on an AMD machine or anything else.
You don't need an AMD machine. All you need is MSDOS or Windows 3.1.
Pointers in 16-bit DOS/Win3.1 were either 2 or 4 bytes (near and far pointers, respectively).
Regards,
Paul McKenzie
-
August 9th, 2004, 10:33 AM
#10
Originally Posted by Paul McKenzie
You don't need an AMD machine. All you need is MSDOS or Windows 3.1.
Pointers in 16-bit DOS/Win3.1 were either 2 or 4 bytes (near and far pointers, respectively).
Regards,
Paul McKenzie
So I take that in AMD 64 it is 4 or 8 bytes? If so, which types of pointers are 8 bytes large and which ones are 4?
-
August 9th, 2004, 06:13 PM
#11
-
August 10th, 2004, 01:42 AM
#12
Originally Posted by YourSurrogateGod
So I take that in AMD 64 it is 4 or 8 bytes? If so, which types of pointers are 8 bytes large and which ones are 4?
Well, 32 bit intel x86 (32 bit amd included here too) machines have a flat virtual address space for each process which size = 2^32. Thus, to address any memory cell (byte) in such flat address space the size of a pointer must be == 4 byte.
For i64 (amd64, intel64) the size of the virtual address space is 2^64 (well, most 64 bit-compatible OSes use only the part of this space = 2^43 for virtual address space of each process). Thus, for addressing any cell the size of a pointer must be == 8 bytes.
"UNIX is simple; it just takes a genius to understand its simplicity!"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|