CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2004
    Posts
    216

    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!

  2. #2
    Join Date
    Mar 2004
    Location
    India
    Posts
    276
    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.

  3. #3
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238
    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...
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  4. #4
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    Quote 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!"

  5. #5
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238
    Quote 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.
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  6. #6
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    Quote 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!"

  7. #7
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238
    I think it's high time that I got an AMD machine and started playing around with it .
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  8. #8
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Quote 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).

    Quote 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.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449
    Quote 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

  10. #10
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238
    Quote 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?
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  11. #11
    Join Date
    Mar 2004
    Posts
    216
    thanks for the answer!!!

  12. #12
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured