Memory size of a method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mojumbo

    #1

    Memory size of a method

    I am passing around a structure, say:

    tsBob
    {
    int a;
    int b;
    int bobMethod(bool lb);

    };

    I would like to make sure the structure is aligned along a 64-bit
    boundary (it's being thrown around the system). I assumed it was the
    size of a pointer (32-bits) but when I googled & wrote a little driver
    (See below) it was only 1 byte. Since I can't believe this does
    anyone have an answer?

    int main(int argc, char* argv[])
    {
    tsBob aBob;

    cout << "Size of bobMethod is " << sizeof(aBob.bob Method) << endl;

    return 0;

    }
  • Matthias Buelow

    #2
    Re: Memory size of a method

    mojumbo wrote:
    I am passing around a structure, say:
    >
    tsBob
    {
    int a;
    int b;
    int bobMethod(bool lb);
    >
    };
    >
    I would like to make sure the structure is aligned along a 64-bit
    boundary (it's being thrown around the system).
    Function members do not contribute to a structure/class's size.
    If you call aBob->bobMethod(true ), the compiler essentially
    underhandedly rewrites the expression into something similar to
    bobMethod(aBob, true) in an unambiguous way.
    I assumed it was the
    size of a pointer (32-bits)
    A function is not a function pointer.

    Comment

    • mojumbo

      #3
      Re: Memory size of a method

      Thanks

      Comment

      • Richard Herring

        #4
        Re: Memory size of a method

        In message
        <b32bd960-ba0a-4e73-8413-babc44091c46@t5 4g2000hsg.googl egroups.com>,
        mojumbo <jnbbender@gmai l.comwrites
        >I am passing around a structure, say:
        >
        >tsBob
        >{
        int a;
        int b;
        int bobMethod(bool lb);
        >
        >};
        >
        >I would like to make sure the structure is aligned along a 64-bit
        >boundary (it's being thrown around the system). I assumed it was the
        >size of a pointer (32-bits)
        Why do you assume a member function needs to occupy any memory at all
        within individual instances of the class?

        --
        Richard Herring

        Comment

        • James Kanze

          #5
          Re: Memory size of a method

          On Sep 19, 2:28 pm, mojumbo <jnbben...@gmai l.comwrote:
          I am passing around a structure, say:
          tsBob
          {
          int a;
          int b;
          int bobMethod(bool lb);
          };
          I would like to make sure the structure is aligned along a 64-bit
          boundary (it's being thrown around the system).
          Generally speaking, it's the compiler's job to ensure proper
          alignment, not yours.
          I assumed it was the size of a pointer (32-bits) but when I
          googled & wrote a little driver (See below) it was only 1
          byte. Since I can't believe this does anyone have an answer?
          int main(int argc, char* argv[])
          {
          tsBob aBob;
          cout << "Size of bobMethod is " << sizeof(aBob.bob Method) << endl;
          return 0;
          }
          This shouldn't compile; in C++, functions don't have any size
          (or at least, you can't use sizeof on them). And of course,
          something like 'aBob.bobMethod ' is only a legal expression if it
          is an operand to a function call operator, i.e.
          aBob.bobMethod( ).

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          Working...