472,780 Members | 2,042 Online

Hi:

Below is a simple code:

{
public:
int* a1;
double* a2;
};

a1 = &b1;
a2 = &b2;
}

int main(){
int c1 = 10;
double c2 = 0.5;

int* p1;
double* p2;
p1 = c3->a1;
p2 = c3->a2;

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl;//LINE1

delete c3; //LINE2

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl; //LINE3

return 0;
}

At LINE2, the memory that c3 points to is released, so c3 becomes a
dangling pointer. How about c3->a1 and c3->a2? Are they dangling
pointers?
Can the memory that c3 pointed to be reallocated now?

I ran the code. The output of LINE1 and LINE2 are the same.

Thanks a lot.

john
Jul 22 '05 #1
11 1875

"John" <jo*********@yahoo.com> wrote in message
Hi:

Below is a simple code:

{
public:
int* a1;
double* a2;
};

a1 = &b1;
a2 = &b2;
}

int main(){
int c1 = 10;
double c2 = 0.5;

int* p1;
double* p2;
p1 = c3->a1;
p2 = c3->a2;

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl;//LINE1

delete c3; //LINE2

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl; //LINE3

return 0;
}

At LINE2, the memory that c3 points to is released, so c3 becomes a
dangling pointer. How about c3->a1 and c3->a2? Are they dangling
pointers?
Trying to access memory after its deletion is a source of undefined
behavior.
Can the memory that c3 pointed to be reallocated now?
Yes, system has reclaimed the memory and can use it at its will.
I ran the code. The output of LINE1 and LINE2 are the same.

You mean LINE1 and LINE3. That's what undefined behavior is all about, it
seems to work but can break any time.

Jul 22 '05 #2
John wrote:
Hi:

Below is a simple code:

{
public:
int* a1;
double* a2;
};

a1 = &b1;
a2 = &b2;
}

int main(){
int c1 = 10;
double c2 = 0.5;

int* p1;
double* p2;
p1 = c3->a1;
p2 = c3->a2;

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl;//LINE1

delete c3; //LINE2

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl; //LINE3

return 0;
}

At LINE2, the memory that c3 points to is released, so c3 becomes a
dangling pointer.
How about c3->a1 and c3->a2? Are they dangling
pointers?
Yup. The moment you say c3->x , it invokes UB . You are
trying to access a memory location that has been deallocated / freed.
Can the memory that c3 pointed to be reallocated now?
Of course - yes.
I ran the code. The output of LINE1 and LINE2 are the same.

Purely coincidental.

--
Karthik. http://akktech.blogspot.com .
' Remove _nospamplz from my email to mail me. '
Jul 22 '05 #3
John wrote:
At LINE2, the memory that c3 points to is released, so c3 becomes a
dangling pointer. How about c3->a1 and c3->a2? Are they dangling
pointers?

After deleting c3, c3->a1 and c3->a2 no longer exist. That's doesn't imply
their value turns invalid.

Before deleting 'c3' you saved the value of c3->a1 and c3->a2 into p1 and
p2 respectively, which now point to c1 and c2 (as they were passed by
reference to link1's ctor). Therefore, p1 and p2 are valid pointers and
your program behaves as one would expect.

Max

Jul 22 '05 #4

Jul 22 '05 #5
Max M. wrote:
John wrote:
At LINE2, the memory that c3 points to is released, so c3 becomes a
dangling pointer. How about c3->a1 and c3->a2? Are they dangling
pointers?
After deleting c3, c3->a1 and c3->a2 no longer exist. That's
doesn't imply their value turns invalid.

Before deleting 'c3' you saved the value of c3->a1 and c3->a2
into p1 and p2 respectively, which now point to c1 and c2 (as
they were passed by reference to link1's ctor). Therefore, p1
and p2 are valid pointers and your program behaves as one would
expect.

Not quite: p1 and p2 are indeed valid pointers, but c3 isn't. So it is
(as pointed out by Karthik) fortuitous that you get the same output for
c3 before and after the delete (IIRC c3 is undefined after delete).

<pedantic>

Karthik Kumar wrote: Yup. The moment you say c3->x , it invokes UB . You are
trying to access a memory location that has been deallocated
/ freed.

Actually the OP doesn't say c3->x after the delete.
</pedantic>

--
Lionel B

--
Lionel B

Jul 22 '05 #6

"Karthik Kumar" <ka*******************@yahoo.com> wrote in message
Yup. The moment you say c3->x , it invokes UB . You are
trying to access a memory location that has been deallocated / freed.

Jul 22 '05 #7
Lionel B wrote:

Not quite: p1 and p2 are indeed valid pointers, but c3 isn't. So it is
(as pointed out by Karthik) fortuitous that you get the same output for
c3 before and after the delete (IIRC c3 is undefined after delete).

c3 never gets dereferenced after the delete occurs. Are you arguing that
printing invalid pointer values causes undefined behaviour?

Max

Jul 22 '05 #8
Max M. wrote:
Lionel B wrote:
Not quite: p1 and p2 are indeed valid pointers, but c3 isn't. So it is
(as pointed out by Karthik) fortuitous that you get the same output for
c3 before and after the delete (IIRC c3 is undefined after delete).

c3 never gets dereferenced after the delete occurs. Are you arguing that
printing invalid pointer values causes undefined behaviour?

Absolutely! Use of a deleted pointer value in anyway is undefined.
Last sentence of 3.7.3 says that using the an invalid pointer value
(such as one passed to delete) is undefined.
Jul 22 '05 #9
Max M. wrote:
c3 never gets dereferenced after the delete occurs. Are you arguing
that printing invalid pointer values causes undefined behaviour?

Lionel B wrote previously, choosing his words with immense care:
<quote> ... it is ... fortuitous that you get the same output for c3
before and after the delete ... </quote>

--
Lionel

Jul 22 '05 #10
Karthik Kumar <ka*******************@yahoo.com> wrote in message news:<4189d259\$1@darkstar>...
John wrote:
Hi:

Below is a simple code:

{
public:
int* a1;
double* a2;
};

a1 = &b1;
a2 = &b2;
}

int main(){
int c1 = 10;
double c2 = 0.5;

int* p1;
double* p2;
p1 = c3->a1;
p2 = c3->a2;

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl;//LINE1

delete c3; //LINE2

std::cout<<"c3:"<<c3<<" p1:"<<*p1<<" p2:"<<*p2<<std::endl; //LINE3

return 0;
}

At LINE2, the memory that c3 points to is released, so c3 becomes a
dangling pointer.
How about c3->a1 and c3->a2? Are they dangling
pointers?

Yup. The moment you say c3->x , it invokes UB . You are
trying to access a memory location that has been deallocated / freed.
Can the memory that c3 pointed to be reallocated now?

Of course - yes.

I ran the code. The output of LINE1 and LINE2 are the same.

Purely coincidental.

The pointers c3->a1 and c3->a2 point to c1 and c2. At LINE3, c1 and c2
are still valid. If the memory that c3->a1 and c3->a2 point to is
reallocated, what will happen to c1 and c2?

john
Jul 22 '05 #11

The pointers c3->a1 and c3->a2 point to c1 and c2. At LINE3, c1 and c2
are still valid. If the memory that c3->a1 and c3->a2 point to is
reallocated, what will happen to c1 and c2?

Nothing.

Jul 22 '05 #12

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