|
From: Damien R <dam...@gm...> - 2013-09-29 11:17:42
|
Hi,
I am using valgrind 3.7.0 and with the following program, valgrind reports
no error.
#include <iostream>
struct Foo
{
void print()
{
std::cout << "foo" << std::endl;
}
};
int main()
{
Foo * foo = new Foo;
delete foo;
foo->print();
return 0;
}
Can someone explain why valgrind does not report any error?
Regards,
Damien R.
|
|
From: Matthias S. <zz...@ge...> - 2013-09-29 12:52:23
|
On 29.09.2013 13:16, Damien R wrote:
> Hi,
>
> I am using valgrind 3.7.0 and with the following program, valgrind
> reports no error.
>
> #include <iostream>
>
> struct Foo
> {
> void print()
> {
> std::cout << "foo" << std::endl;
> }
> };
>
> int main()
> {
> Foo * foo = new Foo;
> delete foo;
> foo->print();
> return 0;
> }
>
> Can someone explain why valgrind does not report any error?
>
Hi!
You call Foo::print() on a deleted instance of Foo, but this method does
not access any data from inside Foo and is no virtual method.
So nothing happens, that valgrind should complain about.
Regards
Matthias
|
|
From: Philippe W. <phi...@sk...> - 2013-09-29 13:03:55
|
On Sun, 2013-09-29 at 13:16 +0200, Damien R wrote:
> Hi,
>
>
> I am using valgrind 3.7.0 and with the following program, valgrind
> reports no error.
>
>
> #include <iostream>
>
> struct Foo
> {
> void print()
> {
> std::cout << "foo" << std::endl;
> }
> };
>
> int main()
> {
> Foo * foo = new Foo;
> delete foo;
> foo->print();
> return 0;
> }
>
>
> Can someone explain why valgrind does not report any error?
The compiler inserts a call to foo print, but this does not
imply a dereference of foo pointer. If you add int a
in Foo, and assigns a value to a inside print, then you get an error.
Philippe
|
|
From: Tom H. <to...@co...> - 2013-09-29 13:32:48
|
On 29/09/13 12:16, Damien R wrote:
> I am using valgrind 3.7.0 and with the following program, valgrind
> reports no error.
>
> #include <iostream>
>
> struct Foo
> {
> void print()
> {
> std::cout << "foo" << std::endl;
> }
> };
>
> int main()
> {
> Foo * foo = new Foo;
> delete foo;
> foo->print();
> return 0;
> }
>
> Can someone explain why valgrind does not report any error?
Yes. Nothing in the "foo->print()" call actually accesses any memory
that was part of the object.
The body of the function doesn't access any member variables, and it's
not a virtual function so the vtable is not read, so there is no access
to any freed memory.
Tom
--
Tom Hughes (to...@co...)
http://compton.nu/
|
|
From: Damien R <dam...@gm...> - 2013-09-30 08:00:30
|
On 09/29/2013 02:22 PM, Tom Hughes wrote:
> Yes. Nothing in the "foo->print()" call actually accesses any memory
> that was part of the object.
>
> The body of the function doesn't access any member variables, and it's
> not a virtual function so the vtable is not read, so there is no
> access to any freed memory.
>
> Tom
>
Thanks to everyone who answered the question but I still do not
understand why foo->print() does work. For me foo is part of the object
because it is the object. Does it mean that the method print is
generated like:
void print(Foo *)
{
std::cout << "foo" << std::endl;
}
If this is the case, can someone give me some resources about the memory
organisation of c++ and about class/methods generation?
Regards,
Damien R.
|
|
From: Tom H. <to...@co...> - 2013-09-30 08:07:38
|
On 30/09/13 08:59, Damien R wrote:
> Thanks to everyone who answered the question but I still do not
> understand why foo->print() does work. For me foo is part of the object
> because it is the object. Does it mean that the method print is
> generated like:
> void print(Foo *)
> {
> std::cout << "foo" << std::endl;
> }
Yes, basically that is exactly what happens. The function (with g++) is
actually called _ZN3Foo5printEv but other than that you have the details
exactly right.
> If this is the case, can someone give me some resources about the memory
> organisation of c++ and about class/methods generation?
Well strictly speaking those are implementation details which may vary
from one compiler to another, or at least from one platform to another.
Tom
--
Tom Hughes (to...@co...)
http://compton.nu/
|