Using the delete operator in a for loop.

C

Camel

I am very new to C++ and came across this section of code in an
example, whilst learning about dynamic memory. The example uses the
delete operator in a for loop, it seems to imply that each time the
loop runs the memory for the array will be deleted. I don't think this
is correct. Can someone offer me an explanation as to why this code
works?


I have only included the section which includes the for loop and the
delete operator.

cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}


Thank you very much
 
A

Alan Johnson

Camel said:
I am very new to C++ and came across this section of code in an
example, whilst learning about dynamic memory. The example uses the
delete operator in a for loop, it seems to imply that each time the
loop runs the memory for the array will be deleted. I don't think this
is correct. Can someone offer me an explanation as to why this code
works?


I have only included the section which includes the for loop and the
delete operator.

cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}


Thank you very much

The fragment of code you posted does not call delete[] in a for loop. A
for loop only applies to the following statement (or block if one is used).

It could be that the closing brace just before the "return 0;" line is
closing an additional for loop, but there is no way to tell from the
code you posted.
 
V

Victor Bazarov

Camel said:
I am very new to C++ and came across this section of code in an
example, whilst learning about dynamic memory. The example uses the
delete operator in a for loop,

No, it doesn't. At least not in the 'for' loop that you have in
the code section below.
it seems to imply that each time the
loop runs the memory for the array will be deleted.
Huh?

I don't think this
is correct. Can someone offer me an explanation as to why this code
works?


I have only included the section which includes the for loop and the
delete operator.

cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";

The statement above is the body of the 'for' loop. The statement
below is in the same scope as the 'for' loop, and as such does not
belong to the 'for' loop body.
delete[] p;
}
return 0;
}

V
 
M

Markus Becker

Camel said:
example, whilst learning about dynamic memory. The example uses the
delete operator in a for loop, it seems to imply that each time the
loop runs the memory for the array will be deleted. I don't think this

No, it doesn't.
is correct. Can someone offer me an explanation as to why this code
works?
for (n=0; n<i; n++)
cout << p[n] << ", ";

The loop ends here!

So the:
delete[] p;

is outside the loop.

Markus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top