Delete operator

  • Thread starter Dimitris Kamenopoulos
  • Start date
D

Dimitris Kamenopoulos

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.
 
A

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
 
B

Bill Thompson

A said:
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.
 
Y

Ying Yang

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
 
R

Rob Williscroft

Ying Yang wrote in
[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.
 
B

Bill Thompson

Ying Yang said:
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.
 
N

Nils Petter Vaskinn

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.
 
Y

Ying Yang

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
 
K

Kevin Goodsell

A said:
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
 
Y

Ying Yang

A said:
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
 
K

Karl Heinz Buchegger

Ying said:
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.
 
K

Karl Heinz Buchegger

Ying said:
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.
 
G

Gavin Deane

Ying Yang said:
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
 
G

Gavin Deane

Ying Yang said:
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
 

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

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top