Memory for Functions

This is a discussion on Memory for Functions within the C++ Programming forums, part of the General Programming Boards category; I'm a little confused exactly how the compiler handles functions in a class. I know that when each object of ...

  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
    ggs
    ggs is offline
    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,662
    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,705
    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 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, 01:32 AM
  2. Replies: 4
    Last Post: 01-13-2008, 01:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 01:15 AM

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