Thread: Class object memory usage?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    245

    Class object memory usage?

    Say a class has 30 bytes of data storage (char of 30 bytes, say) and 5 methods for manipulating the data. How much memory would be used if 1000 of these objects were created ? Would it be (1000 * 30) + method code size, or 1000 * (30 + method code size) ?

    (That is, is the code in the method duplicated for each created instance of it, or just once and pointers used for the rest ?)

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The program can use only one set of code per class because, in addition to the normal arguments you would call for any methods in the structure, a pointer to the calling structure is also passed (the 'this' pointer).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by Salem
    > Would it be (1000 * 30) + method code size
    This one.
    in addition. you would have + 4 bytes if one or more of those functions are virtual.

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Perspective
    in addition. you would have + 4 bytes if one or more of those functions are virtual.
    Blah blah blah, plus any padding done by the compiler.
    There's many factors...

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Thanks, makes the coding a little easier. I can just use an iterator on the vector of my class now then and just tell each instance of the class to save itself to an opened file rather than worrying about how to save each type of class. Suppose it makes it more generic now too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Is there a way to tell what class an object is?
    By Loduwijk in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2006, 09:20 PM
  3. Window Class Memory
    By CornedBee in forum Windows Programming
    Replies: 1
    Last Post: 05-07-2005, 11:08 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM