From: William Djaja Tjokroaminata Date: 2002-08-16T05:55:16+09:00 Subject: Re: Data_Make_Struct Considered Dangerous? Two things: 1) First, it is not the intention to make a clear-cut judgment whether Data_Make_Struct IS dangerous or Data_Make_Struct IS NOT dangerous. As written below, "... it is only dangerous when used incorrectly". By the same token, statement such as "Goto Considered Harmful" is then also false, Goto is only harmful when used incorrectly or unwisely. The intention is to bring up the issue that may occur during this memory allocation with respect to garbage collection. As written below, in C++ we have to use Data_Wrap_Struct instead of Data_Make_Struct. 2) Second, the "danger" in Data_Make_Struct comes from ALLOC and even from the Data_Wrap_Struct itself, which may invoke the garbage collector. However, I still do not get the answer why I ever need to use ALLOC instead of malloc and its derivatives. For example, ignoring run-out-of-memory problem, a C statement such as ptr = ALLOC (my_struct); may create segmentation fault at this point (if some of the memory values are not proper) while a C statement such as ptr = (my_struct*) malloc (sizeof (my_struct)); will never create segmentation fault (at this point). Why do we ever need to use ALLOC (which is tied to a garbage collector) instead of malloc and its derivatives? Regards, Bill ========================================================================= Paul Brannan wrote: > On Thu, Aug 15, 2002 at 11:44:59PM +0900, ts wrote: >> Data_Make_Struct() is *not* dangerous. The goal of Data_Make_Struct() >> is to call ALLOC(), initialize all members of the struct to zero then >> call Data_Wrap_Struct() > This is a problem, though, because setting a struct to all zeroes > is not the same as initializing all members of the struct. It is > impossible to write a macro in C that can properly initialize all C > structs in a generic manner. > In C++, for example, a class is not initialized until its constructor > has completed. I might find it convenient to use Data_Make_Struct and > then to use placement new to initialize the object, but this would be a > bug. Instead, I should call ALLOC, use placement new on the allocated > structure, then call Data_Wrap_Struct to wrap the object. I have to be > very careful of exception-safety issues if I do this. > The same applies in C to any structure that has non-trivial > initialization. If you have non-trivial initialization, then perhaps > Data_Wrap_Struct is more appropriate for your application. I agree with > ts that THIS DOES NOT make Data_Make_Struct dangerous; it is only > dangerous when used incorrectly. > Paul