What's wrong?

3 views
Skip to first unread message

Michael

unread,
Aug 15, 2006, 11:04:41 PM8/15/06
to
Hi,

I am trying to delete p to see whether the thing pointed toby p is
deleted. However, the program is stuck there. Could you please help me
out?

Thanks in advance,
Michael

#include <iostream>

int main()
{
char a[]="abcde";

char* p=new char;
char* q=a;

p=a;

std::cout<<p<<'\n';

delete p;

//std::cout<<q;

return 0;

}

Victor Bazarov

unread,
Aug 16, 2006, 12:03:57 AM8/16/06
to
Michael wrote:
> I am trying to delete p to see whether the thing pointed toby p is
> deleted. However, the program is stuck there. Could you please help me
> out?

Why are you trying to delete what you didn't allocate using 'new' or
'new[]'?

>
> Thanks in advance,
> Michael
>
> #include <iostream>
>
> int main()
> {
> char a[]="abcde";

So, 'a' is an array of char, 6 chars long, 'a', 'b', 'c', 'd', 'e', '\0'.

> char* p=new char;

Here 'p' is a pointer to a dynamically allocated _single_ character.

> char* q=a;

Here 'q' points to the first character in the 'a' array.

>
> p=a;

Here you _lose_ the old value of 'p' and make it point to the first
element of 'a' array (just like 'q').

> std::cout<<p<<'\n';

You probably see 'abcde' here. Since 'p' points to the 'a' array,
you essentially output the contents of that array.

>
> delete p;

And here you try to delete the array. You didn't allocate it using
'new' or 'new[]', so this try has undefined behaviour.

>
> //std::cout<<q;
>
> 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


David Harmon

unread,
Aug 16, 2006, 12:06:38 AM8/16/06
to
On 15 Aug 2006 20:04:41 -0700 in comp.lang.c++, "Michael"
<mich...@gmail.com> wrote,

>I am trying to delete p to see whether the thing pointed toby p is
>deleted. However, the program is stuck there. Could you please help me
>out?

You are trying to delete a pointer value that was not obtained from
new. Don't do that, it's against the rules.

Also, never try to look at a pointer value after it's been deleted.
That's against the rules as well.

Now, what was it you are trying to accomplish?

Jack Klein

unread,
Aug 16, 2006, 12:08:40 AM8/16/06
to
On 15 Aug 2006 20:04:41 -0700, "Michael" <mich...@gmail.com> wrote
in comp.lang.c++:

> Hi,
>
> I am trying to delete p to see whether the thing pointed toby p is
> deleted. However, the program is stuck there. Could you please help me
> out?

Your concept of pointers, arrays, and new and delete is what is wrong.

> Thanks in advance,
> Michael
>
> #include <iostream>
>
> int main()
> {
> char a[]="abcde";

This defines an array of 6 characters, including the '\0' null
terminator at the end.

> char* p=new char;

This defines a pointer to char and allocates exactly one char for it
to point to.

> char* q=a;

This defines another pointer to char, and points it to the 'a' in the
6 character array named 'a'.

> p=a;

Here you make an error that could be very serious in a real program.
You overwrite the address returned by "new" with the address of the
array 'a'. You have just created a memory leak, because you no longer
have the address returned by "new", so you cannot delete it.

> std::cout<<p<<'\n';
>
> delete p;

Here you make an error that is even more serious, even in a tiny test
program like this one. You delete a different pointer value than what
you received from new. This is undefined behavior and quite likely to
crash the program right at this point.

You can only delete the exact pointer value you received from new. You
can only delete[] the exact pointer value you received from new[]. Not
whatever value happens to be in the same pointer object, but the exact
pointer value.

> //std::cout<<q;
>
> return 0;
>
> }

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Frederick Gotham

unread,
Aug 16, 2006, 12:41:09 PM8/16/06
to
Michael posted:

> I am trying to delete p to see whether the thing pointed to by p is
> deleted.


You don't understand pointers. There's nothing magical about pointers --
they're simply used for storing a memory address. Just as how an "int"
stores an integer number, and just as how a "double" stores a floating-
point number, a pointer stores a memory address.

A pointer variable can also indicate the type of the object whose address
it is intended to hold. For instance, the following defines a pointer to
hold the address of an int:

int i;

int *p = &i;

A pointer is just like any other built-in type: it's an object which takes
up space in memory. Let's say that the address of an int on our system is a
32-Bit value. Our int* will look like something like so in memory:

0000 0111 0000 1000 0101 1000 0010 1111

In decimal, this is the memory address 117987375.


> #include <iostream>
>
> int main()
> {
> char a[]="abcde";
>
> char* p=new char;


"new" is a unary operator ("unary" mean's that it takes one sole
operand). What it does is construct an object of the type you specify, and
then returns a pointer to the object.

"p" is a pointer variable which shall hold the address of a char. The
address you store in it is the address of the char you dynamically
allocated by using "new". Let's say that this address is:

0100 1110 0101 1000 000 1010 0001 0100

In decimal, this is memory address 657197588.


> char* q=a;


"q" is a pointer variable which shall hold the address of a char. The
address you store in it is the address of the first char in the "a" array.
In this context, it's the same as writing:

char *q = &a[0];

Let's say that this address is:

1110 1111 1111 1111 0000 0001 10011 1100

In decimal, this is memory address 8052933436.


> p=a;


Here, you store in "p" the address of the first element of array "a". In
this context, it's the same as writing:

p = &a[0];

Now, "p" and "q" have the following values:

p == 8052933436

q == 8052933436


> std::cout<<p<<'\n';


Here you print the address which is stored in "p".


> delete p;


"delete" is a unary operator ("unary" mean's that it takes one sole
operand). What it does is destruct an object. You specify the address of
the object which it shall destruct. The operand you supply it with is "p".
As we know, "p" contains the address 8052933436.

The char you allocated by using "new" above has the address 657197588,
but this is NOT the address you supply as an operand to "delete".

If you supply "delete" with a memory address which does NOT point to an
object which was allocated via "new", then you have undefined behaviour.

Your program invokes undefined behaviour.

--

Frederick Gotham

Default User

unread,
Aug 16, 2006, 1:30:38 PM8/16/06
to
Michael wrote:

> Hi,
>
> I am trying to delete p to see whether the thing pointed toby p is
> deleted. However, the program is stuck there. Could you please help me
> out?

What book are you using? You make the most bizarre fundamental errors
that's it's hard to believe you're working with a solid text.

Brian

Howard

unread,
Aug 16, 2006, 2:05:25 PM8/16/06
to

"Default User" <defaul...@yahoo.com> wrote in message
news:4kh31uF...@individual.net...

Bookses? We don't need no stinkin' bookses! :-)

-H


Victor Bazarov

unread,
Aug 16, 2006, 5:53:30 PM8/16/06
to

We thinkses you means "bookseses"...


Reply all
Reply to author
Forward
0 new messages