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 3 1867
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
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
Camel <el************ @yahoo.co.uksch rieb:
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 will call it's destructor when it
is made a target of a delete".
|
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()
|
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 work fine on its own but when I tried to use say 'cout' or
'cerr' from STL
the tester crashed upon termination.
using some breakpoints I found out that when I make use of 'cerr' in the
program, there
is a call to my override of delete which do not...
|
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 operator, placement, nothrow, arrays,
etc...
My books cover the topic, I've found FAQs on the web that cover the topic,
and so on, but all the sources I've found are disjointed. There's a bit on
this page, a bit on that page, and so on. The...
|
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 field #6 to the last field
(#63). When I use the fld variable (type integer) in the loop, I have no
problem with this line which is near the end of the SQL statement:
strMySql = strMySql & "Accts., Accts.Manager, " & ","
But when I try to...
| |
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(
Pk_myPrimaryKey INTEGER CONSTRAINT pk PRIMARY KEY DESCRIPTION 'This
is the primary key of the table',
|
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 the beginning of the
array. But I couldn't find this information by looking at the memory.
(Via VS2005 - C++)
My second questions is, if there is a mechanism to know how much memory
is allocated for the array, why don't we use it for things like...
|
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 sizeof(foo)-sized buffer but does NOT call foo::foo().
|
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
the + operator by means of a friend function. Everything worked fine
until I decided to use a variable array size (by using new/delete),
now I get an error when a temporary object is deleted (e.g. after
addition), the error occurs at the delete...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |