473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using the delete operator in a for loop.

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

Feb 6 '07 #1
3 1829
Camel wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 6 '07 #2
Camel wrote:
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.

--
Alan Johnson
Feb 6 '07 #3
Camel <el************@yahoo.co.ukschrieb:
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
Feb 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
2
by: hamidi | last post by:
hi all this is the whole of my program: #include "stdafx.h" #ifdef _DEBUG #define new DEBUG_NEW #endif void main()
6
by: shoosh | last post by:
hi for my application under VC6 I need to implement my own memory manager which overrides the global new and delete operators and which Do Not use the normal free() and malloc(). it seemed to...
2
by: Dave | last post by:
Hello all, I'd like to find a source on the web that discusses, in a comprehensive manner and in one place, everything about new / delete. It should include overloading operator new, the new...
5
by: Colleyville Alan | last post by:
I have built a SQL statement that is trying to loop through the fields of a table that was built from a spreadsheet and hence is "short and fat". So rather than hard-coding, I have a loop from...
8
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI) using these scripts. EX: CREATE TABLE(...
12
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before...
10
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a...
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.