Thread: Memory for Functions

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    Memory for Functions

    I'm a little confused exactly how the compiler handles functions in a class. I know that when each object of a class is created, memory is set aside for each data type (unless it's static), but that the functions for a class are only created once in memory. So, are functions DEFINED in a class and data types only DECLARED in a class? Thanks

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    imagine that class x { int foo(...); }; turns into this int x_manglebleh_foo(class x, ...);. it's close enough to the truth
    .sect signature

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Name mangling aside, you can think of non-static member functions as normal C functions that use the thiscall calling convention.
    The details in the above link are MS specific, but the general idea is that the "this" pointer gets passed to the function "under the hood".

    If you want to learn how virtual functions work "under the hood", read this thread.

    gg

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by jrahhali
    I'm a little confused exactly how the compiler handles functions in a class. I know that when each object of a class is created, memory is set aside for each data type (unless it's static), but that the functions for a class are only created once in memory. So, are functions DEFINED in a class and data types only DECLARED in a class? Thanks

    Each instance of a class needs it's own data, but can obviously share the same code (functions). It's just doesn't make sense to make copies of a function since, unlike data, it never changes.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. available memory from task manager
    By George2 in forum Tech Board
    Replies: 10
    Last Post: 01-18-2008, 02:32 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM