-2

If i define array in the next way:
char** array=new char*[3];
and I initialize the array like that for example:

for(int i=0; i<3;i++){  
array[i]=new char[5];  
}  

and after that, I doing:

delete[] array;  

What happens during the delete?

Mathing
  • 41
  • 5
  • 4
    mainly what happens is that you leak lots of memory. every `new` needs to be matched with the corresponding `delete`, best practice is btw zero `new` and zero `delete` – 463035818_is_not_an_ai Jun 05 '18 at 10:13
  • Memory leak happens... – CinCout Jun 05 '18 at 10:13
  • it looks like you actually want a `std::string` or a `std::vector` – 463035818_is_not_an_ai Jun 05 '18 at 10:14
  • @user463035818 But exists cases that you must use `new` and `delete`.. for example, for function that get values from external user, and you want to copy them to your strcture – Mathing Jun 05 '18 at 10:14
  • @Mathing there are rare cases where you need `new` and `delete` the one you describe is not one of them – 463035818_is_not_an_ai Jun 05 '18 at 10:15
  • What exactly are you asking? In the simplest form, storage for `array` gets invalidated and nothing else. But I don't think this satisfies you. Or better, what exactly is your problem. – luk32 Jun 05 '18 at 10:15
  • 1
    memory leak might be acceptable if your use case is building a rocket https://groups.google.com/forum/message/raw?msg=comp.lang.ada/E9bNCvDQ12k/1tezW24ZxdAJ – Micha Wiedenmann Jun 05 '18 at 10:16
  • @user463035818 it looks like I want to understand the behavior of this code... – Mathing Jun 05 '18 at 10:16
  • @user463035818 why not? can you explain please? – Mathing Jun 05 '18 at 10:16
  • I just dont see why you would need `new` or `delete`. If you want to copy a user supplied value into your structure then you simply copy the value. If you ever think you need to manage memory manually you are wrong. Even if you need to allocate something on the heap you'd use smartpointers but no raw `new` and `delete` (unless you like trouble) – 463035818_is_not_an_ai Jun 05 '18 at 10:18
  • @user463035818 I don't want to take them by-value because that it's dangerous. Maybe not in this case, but in total, if someone send me argument I usually want to insert copy of it (new something, and not the source of this).. – Mathing Jun 05 '18 at 10:21
  • 3
    @Mathing you have a big misunderstanding of how C++ works. – bolov Jun 05 '18 at 10:24
  • @Mathing sorry, really no offense, but your last comment literally makes no sense. I suggest you to take a look here https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Jun 05 '18 at 10:26
  • @bolov can you explain what is not correct with what that I said? – Mathing Jun 05 '18 at 10:31
  • The result of the first new-expression `new char*[3]` is released. The results of the new-expressions in the loop are not and will be leaked. General rule: every new-expression needs to be matched exactly once with the corresponding delete-expression. Or, better yet, use standard containers (`std::vector`, `std::string`, etc) - in your case, a `vector` will almost certainly be sufficient (assuming you don't write squirrelly code that makes the containers malfunction) and you will not need to explicitly allocate or deallocate memory at all. – Peter Jun 05 '18 at 10:42
  • @Mathing "what is not correct with what that I said"> Literally everything. If you want to safely use user input then you need to properly sanitise it, simply copying it doesn't magically make it any safer. And even if you do want to copy it, there's still no reason you need to use 'new' to do that. – Sean Burton Jun 06 '18 at 09:23

1 Answers1

3

This causes a memory leak: Only memory of array is freed, but memory reserved for cells is not freed before your program closes.

You should match each new with a delete:

for(int i=0; i<3;i++){  
    delete[] array[i];  
}  
delete[] array;  

If you received the pointer from some function, you need to know its size to be able to free all the memory.

To avoid manual memory handling, you can either use vector of shared pointers (std::vector<std::shared_ptr<char>) or in this specific case, use strings (std::vector<std::string>).

VLL
  • 9,634
  • 1
  • 29
  • 54
  • although technically correct, if the answer doesn't start and end with "don't do this in C++" in my view it is a wrong answer. – bolov Jun 05 '18 at 10:18
  • What is the declaration of `array`? – quamrana Jun 05 '18 at 10:20
  • @quamrana In the question: `char** array` – VLL Jun 05 '18 at 10:21
  • 3
    @bolov - I'd suggest your requirement is a bit harsh. This post answers the question as asked, and explicitly concludes with a mention of alternatives that are preferable in C++. You might like bludgeoning people with repeated admonitions of "don't do this", but such approaches tend to alienate intelligent people after a while - even more so when those people realise that the "don't do this" guidance isn't always correct (even if the circumstances, as in this case, where the guidance is incorrect are rather advanced/specialised). – Peter Jun 05 '18 at 10:54
  • 2
    @bolov I may remind you how agressive Knut became in response to Dijkstra's critique of goto. Just like goto, new\delete can be harmful if misused. "Do not do this at all" is never correct. I would downvote any answer that would boiled down to this. I saw enough people downvoting questions or berating OP for C++ programming for embedded platforms or Arduino. `new/delete` is part of language. Standard class library is still a library and it _may_ not be available. Even standard allows partial implementation. Programmer _should_ know how `new`\`delete` works and now when to use it and when not. – Swift - Friday Pie Jun 05 '18 at 11:00
  • Thank you for the critique. I can indeed go too strong on the imperative "don't do this". Some clarification from my part: I mentioned the answer is correct. When I wrote the comment the answer wasn't mentioning any alternatives. Because the answer is correct I didn't downvote. Yes, the "always do/don't do this or that" kind of advice is almost always wrong. I wholeheartedly agree you need to understand the reasons behind and then you can judge or understand the exception when you can do/not do this or that and what implications that brings. – bolov Jun 05 '18 at 20:24
  • That being said I do believe that because many many students are taught to use `new` `delete` for manual memory management totally ignoring RAII principles we need to first and foremost stop this behavior and then optionally teach how `new` `delete` works and maybe how you can create a `std::vector` like class from scratch. My comment should have been "tell them <> and then show in which circumstances they can be used". But I was too lazy, which doesn't make me less guilty. – bolov Jun 05 '18 at 20:24