The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: DC on January 26, 2006, 04:00:16 AM

Title: pointer Q
Post by: DC on January 26, 2006, 04:00:16 AM
is there a better way to translate this then this?
from:
if (pPDX->hDevMode != NULL)
    GlobalFree(pPDX->hDevMode);
to:
    lea     eax, pd.hDevMode
    mov     al, [eax]
    .if al != NULL
        invoke  GlobalFree, pd.hDevMode
    .endif
Title: Re: pointer Q
Post by: zooba on January 26, 2006, 05:08:43 AM
.if(pd.hDevMode)
    invoke  GlobalFree, pd.hDevMode
.endif


Alternatively you can include '!= NULL' in the .if statement, but since NULL will (at least in a Windows environment) evaluate to 0, there's no need.
Title: Re: pointer Q
Post by: Mincho Georgiev on January 26, 2006, 03:56:32 PM
DC,
I dont know where you find this ,but mem free for a structure member ,especially GlobalFree is not a good idea (you can do it for the whole struct if it's dnamically allocated). You better put NULL in that member instead.
If it's a single varialbe struct member offcourse,not a another structure in it (or class member structure).
Title: Re: pointer Q
Post by: P1 on January 26, 2006, 04:34:42 PM
Quote from: shaka_zulu on January 26, 2006, 03:56:32 PMI dont know where you find this ,but mem free for a structure member ,especially GlobalFree is not a good idea (you can do it for the whole struct if it's dnamically allocated). You better put NULL in that member instead.
He is converting M$ recommended code from C.

Most of the time, M$ does know what it's doing.  Most of the time, we may not be privileged to know.

A handle object is dword, so you need to fix this:
    mov     al, [eax]
    .if al != NULL

The low order byte can be zero, but the handle may not be NULL.

so that, the GlobalFree is actual releasing the complete handle object.

Regards,  P1  :8)
Title: Re: pointer Q
Post by: Mincho Georgiev on January 26, 2006, 04:52:06 PM
Yeah, more atention is never useless. hDevMode is a handle to dynamically allocated DEVMODE. Sorry, i didn't even read the var's name.
Title: Re: pointer Q
Post by: DC on January 27, 2006, 12:35:34 AM
thanx guys,
I was assuming that pd.hDevMode was a pointer to a string, and didn't know if they were testing the pointer or the member.
take care,
DC
Title: Re: pointer Q
Post by: zooba on January 27, 2006, 01:27:36 AM
In Microsoft-world, handles always start with an 'h'. Pointers to strings usually involve 'p', 'lp' and 'sz', but there's not much in the way of conventions (well, perhaps there is, but I can't figure it out from looking at their names) :U
Title: Re: pointer Q
Post by: Tedd on January 27, 2006, 12:57:24 PM
Quote from: zooba on January 27, 2006, 01:27:36 AM
In Microsoft-world, handles always start with an 'h'. Pointers to strings usually involve 'p', 'lp' and 'sz', but there's not much in the way of conventions (well, perhaps there is, but I can't figure it out from looking at their names) :U

If you really want to get into it, then you should search for information about 'hungarian' notation. Personally I think they get a bit too hard to read when you have more than 2 or 3 letters prefixed.
I prefer to just stick with 'h' for handles, 'p' for pointers ('lp' means long-pointer, but all my pointers are longs (dwords) anyway) and occasionally 'sz' for string (zero terminated) pointer.
Title: Re: pointer Q
Post by: EduardoS on January 29, 2006, 01:41:46 PM
I'm not a C/C++ specialist, but i think the "->" operator works in this way:

mov eax, pPDX
mov eax, (struct ptr [eax]).hDevMode
Title: Re: pointer Q
Post by: u on February 02, 2006, 04:42:32 AM
Exactly, EduardoS!
So the whole code is

mov eax,pPDX
mov eax,[eax].DEVMODE.hDevMode
.if eax
invoke GlobalFree,eax
.endif