Hello!
Please, what is the right way to delete the thing in that program?
I thought delete[] z, because we acutally DO have an array in the end.
Am I right?
I want to delete the whole array with all its elements, so I suppose delete[] z woudl do this.
Delete z would delete just variable z which is a pointer and contains as a value the adress of the first elemen of a array created in pus function, doesn' ti?
MANY THANKS!!!
delete [] and deletework on different memory structures. If you use delete [] on single object created with new, behavior is unpredicted (probably writing over memory). Same when executing deleteon objects created withnew [] - it would lead to memory leak and again - undefined behavior.
in your case you wrote: int* a=newint[5];
so you should do delete [] a;
Hello!
I think U did not understand my question!
In my function we have array a on two places, cause function pus() is called twice.
1.First call alloctes value of its elements to z.
But , as we told before, z is not an array. A "virtual" array will be creatied by pointer arthmetics.
So, I think, to free the FIRST adress of the array a(called by the line35, we have to write delete[]z, as I did, before return 0.
But, if we write delete z, then we delete- what? z is the pointer, and its adress is different form the address of the z[0], is it?
The adress of z[0] is just its VALUE!!!
So, if we write delete z, what do owe acheive IN THIS PROGRAM at all?
2. Anyway, w ecall pus() twice in that program. In second call array a is created again , but on ANOTHER ADRESS. WHat do we have to do, to clear the second adress field from its value?
1. Calling non-matching variant of delete leads to undefined behavior. You should call correct variant of delete. Calling incorrect one will achieve nothing aside from possible problems with your program
2.
In second call array a is created again , but on ANOTHER ADRESS. WHat do we have to do, to clear the second adress field from its value?
Hello, MiiNiiPaa! Was waiting for U!
Look, what does this mean in praxix? Should I write delete[] z twice? I cannot delete a, cause in that case I wouldn't be able to pass it to main, would I ?
So if I want to delete array from its first address (the adress it got in line 35) I just write delete[] z, don't I?
And, what do I do to free places of the second adres? What do I exactly have to do, and WHERE????
MANY THANKS, hope U all understad what I want to achieve! (and as program is always giving errors, I cannnot test the values on the adresses)
Hello!
It seems to me , U understood my question better then I did the answer!!!
Right this "smelts" like what I wanted to do:
Or you can do delete[] pus(), but it isn't of much use unless you just want to see "Created array at address" line been outputted in your function.
well, line 39 , pus(st) creates an array on second adress, doesn+t it? And it IS created by new[], isnt it?
So, sth like delete[] pus() would exactly describe what I acutally thought to do. Is that a right way to to that , and what did U think with the second part of yr sentence?
Your line with thif function: cout<<"main: "<<pus(st)<<endl;
You cannot use delete[] pus(st) here because you want to output result to cout. You need to save return value to a variable to delete it later
1 2 3 4 5 6 7 8 9
//1 Recommended
int* arr = pus(st);
cout<<"main: "<< arr <<endl;
delete[] arr;
//2 If you like obscure syntax
int* arr;
cout<<"main: "<< (arr = pus(st)) <<endl;
delete[] arr;