473,387 Members | 1,441 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,387 software developers and data experts.

Delete operator

A
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
Jul 19 '05 #1
14 4561
"A" <A@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...
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


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.
Jul 19 '05 #2
A wrote:

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.


Yes, but the array is created by the compiler and is statically allocated.
You should not delete it.
Jul 19 '05 #3
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
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.

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
Jul 19 '05 #4
Ying Yang wrote in news:3f********@news.iprimus.com.au:
>
> char* ptr = "a string";
>

[snip]
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.


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/
Jul 19 '05 #5
"Ying Yang" <Yi******@hotmail.com> wrote in message
news:3f********@news.iprimus.com.au...
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
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.

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


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.
Jul 19 '05 #6
On Thu, 18 Sep 2003 18:44:32 +0930, A wrote:
Is ptr a pointer to a dynamically created object for which i must use
the delete operator to deallocate memory?


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

Jul 19 '05 #7
A wrote:
Hi,

Consider this:

char* ptr = "a string";


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.

Jul 19 '05 #8

Is ptr a pointer to a dynamically created object for which i must use
the delete operator to deallocate memory?


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.


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

Jul 19 '05 #9
> A wrote:
Hi,

Consider this:

char* ptr = "a string";


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";


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
Jul 19 '05 #10


Ying Yang wrote:
Is ptr a pointer to a dynamically created object for which i must use
the delete operator to deallocate memory?


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.


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?


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
kb******@gascad.at
Jul 19 '05 #11


Ying Yang wrote:
A wrote:
Hi,

Consider this:

char* ptr = "a string";


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";


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.


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
kb******@gascad.at
Jul 19 '05 #12
"Ying Yang" <Yi******@hotmail.com> wrote in message news:<3f**********@news.iprimus.com.au>...
A wrote:
Hi,

Consider this:

char* ptr = "a string";


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";


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.


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
Jul 19 '05 #13
"Ying Yang" <Yi******@hotmail.com> wrote in message news:<3f********@news.iprimus.com.au>...
Is ptr a pointer to a dynamically created object for which i must use
the delete operator to deallocate memory?


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.


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?


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
Jul 19 '05 #14
de*********@hotmail.com (Gavin Deane) wrote in message news:<6d**************************@posting.google. com>...
Not that in all such cases there is a matching delete _somewhere_ (in
this case, in the auto_ptr destructor).


Grrrrr !!

s/Not/Note
Jul 19 '05 #15

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

Similar topics

2
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
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...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
5
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) {...
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...
4
by: mail.dsp | last post by:
Suppose in a class we overload four operators: operator new operator delete operator new operator delete class Test{ public: void * operator new (size_t t){ cout<<"\nCalling... new";
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.