Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



One more note:

This is a notation suggestion, that may help you with pointers in C/C++ (I
found that useful myself).

When you have a pointer to something, you can prefix the variable name with
a 'p':

MYDATA MyData;

MYDATA * pMyData; /* pointer to MYDATA */

Now, there is a "cancelation rule": the * and the 'p' cancel each other,
e.g.:

*pMyData

you can see the * and 'p', so they cancel, and *pMyData is kind of MyData
(i.e. *pMyData is of type MYDATA, it is not a pointer anymore).

This comes in handy especially when you have double indirection pointers.

e.g.

void DoSomething( MYDATA ** ppMyData ); /* double pointer --> 2 'p's
prefix */

Now:

- ppMyData is a double indirection pointer to MYDATA (i.e. MYDATA **)
- *ppMyData -> * and the first 'p' cancel, so this is like pMyData, i.e. a
pointer (simple pointer) to MYDATA (i.e. MYDATA *)
- **ppMyData -> both * and 'p's cancel, so this is like MyData, i.e. type
MYDATA (no more pointer).

If you use this notation consistently, I think that this may help in your
code.

e.g.

void config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES );

Change to:

void config(
...
PASSCODE ** ppPassCode,
... );

Your wrong code was:

obj_PASSCODE = malloc (sizeof (PASSCODE));

Now, you see, malloc() is expected to return a pointer to PASSCODE
structure, so malloc() is expected to return a PASSCODE*, right?
You have ppPassCode as function parameter; how can you get a PASSCODE* from
ppPassCode?
Simple: cancel one 'p', using one '*':

*ppPassCode

is of type PASSCODE*, because * and the first 'p' cancel, and you have
pPassCode (i.e. PASSCODE *).

I hope I did not make much confusion with my English... But, trust me, this
rule is very simple, and after some practice you may find it helpful.

Giovanni


.



Relevant Pages

  • Re: Help about the complexity of code
    ... Whether implementors take advantage of the ... delete myData; ... to signify an unassigned pointer. ... Programs that actually *trigger* such trapping are already non- ...
    (rec.games.roguelike.development)
  • Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!
    ... (i.e. *pMyData is of type MYDATA, it is not a pointer anymore). ... Now, you see, mallocis expected to return a pointer to PASSCODE ... You have ppPassCode as function parameter; how can you get a PASSCODE* from ...
    (microsoft.public.vc.language)
  • Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!
    ... (i.e. *pMyData is of type MYDATA, it is not a pointer anymore). ... Now, you see, mallocis expected to return a pointer to PASSCODE ... You have ppPassCode as function parameter; how can you get a PASSCODE* from ...
    (microsoft.public.vc.language)
  • Re: Help about the complexity of code
    ... the pointer passed to NULL? ... bug which reveals itself when I port to a platform that doesn't. ... delete myData; ... freemight return the segment s to the system. ...
    (rec.games.roguelike.development)
  • Re: Help about the complexity of code
    ... the pointer passed to NULL? ... but it would clean up a vast percentage of stale ... delete myData; ... If my platform did auto-NULL deletes, it would be folly to rely on it, ...
    (rec.games.roguelike.development)