9

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
SimpleButPerfect
  • 1,529
  • 2
  • 11
  • 20
  • Possible duplicate of [Class members and explicit stack/heap allocation](https://stackoverflow.com/questions/10836591/class-members-and-explicit-stack-heap-allocation) – underscore_d Nov 12 '17 at 14:50

3 Answers3

7

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
  • 54,973
  • 13
  • 116
  • 224
  • Thanks for the tip, I'll consider using this technique more often. – SimpleButPerfect May 12 '10 at 16:01
  • 3
    Though technically Chris is correct, It is not a good idea to do this. Use a std::vector 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 May 12 '10 at 19:12
  • 1
    The question wasn't about arrays at all, i should've changed this example, it was about the concept of allocation itself. – SimpleButPerfect May 13 '10 at 05:28
3

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
  • 47,994
  • 7
  • 61
  • 70
1

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
  • 1,634
  • 1
  • 10
  • 3
  • 1
    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 May 12 '10 at 16:19
  • 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 May 12 '10 at 16:58