0

talking about C++. Let's say I have an array of pointers to objects.In position Array[6] for example,I want to replace the object pointed by, with another of the same type. Which is the wisest way?

1.

delete Array[6];
Array[6]=new Object(...);

2.

Array[6]=NULL;
Array[6]=new Object(...);

3.

Array[6]=new Object(...);

Or something else I'm missing.Waiting for advice,thank you!!

EDIT: question:
Do you want to delete the object pointed to by Array[6]? If so, do delete Array[6];. If not, don't delete Array[6];. I'm not sure what there is to be confused about there.

answer: Yes,it's what you've mentioned that I want. I want the object pointed by Array[6] to "disappear" and make a new one there. My other problem: I want to delete some objects of the Array in a function, but in another I want to access them so I have to check. Which will be my check? Maybe: if (Array[i]==NULL){..} or is it wrong since I have not "NULLed" it?

JAM AICA
  • 91
  • 1
  • 10
  • delete it, other wise memory leak – BAE Feb 03 '16 at 22:25
  • Possible duplicate of [Is it good practice to NULL a pointer after deleting it?](http://stackoverflow.com/questions/1931126/is-it-good-practice-to-null-a-pointer-after-deleting-it) – Samer Feb 03 '16 at 22:25
  • There is a fundamental difference between `delete` and assigning `NULL` to a pointer variable. I suggest you educate about that difference. – datenwolf Feb 03 '16 at 22:26
  • @datenwolf isnt that the point of the question – pm100 Feb 03 '16 at 22:27
  • After deleting,i guess I have destroyed the Object pointed to by Array[6],so now Array[6] can't be accessed,right? What should I check then,so as not to access a deleted position on my Array? – JAM AICA Feb 03 '16 at 22:41
  • Do you want to delete the object pointed to by `Array[6]`? If so, do `delete Array[6];`. If not, don't `delete Array[6];`. I'm not sure what there is to be confused about there. – user253751 Feb 03 '16 at 22:44
  • Yes,it's what you've mentioned that I want. I want the object pointed by Array[6] to "disappear" and make a new one there. – JAM AICA Feb 03 '16 at 22:47
  • Do you really need to destroy the object and make a new one? Can't you just change the value of the object? Does the object not support `operator=`? Why not `*(Array[6]) = ...;` or `*(Array[6]) = Object(...);`? – David Schwartz Feb 03 '16 at 22:59
  • 1
    @pm100: I think OP may still have problems grasping that a pointer is just a pointer and that by `delete Array[6]` it's not `Array[6]` that gets destroyed, but the object `Array[6]` is pointing to. `Array[6]` of course still can be accessed, but the pointer it holds is then invalid. – datenwolf Feb 03 '16 at 23:26
  • I know that the object gets destroyed and not the Array[6].But I search for a way afterwards to check if Array[6] is pointing to an existing object or a deleted one.(NULL) How I will achieve that? – JAM AICA Feb 03 '16 at 23:59
  • Can anyone help me on that? How will I make sure with a statement(?) that I will not try to access a deleted memory and end with an undefined behaviour? – JAM AICA Feb 04 '16 at 01:09

2 Answers2

3

2 and 3 are likely to cause memory leaks. As such I would suggest 1, or use array of smart pointers and reset them with new object.

101010
  • 41,839
  • 11
  • 94
  • 168
  • 3
    we dont know they have leaks, it depends if the array is the only place where that old pointer was stored. Probably but not 100% – pm100 Feb 03 '16 at 22:27
  • @pm100 based on the title the OP wants to reset the pointers and she/he wonders which one of the 3 methods renders best. As such my argument holds. – 101010 Feb 03 '16 at 22:36
1

Number 1 is very different. The other 2 are basically the same (but with more or less typing)

Do #1 if the array is the owner of the object, ie you need to free the memory. Otherwise do #2 or 3

And anyway use std::vector and std::shared_ptr

If feels like you come from languages that do magic with references (like c# or Java) where assugining null to a pointer actually does someting. Not so in c++ (unless you use smart pointers and containers as I said before)

pm100
  • 48,078
  • 23
  • 82
  • 145