PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

LocalAlloc vs. calloc

This is more of a curiosity than anything else.

What is the technical difference between the 2? THe docs don't say too much. I KNOW that LocalAlloc goes to the heap, and is limited to 1 meg unless you do a new create heap...but wouldn't calloc go to the heap as well?

I guess what I am asking is, which, if either, is better, and why?
calloc calls LocalAlloc under the hood.

calloc is the more portable of the two.

There's currently no 1MB limit that I'm aware of.
that's kinda what i figured.

"For Windows CE version 1.0 and 1.01, the local heap for each process cannot exceed 1 MB" (MSDN)

I must have misread.
what is calloc and what would you use it for..?
to allocate space from the heap initialized to 0s.

Code:

TCHAR *szSomeString;
szSomeString = (TCHAR *)calloc (255, sizeof(TCHAR));
wcscpy (szSomeString, L"HELLO!");
free (szSomeString);


That would make a 255 wchar buffer initialized to 0, then copy HELLO! into it, then free the memory...useless save for this example.
thanks, so its for allocating memory.
In a C++ context, why would I use calloc() instead of this:

Code:

;
szSomeString = new TCHAR[255];

...

delete [] szSomeString;
;

Is calloc() or malloc() faster then new?

Thanks!
faster, maybe...mostly because you are not going through the constructor/destructor as you would in your example. You are also going under the assumption that something, like TCHAR, has a valid class definition.

calloc is the same as malloc except that it sets the memory to zeros on allocation.

in essense, all you are doing is allocating raw memory from the heap. it is not instantiated classes or anything else, and it is not bound to any datatype.