digitalmars.D - struct - set to null?
- Brad Anderson (10/16) May 31 2004 and later:
- Vathix (16/32) May 31 2004 lacks
- Brad Anderson (14/68) May 31 2004 Vathix,
- Ben Hinkle (6/33) Jun 01 2004 I bet the Java code is explicitly setting oldFont to null to help the GC
- Brad Anderson (9/14) Jun 01 2004 And I would too, except that later, there's a test to see if LOGFONT is
I am porting some Java code that used classes as structs (because Java lacks structs).LOGFONT oldFont;and later:void releaseWidget() { oldFont = null; }The compiler (dmd 0.89, WinXP) complains that you cannot implicitly convert void* to LOGFONT. So, what options do I have to set this struct to null or point to nothing?oldFont = '\0'; (now it's char to LOGFONT)Also, how do I do the following?if(oldFont == null) return;I poked around the DM website and the Wiki, and found constructors for structs with opCall() but nothing about destructors or setting/comparing structs to null. BA
May 31 2004
"Brad Anderson" <brad sankaty.dot.com> wrote in message news:c9eloi$2d83$1 digitaldaemon.com...I am porting some Java code that used classes as structs (because Javalacksstructs). > LOGFONT oldFont; and later: > void releaseWidget() { > oldFont = null; > } The compiler (dmd 0.89, WinXP) complains that you cannot implicitlyconvertvoid* to LOGFONT. So, what options do I have to set this struct to null or point to nothing? > oldFont = '\0'; (now it's char to LOGFONT) Also, how do I do the following? > if(oldFont == null) return; I poked around the DM website and the Wiki, and found constructors forstructswith opCall() but nothing about destructors or setting/comparing structsto null.BAMaybe you should use a pointer and set it to null when you're done with it. You can't set a struct to null just like you can't set an int to null. Sometimes you do want to initialize all data to zero, but that's different, I do this: (cast(byte*)(&struct))[0 .. struct.size] = 0; I admit it's ugly, memset() is actually cleaner. To reinitialize the struct you could do this: struct = struct.init; But either would be very inefficient to do often and worse to check for those values (especially for that huge LOGFONT).
May 31 2004
Vathix, What about another struct member/property (whatever they're called). struct LOGFONT { ... bool isCurrent } I could set it to true or false and not bother with re-initializing everything. Any thoughts as to how it would affect any .sizeof calcs in its use? I.E. any negative effects because the WinAPI is expecting a certain set of data and it would receive extra? Thanks for the response. BA Vathix wrote:"Brad Anderson" <brad sankaty.dot.com> wrote in message news:c9eloi$2d83$1 digitaldaemon.com...I am porting some Java code that used classes as structs (because Javalacksstructs).convertLOGFONT oldFont;and later:void releaseWidget() { oldFont = null; }The compiler (dmd 0.89, WinXP) complains that you cannot implicitlyvoid* to LOGFONT. So, what options do I have to set this struct to null or point to nothing?structsoldFont = '\0'; (now it's char to LOGFONT)Also, how do I do the following?if(oldFont == null) return;I poked around the DM website and the Wiki, and found constructors forwith opCall() but nothing about destructors or setting/comparing structsto null.BAMaybe you should use a pointer and set it to null when you're done with it. You can't set a struct to null just like you can't set an int to null. Sometimes you do want to initialize all data to zero, but that's different, I do this: (cast(byte*)(&struct))[0 .. struct.size] = 0; I admit it's ugly, memset() is actually cleaner. To reinitialize the struct you could do this: struct = struct.init; But either would be very inefficient to do often and worse to check for those values (especially for that huge LOGFONT).
May 31 2004
Brad Anderson wrote:I am porting some Java code that used classes as structs (because Java lacks structs). > LOGFONT oldFont; and later: > void releaseWidget() { > oldFont = null; > } The compiler (dmd 0.89, WinXP) complains that you cannot implicitly convert void* to LOGFONT. So, what options do I have to set this struct to null or point to nothing? > oldFont = '\0'; (now it's char to LOGFONT) Also, how do I do the following? > if(oldFont == null) return; I poked around the DM website and the Wiki, and found constructors for structs with opCall() but nothing about destructors or setting/comparing structs to null. BAI bet the Java code is explicitly setting oldFont to null to help the GC collect the LOGFONT as garbage. I would skip the oldFont=null in D. A related question has to do with how the port passes LOGFONT to functions: are you passing by value or pointer or inout? -Ben
Jun 01 2004
Ben Hinkle wrote:I bet the Java code is explicitly setting oldFont to null to help the GC collect the LOGFONT as garbage. I would skip the oldFont=null in D.And I would too, except that later, there's a test to see if LOGFONT is null. if(LOGFONT == null)A related question has to do with how the port passes LOGFONT to functions: are you passing by value or pointer or inout?I believe we are switching to pass them by pointer. Which is better? BTW, LOGFONT will have extra properties if version(Unicode) is true. The old way in Java is to have a base class and the unicode and non-unicode classes inherit from it and add their wchar/char properties. BA
Jun 01 2004