Re: data exchange between two windows

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 05/25/04


Date: Tue, 25 May 2004 13:09:39 -0500

Yasoo wrote:

>I don't know why anyone would want to use "free" on object created with "new".

That would be an error. You must delete what you new, delete[] what you
new[], free what you malloc/calloc/realloc, and so on.

>(I mispoke about allocating a pointer. I was trying to figure out how "new" worked like "malloc".)
>
>Even still, is there a performance benefit? *p=new char[1000] seems clunky. Is there a destructor associated with the "char" object (is "char" an object?)?

There is a do-nothing dtor associated with basic types, primarily so you can
write templates that explicitly call dtors for types used to instantiate the
template.

>I don't see the point in using this for the basic types. Does it reside in a memory manager table differently?

I haven't read this entire thread, as it's kinda long, so I'll just add my
$.02 and hopefully not restate everything that's already been stated.
Concerning using new[] vs. malloc to allocate arrays of basic types, I can
think of three important differences.

1. The "new[] operator" uses the "operator new[]" function, which by default
calls the registered new-handler, if any, and throws std::bad_alloc if the
memory can't be allocated. On the other hand, malloc doesn't call the
new-handler and doesn't throw exceptions.

2. A naive implementation of the "new[] operator" can conceivably store
extra information to hold the array count for element
construction/destruction purposes, adding some overhead to the allocation.
Note that VC doesn't do this for basic types. However, this possibility
highlights that the most direct C++ analogs to malloc are the scalar and
array forms of the "operator new" functions, not the "new operators". These
functions are replaceable and including throwing and non-throwing forms.

3. The "new[] operator" returns a pointer of the correct type, and
new-expressions like "new T[n]" translate n into the number of bytes to
request from "operator new[]", saving you from doing that calculation.

So when is malloc used in C++? It's typically used to help write the
"operator new" functions.

When are the "operator new" functions used directly, as opposed to
implicitly by the "new operators"? The "operator new" functions are used
when writing low-level code such as a std::vector implementation (actually,
those calls would be buried in the default allocator, but the idea is the
same), which internally separates the acts of allocation, construction,
destruction, and deallocation.

What about the "new[] operator"? For basic types, it's used to allocate an
uninitialized array, typically a very large one, where the expense of
initialization by std::vector must be avoided. I'd think such cases are
pretty rare, and for a user-defined type with a default ctor, that advantage
goes away entirely. It's also used to allocate certain multidimensional
arrays.

In general, when thinking about dynamic arrays, std::vector is the first
thing that should come to mind. It's very rare that I say new[].

-- 
Doug Harrison
Microsoft MVP - Visual C++


Relevant Pages

  • Re: openning a file
    ... Sometimes posts arrive out of order, sometimes posts don't arrive at all. ... You might be able so allocate these at file scope or allocate the space using malloc (see the comp.lang.c FAQ for how to allocate 2D arrays using malloc). ...
    (comp.lang.c)
  • Re: xmalloc string functions
    ... Failed malloc() is. ... Why can't you test an allocation failure? ... each can fail on request, each needs to have the request failure dealt ... process Save click because it failed to allocate memory for the event ...
    (comp.lang.c)
  • Re: style question,itoa
    ... able to allocate buffers in some convenient location the callee ... dynamically allocate a block of memory and computing this size to use ... call might require allocating a page for the stack. ... " Checking every single malloc in a bigger application for possible ...
    (comp.unix.programmer)
  • Re: malloc and maximum size
    ... The malloc function takes size_t type argument. ... malloc allocates much much less memory. ... don't assume that you can allocate as much as SIZE_MAX bytes; ... This program compiles fine with gcc 3.4.3 as ...
    (comp.lang.c)
  • Re: xmalloc string functions
    ... Failed malloc() is. ... minor annoyance to a critical failure. ... there is something to recover from. ... Now, if I don't use this half-baked notion of "allocate or die", I can ...
    (comp.lang.c)