Delete operator 
July 19th, 2005, 06:55 PM
| | |
Hi,
Consider this:
char* ptr = "a string";
Is ptr a pointer to a dynamically created object for which i must use the
delete operator to deallocate memory? I'm sure the "a string" part creates
an array of characters, and returns a pointer to the first element.
Regards
wert
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ( http://www.grisoft.com).
Version: 6.0.518 / Virus Database: 316 - Release Date: 11/09/2003 | 
July 19th, 2005, 06:55 PM
| | | | re: Delete operator
"A" <A@iprimus.com.au> wrote in message
news:3f697766_1@news.iprimus.com.au...[color=blue]
> Hi,
>
> Consider this:
>
> char* ptr = "a string";
>
> Is ptr a pointer to a dynamically created object for which i must use the
> delete operator to deallocate memory? I'm sure the "a string" part creates
> an array of characters, and returns a pointer to the first element.
>
> Regards
> wert
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system ( http://www.grisoft.com).
> Version: 6.0.518 / Virus Database: 316 - Release Date: 11/09/2003
>
>[/color]
ptr is definitely NOT a dynamically created object, nor is "a string". If
you attempt to delete ptr, either a run time error will result, or the
memory manager's free list will be messed up.
Memory for literal strings and other 'static' objects is allocated when the
program is loaded, and is not in the realm of dynamically allocated memory. | 
July 19th, 2005, 06:55 PM
| | | | re: Delete operator
A wrote:
[color=blue]
> char* ptr = "a string";
>
> Is ptr a pointer to a dynamically created object for which i must use the
> delete operator to deallocate memory? I'm sure the "a string" part creates
> an array of characters, and returns a pointer to the first element.[/color]
Yes, but the array is created by the compiler and is statically allocated.
You should not delete it. | 
July 19th, 2005, 06:55 PM
| | | | re: Delete operator
[color=blue][color=green]
> > Hi,
> >
> > Consider this:
> >
> > char* ptr = "a string";
> >
> > Is ptr a pointer to a dynamically created object for which i must use[/color][/color]
the[color=blue][color=green]
> > delete operator to deallocate memory? I'm sure the "a string" part[/color][/color]
creates[color=blue][color=green]
> > an array of characters, and returns a pointer to the first element.
> >
> > Regards
> > wert[/color]
>
> ptr is definitely NOT a dynamically created object, nor is "a string". If
> you attempt to delete ptr, either a run time error will result, or the
> memory manager's free list will be messed up.
>
> Memory for literal strings and other 'static' objects is allocated when[/color]
the[color=blue]
> program is loaded, and is not in the realm of dynamically allocated[/color]
memory.
I gather that the compiler creates an array of characters for the string
literal on the stack, and thus there is no need to use the delete operator.
Regards BN | 
July 19th, 2005, 06:55 PM
| | | | re: Delete operator
Ying Yang wrote in news:3f69841c_1@news.iprimus.com.au:
[color=blue][color=green][color=darkred]
>> >
>> > char* ptr = "a string";
>> >[/color][/color][/color]
[snip]
[color=blue]
> I gather that the compiler creates an array of characters for the
> string literal on the stack, and thus there is no need to use the
> delete operator.
>
>[/color]
Nope, the array of char is staticaly allocated (like a static or
global is), the pointer ptr is created in automatic storage (usually
"the stack") and the compiler initializes it to point to the first
ellement of the array.
Rob.
-- http://www.victim-prime.dsl.pipex.com/ | 
July 19th, 2005, 06:55 PM
| | | | re: Delete operator
"Ying Yang" <YingYang@hotmail.com> wrote in message
news:3f69841c_1@news.iprimus.com.au...[color=blue]
>[color=green][color=darkred]
> > > Hi,
> > >
> > > Consider this:
> > >
> > > char* ptr = "a string";
> > >
> > > Is ptr a pointer to a dynamically created object for which i must use[/color][/color]
> the[color=green][color=darkred]
> > > delete operator to deallocate memory? I'm sure the "a string" part[/color][/color]
> creates[color=green][color=darkred]
> > > an array of characters, and returns a pointer to the first element.
> > >
> > > Regards
> > > wert[/color]
> >
> > ptr is definitely NOT a dynamically created object, nor is "a string".[/color][/color]
If[color=blue][color=green]
> > you attempt to delete ptr, either a run time error will result, or the
> > memory manager's free list will be messed up.
> >
> > Memory for literal strings and other 'static' objects is allocated when[/color]
> the[color=green]
> > program is loaded, and is not in the realm of dynamically allocated[/color]
> memory.
>
> I gather that the compiler creates an array of characters for the string
> literal on the stack, and thus there is no need to use the delete[/color]
operator.[color=blue]
>
>
> Regards BN
>
>[/color]
A simplified model (which may not be correct for all compilers) is as
follows. Please note that this is based on my experience writing assembly
language in the early 90's for MS-DOS, and other environments may take
different approaches.
1) static memory: for literal strings, global variables, etc.
2) the stack, storage for function parameters and variables local to
functions, and the address to which the function returns. When a function
is called, the stack pointer (a memory address) is modified so that the
local variables are allocated. At the time I was working with software, one
had to be careful not to overflow the stack into the...
3) The heap: remaining unused memory allocated dynamically, say by malloc()
or new()
Thus, memory might look like this:
STATIC literals, etc.
....
STACK growing down
....
....
....
HEAP growing up.
Thus, in your example, if ptr was declared in a function, a small bit of
stack space enough to store a memory address is associated with 'ptr', the
address contained in 'ptr' refers to an area in the static area where the
literal string is stored.
Where you to define an array of characters, or a std::string, as a local
variable, and assign the literal text to that variable, then the values
contained in the static area would be copied to the appropriate location in
the stack. | 
July 19th, 2005, 06:55 PM
| | | | re: Delete operator
On Thu, 18 Sep 2003 18:44:32 +0930, A wrote:
[color=blue]
> Is ptr a pointer to a dynamically created object for which i must use
> the delete operator to deallocate memory?[/color]
You've already got your question answered but here is a nice little rule:
Never use new without using delete later
Never use delete without using new first
There are exceptions, but they have to do with classes that "take
ovnership" of dynamically allocated objects you give them pointers to, and
delete them for you when they're done. Any things like that should be
documented so don't go around worrying about it.
--
NPV
"Linux is to Lego as Windows is to Fisher Price." - Doctor J Frink | 
July 19th, 2005, 06:58 PM
| | | | re: Delete operator
A wrote:
[color=blue]
> Hi,
>
> Consider this:
>
> char* ptr = "a string";[/color]
This uses a deprecated and dangerous language feature - the implicit
conversion of a string literal to a (non-const) char *. You should use
this instead:
const char* ptr = "a string";
The rest of the post has already been addressed.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. | 
July 19th, 2005, 07:00 PM
| | | | re: Delete operator
[color=blue][color=green]
> > Is ptr a pointer to a dynamically created object for which i must use
> > the delete operator to deallocate memory?[/color]
>
> You've already got your question answered but here is a nice little rule:
>
> Never use new without using delete later
> Never use delete without using new first
>
> There are exceptions, but they have to do with classes that "take
> ovnership" of dynamically allocated objects you give them pointers to, and
> delete them for you when they're done. Any things like that should be
> documented so don't go around worrying about it.[/color]
lets say you have a class as follows:
class A
{
private:
myObject* a;
myObject* b;
public:
A()
{
a = NULL;
b = NULL;
}
~A()
{
delete a;
delete b;
}
}
is this the exception your talking about?
wty | 
July 19th, 2005, 07:00 PM
| | | | re: Delete operator
> A wrote:[color=blue]
>[color=green]
> > Hi,
> >
> > Consider this:
> >
> > char* ptr = "a string";[/color]
>
> This uses a deprecated and dangerous language feature - the implicit
> conversion of a string literal to a (non-const) char *. You should use
> this instead:
>
> const char* ptr = "a string";[/color]
why would i want to make it a pointer to a constant? if i wanted to make
changes to the string literal then it would no longer be possible.
Regards
ttttttttttttt
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ( http://www.grisoft.com).
Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003 | 
July 19th, 2005, 07:00 PM
| | | | re: Delete operator
Ying Yang wrote:[color=blue]
>[color=green][color=darkred]
> > > Is ptr a pointer to a dynamically created object for which i must use
> > > the delete operator to deallocate memory?[/color]
> >
> > You've already got your question answered but here is a nice little rule:
> >
> > Never use new without using delete later
> > Never use delete without using new first
> >
> > There are exceptions, but they have to do with classes that "take
> > ovnership" of dynamically allocated objects you give them pointers to, and
> > delete them for you when they're done. Any things like that should be
> > documented so don't go around worrying about it.[/color]
>
> lets say you have a class as follows:
>
> class A
> {
> private:
> myObject* a;
> myObject* b;
>
> public:
> A()
> {
> a = NULL;
> b = NULL;
> }
> ~A()
> {
> delete a;
> delete b;
> }
> }
>
> is this the exception your talking about?[/color]
In this specific case?
No. The above is plain silly. You didn't allcoate anything
with new, thus there is no need to delete something.
Although passing a NULL pointer will do no harm, it is still
silly.
--
Karl Heinz Buchegger kbuchegg@gascad.at | 
July 19th, 2005, 07:00 PM
| | | | re: Delete operator
Ying Yang wrote:[color=blue]
>[color=green]
> > A wrote:
> >[color=darkred]
> > > Hi,
> > >
> > > Consider this:
> > >
> > > char* ptr = "a string";[/color]
> >
> > This uses a deprecated and dangerous language feature - the implicit
> > conversion of a string literal to a (non-const) char *. You should use
> > this instead:
> >
> > const char* ptr = "a string";[/color]
>
> why would i want to make it a pointer to a constant? if i wanted to make
> changes to the string literal then it would no longer be possible.
>[/color]
Even if you don't make it a const pointer, it is still illegal to try
to modify a string literal. A string literal is constant by definition!
This is why
const char* ptr = "a string";
is *much* better. The compiler will hinder you to do it.
The fact that the compiler has to allow
char* ptr = "a string";
has historical reasons and is there for backwards compatibility. But
this does not mean it is still good practice.
--
Karl Heinz Buchegger kbuchegg@gascad.at | 
July 19th, 2005, 07:00 PM
| | | | re: Delete operator
"Ying Yang" <YingYang@hotmail.com> wrote in message news:<3f6adf56$1_1@news.iprimus.com.au>...[color=blue][color=green]
> > A wrote:
> >[color=darkred]
> > > Hi,
> > >
> > > Consider this:
> > >
> > > char* ptr = "a string";[/color]
> >
> > This uses a deprecated and dangerous language feature - the implicit
> > conversion of a string literal to a (non-const) char *. You should use
> > this instead:
> >
> > const char* ptr = "a string";[/color]
>
> why would i want to make it a pointer to a constant? if i wanted to make
> changes to the string literal then it would no longer be possible.[/color]
Precisely. You are not allowed to make changes to a string literal.
Unfortunately the language allows the implicit conversion to non-const
char* so the compiler can't always help you.
This compiles but is a bug.
char* ptr = "a string";
ptr[0] = 'A'; // Not allowed - undefined behaviour I think.
This does not compile.
const char* ptr = "a string";
ptr[0] = 'A'; // error C2166: l-value specifies const object
If you are going to use pointers to char in favour of std::string for
string literals, always use const char* so the compiler can spot this
mistake for you.
GJD | 
July 19th, 2005, 07:00 PM
| | | | re: Delete operator
"Ying Yang" <YingYang@hotmail.com> wrote in message news:<3f6adc1d_1@news.iprimus.com.au>...[color=blue][color=green][color=darkred]
> > > Is ptr a pointer to a dynamically created object for which i must use
> > > the delete operator to deallocate memory?[/color]
> >
> > You've already got your question answered but here is a nice little rule:
> >
> > Never use new without using delete later
> > Never use delete without using new first
> >
> > There are exceptions, but they have to do with classes that "take
> > ovnership" of dynamically allocated objects you give them pointers to, and
> > delete them for you when they're done. Any things like that should be
> > documented so don't go around worrying about it.[/color]
>
> lets say you have a class as follows:
>
> class A
> {
> private:
> myObject* a;
> myObject* b;
>
> public:
> A()
> {
> a = NULL;
> b = NULL;
> }
> ~A()
> {
> delete a;
> delete b;
> }
> }
>
> is this the exception your talking about?[/color]
No. The exception is things like std::auto_ptr.
std::auto_ptr<int> p(new int(42));
I have used new to allocate an int. The address of that int is used to
initialise the auto_ptr. But auto_ptr is designed (and documented)
such that it now owns the pointer. It is responsible for delete-ing it
so there will be no delete in my code to match the new I used.
Not that in all such cases there is a matching delete _somewhere_ (in
this case, in the auto_ptr destructor).
GJD | 
July 19th, 2005, 07:01 PM
| | | | re: Delete operator deane_gavin@hotmail.com (Gavin Deane) wrote in message news:<6d8002d0.0309190618.6a5087e6@posting.google. com>...[color=blue]
> Not that in all such cases there is a matching delete _somewhere_ (in
> this case, in the auto_ptr destructor).[/color]
Grrrrr !!
s/Not/Note |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,702 network members.
|