Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what is the difference between GlobalAlloc / HeapAlloc / malloc / new / etc.?

789 views
Skip to first unread message

Action

unread,
Nov 29, 2002, 9:00:54 PM11/29/02
to
What's the difference actually?

I just use new most of the time, but sometimes, i find the debugger gives
error on the new operator in some of my program.....

thx

Action


Ron Ruble

unread,
Nov 30, 2002, 8:20:58 AM11/30/02
to

"Action" <actionat...@hotmail.com> wrote in message
news:u7LvRRBmCHA.1412@tkmsftngp04...

new: allocates memory for both Plain Old Data (POD)
and class objects; for class objects, also calls constructors
delete: for class objects, calls destructors; for POD data
types and classes, deallocates memory allocated using
new.

malloc: allocates memory for POD data types; cannot
call constructors, not safe for classes.
free: deallocates memory allocated with malloc. Knows
nothing about destructors.

new and delete are C++ standard operators. Always
use them in C++ programs for both POD data types
and classes, unless you are _damn_ sure you know
what you are doing. Like the saying goes, if you have
to ask, you can't afford it.

malloc and free are C standard functions. Like most C
standard functions, they are available in C++ as well.
They are perfectly safe for POD data types in C++.
Some implementations of new invoke malloc, but this
is _not_ necessary.

GlobalAlloc and HeapAlloc are Windows-specific
API functions. Any code that uses them is Windows-
only code.

GlobalAlloc is a legacy function, maintained for
backward compatibility. In Windows 16 bit, addresses
were physical addresses in RAM, and open to
any application. It was desirable to specify whether
you intended memory for your application's
use, or to pass data to other applications. To do this,
the Windows API had Local and Global allocation
functions, and new/delete and malloc/free weren't
recommended.

In Win32, all addresses are virtual, and memory is
protected. GlobalAlloc and HeapAlloc get memory
from the same heap.

HeapAlloc is the native memory allocation function.
new and malloc ultimately call HeapAlloc for memory,
although they may use logic to increase efficiency by
asking the OS for more memory, and parceling it
out as the program requests.

Usually, you'll use new/delete or malloc/free. The
exceptions involve API functions, where you may
use HeapAlloc or other functions. One of these
other functions you didn't ask about is CoGetMalloc,
a COM function for retrieving a COM-specific
allocator. You use this for Shell interface programming,
among other things. Other COM-specific allocators
include all of the SysAllocXxxx functions for
SAFEARRAYS, BSTRs, etc.

More than you wanted to know?

Carl Daniel [MVP]

unread,
Nov 30, 2002, 10:42:47 AM11/30/02
to
"Ron Ruble" <raff...@att.net> wrote in message
news:_W2G9.30951$vM1.2...@bgtnsc04-news.ops.worldnet.att.net...

> "Action" <actionat...@hotmail.com> wrote in message
> news:u7LvRRBmCHA.1412@tkmsftngp04...
> > What's the difference actually?

... lots of gret description. A couple more things I wanted to tack on for
completeness.

> malloc and free are C standard functions. Like most C
> standard functions, they are available in C++ as well.
> They are perfectly safe for POD data types in C++.
> Some implementations of new invoke malloc, but this
> is _not_ necessary.

The only thing guaranteed by the standard is that malloc does NOT call new.
Whether new calls malloc is implementation defined (new in fact does call
malloc in every implementation I'm aware of).

> HeapAlloc is the native memory allocation function.
> new and malloc ultimately call HeapAlloc for memory,
> although they may use logic to increase efficiency by
> asking the OS for more memory, and parceling it
> out as the program requests.

One more layer: HeapAlloc calls VirtualAlloc (*), which is the
lowest-level interface to the kernel memory manager that's accessible to
application programs. VirtualAlloc lets you explicitly manipulate the
virtual address space, with special operations like reserving memory without
commiting, or requesting memory with a particular base address.

-cd

* For grins, I just checked - HeapAlloc doesn't actually call VirtualAlloc.
Instead, it calls an internal function,
ZwAllocateVirtualMemory(), which is also called by VirtualAlloc.


Action

unread,
Dec 1, 2002, 9:46:20 AM12/1/02
to
this is cool!!
thank you very much!

so I should always use new/delete, right?
it seems that there is no reason to use malloc / free

however, is there any chance to have any problem if I use "new"?
I tried several times before...when I run my programs in debug mode, it
generate an exception on the "new" operator..with unsigned char.......(it
gets into some assembly code in "NTDLL!")

Thank you!

action

"Ron Ruble" <raff...@att.net> wrote in message
news:_W2G9.30951$vM1.2...@bgtnsc04-news.ops.worldnet.att.net...
>

Carl Daniel [MVP]

unread,
Dec 1, 2002, 10:39:58 AM12/1/02
to
"Action" <actionat...@hotmail.com> wrote in message
news:OljvohUmCHA.1324@tkmsftngp04...

> this is cool!!
> thank you very much!
>
> so I should always use new/delete, right?
> it seems that there is no reason to use malloc / free

For C++ programming, you should always use new/delete. Except when you must
use something else. For example, to call a 'C' function that's documented
as requiring a pointer to memory which it will ultimately pass to free().
To call such a function you must use malloc(). Example: to call a COM
function which is documented as taking a pointer which it will eventaully
pass to CoTaskMemFree(): you must use CoTasmMemAlloc().

So, always use new/delete unless there's a specific reason not to.

> however, is there any chance to have any problem if I use "new"?
> I tried several times before...when I run my programs in debug mode, it
> generate an exception on the "new" operator..with unsigned char.......(it
> gets into some assembly code in "NTDLL!")

You'd have to show an example of what you ran into - new can (and should) be
used to allocate memory of any type.

-cd


0 new messages