[Haskell-cafe] FFI: C-side object not destructed

Maciej Marcin Piechotka uzytkownik2 at gmail.com
Sat Feb 26 13:53:41 CET 2011


On Sat, 2011-02-26 at 14:22 +0300, Miguel Mitrofanov wrote:
> Well, this code in C++ would probably work too:
> 
> Klass *k = new Klass(4,5);
> delete k;
> std::cout << k->getY() << std::endl;
> 
> though smart compiler would probably issue a warning. See, when you
> delete something, C++ doesn't automagically mark your pointer as
> "invalid"; in fact, it preserves all the data in your deleted class.
> If you didn't provide a destructor, then the only outcome of "delete"
> would be that the same memory can be assigned to another object by
> "new" operator, but it doesn't get cleared or invalidated in any way.
> 
> Seems to me, Haskell works in the same way. 

It is implementation defined (so not "C++" but "<your C++
implementation>".

% cat test.cc 
#include <iostream>

int main() {
	int *i = new int(4);
	std::cout << *i << std::endl;
	delete i;
	std::cout << *i << std::endl;
}
% g++ test.cc
% ./a.out 
4
0

I believe that crash in 4th line is legal as well.

Regards





More information about the Haskell-Cafe mailing list