Where to put delete (from with new created array) in function?

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
      #include<iostream>
    using namespace std;
    
    int new_int(int a){
    cin>>a;
    return a;
    }

 
    int* f2(int z){
    int* str=new int[40];
    int s=-1;
    srand(time(0));
     
    int zn=0;
     
    int random_integer;
    for(int index=0; index<10; index++){
    random_integer = (rand()%(z));
    str[index]= random_integer;
    cout<<"str["<<index<<"]="<<random_integer<<endl;
    s=s+1;
    zn=zn+random_integer;
    if (zn>3*z){cout<<"Last value is: "<<random_integer<<endl<<"Whole sum is: "<<zn<<endl<<"Index number: "<<s;break;}
     
    }
    return str;
    delete[]str;
    }
     
     
    void sort(int po[40], int do2) {
    for (int p=1; p < do2; p++) {
    for (int i=0; i < do2-p; i++) {
    if (po[i] < po[i+1]) {
    int temp = po[i];
    po[i] = po[i+1];
    po[i+1] = temp;
    }
    }
    }
    for(int t=0;t<do2;t++){cout<<po[t]<<endl;}
    }
     
    int add(int po[40], int do3){
    int all=0;
    for (int z=0;z<do3;z++){
        all=all+po[z];
        }
    return all;
}
    
     
    int main(){
    int amain;
    int doh=new_int(amain);
    int tre=doh/3;
    int vp=40;
     
    int* z;
    z=f2(tre);
     
    cout<<endl<<endl<<endl;
     
     
    // for(int h=0;h<40; h++) {cout<<z[h]<<endl;}
    sort(z, vp);
    cout<<"The whole sum: "<<add(z, vp)<<endl;
     
    delete[]z;
    return 0;
    }
Last edited on
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.


MANY THANKS!!!
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.
I know all U said, just wondering where to put that delete-cause I JUST SIMPLY NEED THIS ARRAY!!!

Being like that I will need the valgrind or?
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.
Last edited on
Hello!
No, no, the problem is next:

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
Last edited on
Thanks!!!

Is it written ok:

delete[]z?
Topic archived. No new replies allowed.