June 25, 2024, 06:01:16 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


New() pointer question

Started by Haim, July 02, 2006, 07:39:59 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Haim

I am confused about the usage of New(..) in Aurora.
I saw in several samples that poiners to UDT's are declared, and the members of the UDT are then assigned and the UDT passed to some function. (without using New)
In other cases I noted that New(UDT,1) is used first.

When should I use New and when can the pointer to the UDT be used directly?

Can someone explain this to me?

Ionic Wind Support Team

New in Aurora is no different then in IBasic.  With the exception that it works for classes as well.

Show me what your confused about, what example code?
Ionic Wind Support Team

Haim

I was looking at 'advanced listview sample' - the code for OnNotify, that has a pointer to an NMHDR structure as an argument.
This argument is then used to retrieve data. It is never explicitly allocated with new(), but it works fine.

On the other hand when I tried to use SendMessageA(m_hwnd,Lvm_subitemhittest,0,pinfo) (pinfo being a pointer to LVHittestinfo, It worked properly only after assigning the pointer with pinfo=new(LVhittestinfo,1);

It seems as if sometimes the called function takes care of allocating the pointer, but not always.
What are the rules?









Ionic Wind Support Team

In that instance Windows is doing the allocation and sending the pointer in a message (WM_NOTIFY).



Ionic Wind Support Team

Haim

Thanks.
If I understand correctly there is no rule. Every function may behave differently. I will have to look up the documentation...   :-\

Ionic Wind Support Team

Well of course there are rules.

All of the the OnXXX handlers are windows messages.  If Windows sends you a pointer then you can be sure it is safe to access.

You can't just define a pointer and access it without initializing it first.  Address of a variable, memory allocated with NEW, etc.  An unitialized pointer, also known as a NULL pointer, will make your program crash.

If your using sendmessage and sending some window a pointer through a message then you are responisible for the memory.

With all pointers you should test for NULL first before acessing though. 

Paul.
Ionic Wind Support Team

Haim