Hello!
Please, if I write delete[] in f2 function, then it does not work.
So, if I want to create a dynamic memoy new array in function different from main, where do I delete it, if I want the array elements , created if function , to be reachable in main for all the other functions?
p.s. Pleasse, do not advise me to make new array in main, I thought about that, but the point is in creating the new array in function different form main.
Well, first of all, the delete-line in the f2-function you are referring to won't ever execute because you are doing it after the return-line (which exits the function).
So, if I want to create a dynamic memoy new array in function different from main, where do I delete it, if I want the array elements , created if function , to be reachable in main for all the other functions?
You delete the memory at the point at which you know your program will no longer need it.
That's one of the principal features of dynamic allocation - it allows you to control the lifetime of the memory being allocated. You can allocate it when you need it, and delete it when you no longer need it.
By the way, you really should make the effort to start indenting your code in a sensible fashion. Then it will be much easier for you - and us - to see the flow of control and scope through your code, and spot any errors.
As I said - you know where in the code you need that memory. You know where in the code you will no longer need the memory. Put the delete statement at the point where you no longer need the memory.
Being like that I will need the valgrind or?
Valgrind won't magically tell you where you should or shouldn't put a delete statement. It will help you find improper use of memory, if you get something wrong.
Everythnig worked fine without delete[].
(Except of invisible leak of memory, of course- I ws doing online anyway, but never mind!)
The question explicitly said , to write a FUNCTION which would generate an array. Elements form this array are ment to be used by OTHER FUNCTIONS.
Well, main is also a function, but this is not what THEY ment.
If I delete this array in f2, of course, I can't pass it to main and from main to other functions.
Deleting it in main would prevent the program do wha twas asked.
So, is it possible at all to do it in a function AT ALL?
P.S. mb, I understood, valgrind would "fix" somehow the holes I make in memory by not deleting such variables. Hey, just don't tell me, if I forget to write delete([ ]), some parts of memory are lost for ever, or:) :????
Then I'm not exactly sure what you mean. If you need the array outside of f2, then don't delete it in f2. It should work the way you're doing it now, deleting it in the main-function, that won't constitute a memory leak. The point is, delete it whenever you don't need it any more :P