0

I have to write an application to complete an assignment, at school. To do so, we have to create a dynamic array. My question is:

  1. If I create a structure containing a dynamic pointer, and copy it, does the memory get copied too?

    For instance:

    struct SomePointerStruct
    {
         int* p_array;
    }
    

    What happens if I copy that structure? Also,

  2. Do I need to clean up the original pointer?

10
  • 3
    How are you copying it? Commented Oct 31, 2013 at 14:42
  • 2
    1. No, you need to follow the rule of three. 2. Most likely, yes. Commented Oct 31, 2013 at 14:43
  • 4
    Your language is sloppy, and this will come to bite you eventually when you start thinking about C++ a bit more. It's not the pointer that's dynamic, but the object to which the pointer points. Similarly, you don't "clean up pointers"; rather, you clean up objects (by means of pointers to the objects). Commented Oct 31, 2013 at 14:46
  • @benjymous obj b = a; Commented Oct 31, 2013 at 14:47
  • If you want automatic clean-up and well defined copy semantics, assuming you actually want to copy the value of the pointer to a dynamic variable, and if you can use C++11, use std::shared_pointer<int> instead. If you need a dynamic array, why not use a std::vector<int>? Copying the vector will automatically copy the data it holds as well. You can also just as well use a shared_ptr with an actual array, but need to provide a custom deleter that actually calls delete[]. Commented Oct 31, 2013 at 14:48

3 Answers 3

3

By default, objects in c++ are copied byte-by-byte. that means that the pointer will be copied, and you will get two pointers pointing to the same array.

To fix this, you need to implement a copy constructor and should also override the operator=. Also, you may want to delete this array when you finish, so implement also a destructor. That is "the rule of three".

Sign up to request clarification or add additional context in comments.

Comments

1

If I create a structure containing a dynamic pointer, and copy it, does the memory get copied too?

No.

Much of the design of the C++ language is dictated by the edict, "You don't pay for what you don't need." One way this plays out is if you want to deep-copy something, you have to do it yourself.

Do I need to clean up the original pointer?

In general, yes.

For every new, there must be exactly one matching delete. If you instantiate a SomePointerStruct, and the construction of that calls new, that's one delete you need somewhere -- probably in SomePointerStruct's destructor. If you create a copy of SomePointerStruct and the copy deep-copies the original pointer (calling new), then that's a second delete you need -- again, probably in the destructor.

Comments

1

To avoid repeating the whole chapter named Memory from your C++ book, I'll answer with that:

Raw pointers shouldn't own memory.

By consequence:

struct SomePointerStruct { int x; }

is trivially copyable, and:

struct SomePointerStruct { std::unique_ptr<int> x; } 

is noncopyable (but it's moveable). That extends to all of your other types (if they are properly constructed), sans the "trivially", perhaps. The fact that the pointer value would be copied in your original example would of course lead to a lot of problems, but since they can be easily avoided by using what I wrote above, it's kind of off the scope WRT such a simple assignment.

A whole another thing would be a non-owning pointer case, in which a copied struct would, of course, point to the same region of memory. You lose all the guarantees of the pointer pointing to a valid location, though, so a reference would be preferred.


As for creating of a dynamic array, you'd be best off with simply using std::vector.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.