Where are class methods in memory?

This is a discussion on Where are class methods in memory? within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm new to C++ object programming, I've been working for one year with C. I googled and searched through ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    Where are class methods in memory?

    Hello, I'm new to C++ object programming, I've been working for one year with C.

    I googled and searched through this forum, but found nothing.

    I'm now trying to use some classes in my C application, so I'm having an "adaptation" work: I'm trying to compile the c++ part in an obj, and then using the classes through C functions and structures.

    I need to know where are placed in memory the methods of Cpp structures. To be clear:

    Code:
    struct A_cpp_base
    {
        int buf[100];
    
        void A_cpp_base( void ){ memset( this , 0 , sizeof( *this ) ) };
        void method( void ){......};
        ........
    };
    
    struct A_c_base
    {
        int buf[100];
    };
    
    struct A_cpp : public A_cpp_base
    {
        int   a;
        int   b;
        int   d[100];
    
        void A_cpp( void ){ memset( this , 0 , sizeof( *this ) ) };
        void method( void ){......};
        ........
    } A_cpp_object;
    
    struct A_c{
        A_c_base publicFromACppBaseAdapted;
        int   a;
        int   b;
        int   d[100];
    }A_c_object;
    How can I copy the members of a A_cpp_object in a A_c_object as fast as possible?

    I was thinking about something as:
    Code:
        memcpy(  &A_c_object , &A_cpp_object , sizeof( A_c_object ) )
    But I need to know where are placed the public components of the base class of the object that I need to copy.

    Thank you for help,
    BrownB
    Last edited by BrownB; 02-02-2005 at 04:28 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > I need to know where are placed in memory the methods of Cpp structures.
    They're not stored in the structure at all, you only get one copy of the code no matter how many instances of the data you have (a good thing).

    > memcpy( &A_c_object , &A_cpp_object , sizeof( A_c_object ) )
    I think you're on thin ice here, though you're probably OK for this example. Push hard enough on the C++ side (say inheritance in your class) and the association will break.

    Better to go with something like a specific class member function which does the appropriate memberwise copying to your C structure.
    Code:
    struct A_cpp{
        int   a;
        int   b;
        int   d[100];
    
        void A_cpp( void ){ memset( this , 0 , sizeof( *this ) ) };
        void method( void ){......};
        void copy_to_c ( struct A_c *c_copy );
        ........
    } A_cpp_object;
    
    void A_cpp_object::copy_to_c ( struct A_c *c_copy ) {
      c_copy->a = a;
      // etc etc
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    Quote Originally Posted by Salem
    > I need to know where are placed in memory the methods of Cpp structures.
    They're not stored in the structure at all, you only get one copy of the code no matter how many instances of the data you have (a good thing).
    I wrote in the wrong way, I was meaning "members", not "methods", my fault.

    A list of questions, I hope "smart" questions...
    What kind of assumptions can I do on a structure members location in a instance of that structure? I know that for "normal" structures is the same as in C: they follow the declariation sequence.

    But what about DERIVED structures? Where will the base class members be placed? Is this a compiler issue?

    Thank you for you help!
    BrownB

    PS: can somebody give me a site about "C++ adaptation to C"? I heard about "wrappers" but I'm not shure it's what I need..

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What kind of assumptions can I do on a structure members location in a instance of that structure?
    Very few. Like C structures, C++ classes also introduce padding between and after members for alignment purposes. Working with the individual bytes of an object (such as with memset) is flirting with danger. Remember that accessing uninitialized memory (like...padding, for instance ) is undefined.

    >Is this a compiler issue?
    That would be a good assumption.

    >can somebody give me a site about "C++ adaptation to C"?
    Can you be more specific?
    My best code is written with the delete key.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > PS: can somebody give me a site about "C++ adaptation to C"?
    http://www.parashift.com/c++-faq-lit...c-and-cpp.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In fact, C++ only guarantees source-order -> memory-order for members of the same visibility.
    That is, for this:
    Code:
    class A
    {
      int a;
    public:
      int b;
    private:
      int c;
    public:
      int d;
    };
    the order might be abcd, but it also might be acbd, or any other variation where a comes before c and b before d.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    Quote Originally Posted by Salem
    > PS: can somebody give me a site about "C++ adaptation to C"?
    http://www.parashift.com/c++-faq-lit...c-and-cpp.html
    Thank you, I read it, and I now understand your hints. The only robust way is to do a member-to-member copy.

    Regarding a class conversion, I'm doing this:

    let's have a class A and a derived class Aderived:
    Code:
    calss A
    {
        public:
           int buf[100];
           long count;
    
           void A( void ){};
           int Amethod1( params1 );
           long Amethod2( params2 );
    }
    
    calss Aderived : public A
    {
        public:
           char string[10];
           long length;
    }
    In C code I'll use this, declared in the "interface" header:
    Code:
    struct A_c
    {
           int buf[100];
           long count;
    }
    
    struct Aderived_c
    {
            A_c  inherited;
           char string[10];
           long length;
    }
    while for the methods, I'll write them in an extended version, adding also a parameter as pointer to the structure which will be affected. Something like this:
    Code:
           void A_create( A **pClass );    //void A( void ){};
           int A_Amethod1( A *pClass , params1 );        // Amethod1();
           long A_Amethod2( A *pClass , params2 );        // Amethod2();
    Will there be something wrong?

    Thank you very much, it's always a pleasure!
    BrownB

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In C++ its call inhertience not conversion. When you public inhertient another class that class' public part is included in your public part, their protected part is included in your private part, and their private part means private.

    What you showed with your C example is private inhertience / composition (technically composition). In this type of inhertience the class becomes a member of your class. You will only have access to the public part of that class. If you wish to allow outside access to that class you must either put it in your public part or put accessor functions in your public part.

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    If you want code that can be called from C and also called from C++ in a C++ friendly class, your best bet would be to write your code in C and create a C++ wrapper class that calls into the C code. Just include the appropriate C struct as a member of your C++ class and delegate the class methods to C functions.
    Code:
    #if defined(__cplusplus)
    
    class A
    {
      private:
        struct A_c;
    
      public:
           A( )
           {
               A_create(&A_c); /* Delegate to C create function. */
           }
    
           ~A( )
           {
               A_destory(&A_c);
           }
    
           int Amethod1( int params1 )
           {
               return A_Amethod1(&A_c, params1);
           }
    
           long Amethod2( int params2 )
           {
               return A_Amethod2(&A_c, params2);
           }
    };
    
    #endif /* defined (__cplusplus)
    The other alternative is to write your code in C++ and create a wrapper so it can be accessed from C. The disadvantage to this approach is that your code will need a C++ compiler to be compiled. With the C code/C++ wrapper solution, you can use defines so that the C++ wrapper is not compiled on a C compiler. The advantage is that you can leverage the benefits of C++ in your code.

    Here is an example of a wrapper that enables a C++ class to be called from C code.
    Code:
    /* Create a handle type for your class. */
    typedef void* CLS_HANDLE;
    
    extern "C" void A_create(CLS_HANDLE* ppHandle)
    {
       A* temp = new A;
       *pHandle = (CLS_HANDLE) temp;
    }
    
    extern "C" int A_Amethod1(CLS_HANDLE pHandle, params1 )
    {
       A* temp = (A*) pHandle;
       return temp->Amethod1(params1);
    }
    
    extern "C" int A_Amethod2(CLS_HANDLE pHandle, params2 )
    {
       A* temp = (A*) pHandle;
       return temp->Amethod2(params2);
    }
    
    extern "C" void A_destroy(CLS_HANDLE pHandle)
    {
       A* temp = (A*) pHandle;
       delete temp;
    }
    Last edited by anonytmouse; 02-02-2005 at 09:30 AM.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    anonytmouse might I suggest the #ifdef preprocessor command. It seems to be the more preferred preproccessor command for seeing if a symbol has been defined yet. It also has the avantage of not requireing ()

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I used to use ifdef but changed to if defined because it is more consistent and as far as I can tell more common in new code. More discussion on this issue can be found at the following pages.

    MSDN: The #ifdef and #ifndef Directives

    Usenet Thread: #ifdef depreciated?

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I use #ifndef for inclusion guards and #if defined for everything else.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Allocation In A String Class
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 09-18-2007, 10:34 AM
  2. Class methods in header or source file?
    By TriKri in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2007, 05:23 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. exporting class methods to library results in .lib and .exp
    By Shadow12345 in forum C++ Programming
    Replies: 15
    Last Post: 01-05-2003, 07:01 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21