Re: data exchange between two windows
From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 05/25/04
- Next message: jonnyair<>_: "Re: CEdit or CRichEditCtrl text highlighting"
- Previous message: JFisher365: "Re: When To Initialize View Controls In a Document View MFC Architecture"
- 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: 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++
- Next message: jonnyair<>_: "Re: CEdit or CRichEditCtrl text highlighting"
- Previous message: JFisher365: "Re: When To Initialize View Controls In a Document View MFC Architecture"
- 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
|