Re: Passing more than 1 param in AfxBeginThread
From: Scott McPhillips [MVP] (scottmcp_at_mvps.org.nothere)
Date: 05/19/04
- Next message: Joseph M. Newcomer: "Re: Best Practices: #include"
- Previous message: Joseph M. Newcomer: "Re: New essay: SetWindowRgn"
- In reply to: Dazz: "Re: Passing more than 1 param in AfxBeginThread"
- Next in thread: Jerry Coffin: "Re: Passing more than 1 param in AfxBeginThread"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 19 May 2004 18:56:04 -0500
Dazz wrote:
> Thanks Joe,
>
> I have a question about your solution. what is the difference between declaring the the pointer *p on the heap and then deleting after finishing with it, and just declaring it on the stack?
>
> class Parameters {
> public:
> int x;
> int y;
> CWnd * wnd;
> };
>
> Parameters *p;
> p->x = ...
> p-> = ...
> p->wnd = this;
> AfxBeginThread(thread, p);
>
>
> What i want to know is when should i declare a var on the heap instead of on the stack?
If you declare a variable on the stack it disappears when the function
that declares it returns. Stack variables are very efficient, but their
use is limited to within-one-function. They cannot be accessed later in
some other function. In the case at hand, it means you won't be able to
delete what you new'ed. Another common mistake: If you declared it on
the stack and passed its address to the thread, it might even disappear
before the thread starts!
You must use member variables or the heap when more than one function
needs access to the variable.
-- Scott McPhillips [VC++ MVP]
- Next message: Joseph M. Newcomer: "Re: Best Practices: #include"
- Previous message: Joseph M. Newcomer: "Re: New essay: SetWindowRgn"
- In reply to: Dazz: "Re: Passing more than 1 param in AfxBeginThread"
- Next in thread: Jerry Coffin: "Re: Passing more than 1 param in AfxBeginThread"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|