-4

I have following C++ code. Since, the memory is dynamically allocated, it should be allocated on heap. Or, since the memory has been allocated right at time of declaration and not in any constructor, is it being allocated on stack? And do I need a de-constructor to free the memory?

class param{
public:
    char* st = new char[256];
};

What happens in the following scenario? I guess this is allocated on stack, and need not to be freed using a deconstructor.

class param{
public:
    char st[256];
};

Third way is to write it as:

class param{
public:
    char* st;
    param()
    {
        st = new char[256];
    }

    ~param()
    {
       delete[] st;
    }
};

Which one of the three above is the correct way to do it?

Bit Manipulator
  • 248
  • 1
  • 2
  • 17
  • Running the first version against valgrind indicates a memory leak, so it is allocated on the heap. You would have to do the delete somewhere manually later and thats pretty weak. Either the 2nd or 3rd way is a valid way to do it. For the 3rd way ensure you provide suitable (or deleted) copy and move constructors and assignment operators to ensure the memory ownership is handled correctly. – Paul Rooney Feb 17 '16 at 01:56
  • 3
    Really none of these make any sense. Use `std::vector` or `std::array`. – David Schwartz Feb 17 '16 at 01:57

2 Answers2

2

since the memory has been allocated right at time of declaration and not in any constructor, is it being allocated on stack?

No, it's still dynamically allocated.

And do I need a de-constructor to free the memory?

You're allocating memory by operator new[], so you need to call operator delete[] to deallocate it.

Other suggestions:

  1. Think about using std::vector or std::array, since it's C++.

  2. For the 3rd case, please refer to What is The Rule of Three? and Rule-of-Three becomes Rule-of-Five with C++11?

Community
  • 1
  • 1
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
1

new always allocated on the heap.

In the first example, the pointer from new is placed into a class member.

An instance of this class can be instantiated on the stack; or the instance of the class can be instantiated on the heap itself, via new. Whichever way an instance of the class gets instantiated itself, whether on the heap or on the stack, its member st will always point to heap-allocated memory.

In the second example, the member st is a part of the class. Nothing is instantiated in the example given. It's only a class declaration. When an instance of the class gets instantiated, it may be instantiated on the heap or on the stack, in either case the st member is a part of the class, and gets instantiated together with that particular class instance.

As far as which is the "correct" way, all three examples could be correct. Or all three of them could be wrong. It all depends on how the class is used. Generally, the 2nd or the 3rd example is the most convenient way to implement, and results in fewer surprises.

But, it could be possible to come up with some esoteric situation where even the first example is correct.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148