Fog Creek Software
Discussion Board




Replicating code on object allocation?

When you create or allocate a new object is only the data allocated or is a new set of code also allocated?  I wouldn't think that it would be, I would think that there is a one to many relationship between the data and the code.  One set of code in the code segment serves many different allocations of data on the heap.  Not sure what advantage replicating code would be upon object creation, maybe in a server environment it would make sense for multiple simultaneous processes.  Thought I'd throw this out to see if anyone can explain it.

Red Oak
Wednesday, January 14, 2004

Only the data is allocated.  If you have a structure that's 8 bytes long and you make it an object, it'll still be 8 bytes long. 

Could you imagine the overhead it would take to duplicate potentially megabytes of code that manipulate small amounts of data?  It's not uncommon to have zillions of little objects running around in an app.

When you call an object method (for example, oFoo.Bar( )), Bar recieves as an implicit "hidden" parameter the location of the oFoo object (usually called the "this" pointer).

Alyosha`
Wednesday, January 14, 2004

Unless you have any virtual functions, in which case there is an extra 4 bytes for each instance of the object.

The 4 bytes is a pointer to a list of pointers to the virtual functions.

Alex.ro
Wednesday, January 14, 2004

and then you get into tear-offs

mb
Wednesday, January 14, 2004

Um, in any particular language? I realize they are likely similar, but you can't really answer the question without knowing....

sgf
Wednesday, January 14, 2004

*  Recent Topics

*  Fog Creek Home