-7

Should I use 'delete' in the following example?

int main(){
T object;
T* pointer = new T[4];
delete[] pointer; // this line is redudant? or error?
pointer = &object;
}

I'm asking Chat GPT this same question and it says: "the line delete is redudant and may lead to undefined behaviour, because when you re-assign the pointer, the data on heap is automatically deallocated". But i have a doubt about this. So should I use 'delete' there?

LiDa Cute
  • 357
  • 2
  • 7
  • 5
    `delete[] pointer` is required to avoid memleak. – Jarod42 Aug 31 '23 at 18:19
  • 8
    Don't trust Chat-GPT with C++ (or any programming questions). The line is not redundant, you'll leak the memory without it. – chrysante Aug 31 '23 at 18:19
  • I was writing an assignment operator for a class, where I have to delete the current object data on heap before giving it a new memory. But when i added the delete line it gives error .And then jumped to chat GPT and it responsed so :> – LiDa Cute Aug 31 '23 at 18:23
  • 8
    Well, you should ask a question about the error you got (including [mcve] - minimal code that produces this error and the error itself) instead of asking ChatGPT and then asking us about what ChatGPT hallucinated. – Yksisarvinen Aug 31 '23 at 18:26
  • 3
    If you have problems with your class, please post a minimal reproducible example and the error messages you get. The code you posted does not have any errors. – chrysante Aug 31 '23 at 18:26
  • 2
    Note Chat GPT is a language model. All it knows is how to string together probable tokens. It can't tell if the result is actually correct because it doesn't understand what it just wrote. – user4581301 Aug 31 '23 at 18:29
  • 5
    my cat knows more about c++ than chatgpt. I dont have a cat but nevertheless. – 463035818_is_not_an_ai Aug 31 '23 at 18:29
  • 2
    You should not do the example at all. We hardly use any `new` or `delete` in modern C++, so just don't. But *if* you use `new` and `delete` (you shouldn't, but just assume for the sake of argument that you do), then that `delete` line is neither redundant nor an error. Also please read the [generative AI policy](https://meta.stackoverflow.com/questions/421831/temporary-policy-generative-ai-e-g-chatgpt-is-banned?cb=1). – n. m. could be an AI Aug 31 '23 at 18:35
  • Handy reading: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/q/6500313/4581301) – user4581301 Aug 31 '23 at 18:38
  • 1
    You should use std::make_unique and std::unique_ptr instead of new/delete. (When you need dynamic polymorphism). Otherwise manage memory using containers like std::vector – Pepijn Kramer Aug 31 '23 at 18:40
  • 2
    I’m voting to close this question because it's just a mess thats all about chatGPT at this point. – UpAndAdam Aug 31 '23 at 18:51
  • @OP read the [C++ core guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) and check anything related to new/delete and raw pointers. Those guidelines is what ChatGPT should have learned. – Pepijn Kramer Aug 31 '23 at 18:55
  • 3
    This question needs to remain available. I want to be able to point to it as a demonstration of how these chat AIs are not actually smart. – sweenish Aug 31 '23 at 19:08
  • *I'm asking Chat GPT this same question and it says:* -- When you are learning something new, you want to go to peer-reviewed books, articles, and if available, examples, advice, etc. from people from the industry. You shouldn't go ask some AI engine for the answers. This is especially the case for written code, where there is an infinite combination of ways you could have written your code, and the AI engine has to be familiar with all of those combinations to give any kind of valid answer. – PaulMcKenzie Aug 31 '23 at 19:15
  • @sweenish the accidental canonical, eh? I support this. Even though it's merely a single datapoint, it does show just how wrong-headed a language model can be with respect to a language it's not designed to model. – user4581301 Aug 31 '23 at 19:21
  • @sweenish I tweeted it, just in case :P – Swift - Friday Pie Aug 31 '23 at 19:22
  • Shouldn't that be you Xed it? – user4581301 Aug 31 '23 at 19:22
  • @user4581301 there is an answer on my ques. wouldn't be a good deed if I'd do such thing. – LiDa Cute Aug 31 '23 at 19:46
  • If you are referring to my suggestion that you heavily modify the question, I removed that comment shortly after the answer appeared. If you're referring to something else... I need a bit more meat to know what you're talking about. – user4581301 Aug 31 '23 at 19:49
  • 1
    That said, it's probably been long enough for the question cooldown to expire. Feel free to ask a new question targeting the over-problem. – user4581301 Aug 31 '23 at 19:50
  • @user4581301 sorry I was mistaken – LiDa Cute Aug 31 '23 at 19:50
  • Goes to show that ChatGPT is only as good as the data it is trained with. Garbage in results in garbage out. – Peter Aug 31 '23 at 23:22

1 Answers1

5

"the line delete is redudant and may lead to undefined behaviour, because when you re-assign the pointer, the data on heap is automatically deallocated"

This is utter nonsense.

What you create via new you must eventually be deleted via delete (and delete [] for what has been allocated via new []). The line does not lead to undefined behavior. It is also not redundant, because if you do not delete the array you have a memory leak. Reassinging the pointer does not deallocate the memory it was pointing to.

It should be noted that you should not use new in the first place. Raw owning pointers are dangerous and will lead to problems. Use std::vector, other containers, and smart pointers for dynamic memory managment, but don't do it manually.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185