views:

285

answers:

3

If a class is declared as follows:

class MyClass
{
  char * MyMember;
  MyClass()
  {
    MyMember = new char[250];
  }
  ~MyClass()
  {
    delete[] MyMember;
  }
};

And it could be done like this:

class MyClass
{
  char MyMember[250];
};

How does a class gets allocated on heap, like if i do MyClass * Mine = new MyClass(); Does the allocated memory also allocates the 250 bytes in the second example along with the class instantiation? And will the member be valid for the whole lifetime of MyClass object? As for the first example, is it practical to allocate class members on heap?

+2  A: 

When you call new it allocates from the heap, otherwise it allocates from the stack (we'll ignore malloc and its ilk).

In your first example, there will be space allocated in both: 4 bytes on the stack for the instance of MyClass (assuming 32-bit pointers), and 250 bytes on the heap for the buffer assigned to MyMember.

In the second example, there will be 250 bytes allocated on the stack for the instance of MyClass. In this case, MyMember is treated as an offset into the instance.

Anon
This isn't really correct, in the second example, the class contains an array of 250 chars. all will be on the heap if you do something like `MyClass *foo = new MyClass;`
Hasturkun
Ah - I didn't see the `new MyClass` (or perhaps the OP added it in the "edits don't get noted" timeframe). So this answer isn't at all correct as the question stands. But unfortunately, I can't delete it.
Anon
+2  A: 

Early note: use std::string instead of a heap allocated char[].

Does the allocated memory also allocates the 250 bytes in the second example along with the class instantiation?

It will be heaped allocated in the constructor, the same way as in a stack allocated MyClass. It depends what you mean by "along with", it won't necessarily be allocated together.

And will the member be valid for the whole lifetime of MyClass object?

Yes.

As for the first example, is it practical to allocate class members on heap?

Yes, in certain cases. Sometimes you want to minimize the includes from the header file, and sometimes you'll be using a factory function to create the member. Usually though, I just go with a simple non-pointer member.

Stephen
Thank you for such a detailed answer, as for the early note i'm just trying to gather knowledge about the subject.
+5  A: 

Yes, yes, and yes.

Your first example has a bit of a bug in it, though: which is that because it one of its data members is a pointer with heap-allocated data, then it should also declare a copy-constructor and assignment operator, for example like ...

MyClass(const MyClass& rhs) 
{
  MyMember = new char[250]; 
  memcpy(MyMember, rhs.MyMember, 250);
}
ChrisW
Thanks for the tip, I'll consider using this technique more often.
Though technically Chris is correct, It is not a good idea to do this. Use a std::vector<char> instead and all your memory management is taken care of. To see full details: http://stackoverflow.com/questions/255612/c-dynamically-allocating-an-array-of-objects/255744#255744
Martin York
The question wasn't about arrays at all, i should've changed this example, it was about the concept of allocation itself.