tags:

views:

57

answers:

5

Hi For example we have code

class MyClass
{
   private:
   int data;
   public:
   int getData()
   {
      return data;
   }
};

int main()
{
   MyClass A, B, C;
   return 0;
}

Since A, B and C are objects of MyClass, all have their own memory. My question is that, are all of these objects share same memory for methods of class ( getData() in this case) or all objects have separate code segment for each object.?

Tnahks in advance....

+2  A: 

They usually share the same code segment.

Nikola Smiljanić
Is there any book or any other resource, from where I can get more detail ????
Asad Hanif
http://stackoverflow.com/questions/2228424/objects-of-a-class-share-same-code-segment-for-methods/2228452#2228452
Alexander Poluektov
Inside C++ Object Model might hold some answers. I haven't read it myself.
Nikola Smiljanić
@Nikola I have - it doesn't.
anon
A: 

Same.

You could be interested in knowledge of how things in C++ are implemented under the hood.

Alexander Poluektov
+2  A: 

The C++ Standard has nothing to say on the subject. If your architecture supports multiple code segments, then whether multiple segments are used is down to the implementation of the compiler and linker you are using. It's highly unlikely that any implementation would create separate segments for each class or object, however. Or indeed produce separate code for each object - methods belong to classes, not individual objects.

anon
I like these abstract (and perfectly correct) answers. C++ standard says nothing about these things, but is there a platform (that you know of) that supports multiple segments?
Nikola Smiljanić
@Nikola Yes - 16-bit windows. And many embedded platforms have no notion of a code segment at all.
anon
I know that C++ says nothing about stack and heap and I can imagine a system without them. But I had no idea that there is a system that actually implements multiple code segments. I'm too young, or maybe you're too old :)
Nikola Smiljanić
I think you and Neil are using the term *segment* to mean different things.
Clifford
@Clifford Care to expand on that?
anon
He appears to be talking about instances of classes occupying separate code space per instance (which they don't unless the code is in-lined). That is how the term was used in the original post. While it appears that you are referring to either segmented memory architectures or perhaps linker segments. I may be wrong, but it seems ambiguous.
Clifford
A: 

In general with classes and objects the following is how it works:

A class is a description of data and operations on that data.

An object is a specification of the type of object (the class it represents) and the values of the attributes. Because the type of the object is saved the compiler will know where to find the methods that are being called on the object. So even though a new copy of the data is created when a new object is made, the methods still are fixed.

Thirler
A: 

In your example, MyClass::getData() is 'inline', so in that case the location of the instructions for each instance may well be in different locations under some circumstances.

Most compilers only truly in-line code when optimisation is enabled (and even then may choose not to do so). However, if this in-line code were defined in a header file, the compiler would necessarily generate code for each compilation unit in which the class were used, even if it were not in-lined within the compilation unit. The linker may or may not then optimise out the multiple instances of the code.

For code not defined inline all instances will generally share the same code unless the optimiser decides to inline the code; which unless the linker is very smart, will only happen when the class is instantiated and used in the same compilation unit in which it was defined.

Clifford