Re: data exchange between two windows

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/23/04


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



Relevant Pages

  • Re: problems with free()
    ... No need to initialize mystr to NULL, ... you could just as easily put the malloc ... printf("Could not allocate memory"); ... array into the pointer object mystr. ...
    (comp.lang.c.moderated)
  • Re: Does malloc() reuse addresses?
    ... if i allocate some memory with malloc() and later free it (using ... allocate memort at the same starting address and will return the same ... In this example, if an instance is freed, and a pointer to it becomes ... If you are doing what you say with the instance array, ...
    (comp.lang.c)
  • Re: Are the following 2 code statements equivalent
    ... In the calloc() case, the ... is not allowed to allocate some smaller amount. ... You call memsetwithout checking the result of the malloc() call. ... If mallocfails, it returns a null pointer, and the call to memset ...
    (comp.lang.c)
  • Re: malloc inside function (I know... I *did* search google first ;)
    ... > and the return value of malloc should ALWAYS be checked for NULL, ... Functions that allocate memory commonly return a pointer to the memory ... and then the float * return is certainly more natural ...
    (comp.lang.c)
  • Does malloc() reuse addresses?
    ... if i allocate some memory with malloc() and later free it (using ... allocate memort at the same starting address and will return the same ... int isInstanceValid ... In this example, if an instance is freed, and a pointer to it becomes ...
    (comp.lang.c)