Re: data exchange between two windows
From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/23/04
- Next message: Joseph M. Newcomer: "Re: Question about processing messages during exit"
- Previous message: Joseph M. Newcomer: "Re: ExitThread hazardous to your destructors"
- In reply to: Yasoo: "Re: data exchange between two windows"
- Next in thread: Yasoo: "Re: data exchange between two windows"
- Reply: Yasoo: "Re: data exchange between two windows"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 22 May 2004 23:32:13 -0400
'new' is designed to allocate dynamic memory. That's what it does.
You won't find much of a discussion of malloc in the C/C++ reference, except perhaps the
sentence describing malloc/calloc/free/realloc in Stroustrup, 3rd edition, p. 577, §19.4.6
"These functions should be avoided in favor of new, delete, and standard containers. These
functions deal with uninitialized memory. In particular, free() does not invoke
destructors for the memory it frees. An implementation of new or delete may use these
functions, but there is no guarantee that it does. For example, allocating an object using
new and deleting it using free is asking for trouble. If you feel the need to use
realloc(), consider relying on a standard container instead; doing that is usually simpler
and just as efficient (§16.3.5)"
There are a few scattered examples that show how you might implement operator new in terms
of a lower-level allocator such as malloc, but malloc is not required, as the previous
paragraph illustrates.
You don't allocate pointers to dynamic memory; you allocate dynamic memory and get a
pointer to it. Of course, you could allocate a pointer, although that is not a common
thing to do. For example, if I wanted to allocate a pointer to dynamic memory, I might do
char * * p = new char *;
which would allocate an (uninitialized) pointer to storage, although the storage itself
does not have to be dynamically allocated.
to allocate 100 ints, you would write
int * p = new int[100];
which is shown on pages 128-129 of Stroustrup 3rd edition. The argument which is the
number of items to allocate does not have to be a constant, e.g.,
count = c_ListBox.GetSelItemCount();
int * p = new int[ count];
c_ListBox.GetSelItems(count, p);
is a typical sort of thing you do to handle a multiselect listbox.
joe
On Sat, 22 May 2004 14:51:03 -0700, "Yasoo" <anonymous@discussions.microsoft.com> wrote:
>Joe -
>
> I don't see that in the documentation for "new". What is the syntax of "new" to allocate a pointer to dynamically allocated memory?
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
- Next message: Joseph M. Newcomer: "Re: Question about processing messages during exit"
- Previous message: Joseph M. Newcomer: "Re: ExitThread hazardous to your destructors"
- In reply to: Yasoo: "Re: data exchange between two windows"
- Next in thread: Yasoo: "Re: data exchange between two windows"
- Reply: Yasoo: "Re: data exchange between two windows"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|