Re: Passing more than 1 param in AfxBeginThread

From: Scott McPhillips [MVP] (scottmcp_at_mvps.org.nothere)
Date: 05/19/04


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]


Relevant Pages

  • Re: FindFirstFile Possible Memory Leak
    ... It's interesting what you say about adding to the stack during ... Declaring all variables at the top is an artifact of how programming ... instructions were setup. ... Interpreted languages were not quite so constrained as their only ...
    (microsoft.public.vb.general.discussion)
  • Re: FindFirstFile Possible Memory Leak
    ... Declaring all variables at the top is an artifact of how programming ... instructions were setup. ... creating a stack setup for the call. ... Interpreted languages were not quite so constrained as their only ...
    (microsoft.public.vb.general.discussion)
  • Re: static allocation question...
    ... provide a stack. ... >{int a;} ... >to declaring a union. ... I have to <SNIP> now... ...
    (comp.lang.c)
  • Re: Stack Overflow
    ... "Igor Tandetnik" wrote: ... > Two common causes are 1) runaway recursion, and 2) declaring large ... > arrays as local variables. ... Specifically, more _stack_ memory. ...
    (microsoft.public.vc.language)
  • Re: Creating member vars on the heap, not the stack
    ... on the heap, not the stack. ... Other than declaring all the members of the class as static, ...
    (microsoft.public.vc.language)