Archived

This topic is now archived and is closed to further replies.

kuphryn

Memory Allocation :: C/C++

Recommended Posts

Hi. I am becoming more familiar with Win32 API especially for winsock and core Windows including threads, processes, and DLL. I began learning programming about a year ago and my first and currently more proficient language is C++. I have no prior programming experience and not knowledge of C. Nonetheless, Win32 API and other Windows programming tools as well and good DOS and linux programs all make use of C run-time library. I would like to know ways to allocate and deallocate a chunk of memory space. In C++, I am familiar with the new and delete operators. However, you have to define the data type you before allocating memory. I C, I believe you can allocate a chunk of memory (BYTE data type?) and store anything there. Furthermore, you can use function, I believe free(), to free the memory. What are some ways to allocate/deallocate memory using C/C++ other than new/delete? Thanks, Kuphryn

Share this post


Link to post
Share on other sites
Win32 has a laundry list of memory allocation functions, which allocate memory in global space, local space, function-local memory, etc. Check MSDN for details.

In terms of standard C and C++, however, malloc()/free() and new/delete are, respectively, the way to go.

As you said, C++ allocates memory for a certain type, as follows:


int *p = new int;


In contrast, malloc() merely allocates a certain amount of memory, specified as an amount of type size_t. This is usually accomplished through the use of sizeof().

Oh, and shouldn't this go in the For Beginners forum?


Don't listen to me. I've had too much coffee.

[edited by - sneftel on August 14, 2002 8:04:49 PM]

Share this post


Link to post
Share on other sites
Yeah.

I looked up memory allocation in Bjarne Stroustrup''s C++ reference. He mentioned all those malloc and malloc derivatives.

Kuphryn

Share this post


Link to post
Share on other sites
quote:
However, you have to define the data type you before allocating memory. I C, I believe you can allocate a chunk of memory (BYTE data type?) and store anything there.

malloc() returns memory aligned for any data type (or maybe any data type that can fit within the block allocated).

new generally doesn''t have to. However using new to allocate a block of chars or unsigned chars is supposed to give memory that''s aligned suitably for storing anything that will fit in the block (For arrays of char and unsigned char, the difference between the result of the new expression and the address returned by the allocation function shall be an integral multiple of the most stringent alignment requirement (3.9) of any object type whose size is no greater than the size of the array being created.).

Such an allocation is thus suitable for constructing any old object into, as long, of course, as it fits.

So, you can define the data type after allocating the memory.

Share this post


Link to post
Share on other sites
Okay. Thanks.

You mean a good point about a a memory block is still a memory block.

For a string or an array of characters for example, you can use strcpy or memcpy. In general, how do you assign any data type to a memory block?

For example:

-----
TCHAR textA[] = TEXT("Test 1 2 3");
TCHAR *pTextA;
malloc(pTextA, 100 * sizeof(TCHAR));
_tcscpy(pTextA, textA, _tcslen(textA) * sizeof(TCHAR));
-----

-----
size_t intOne[] = {0, 1, 2, 3, 4};
size_t *pIntOne;

// Do you have to use memcpy() in thic situation?
-----

-----
AnyDataType mOne;
AnyDataType *pOne;

// How about this situation?
-----

One last question. What is the difference between malloc() and memset()?

Kuphryn

Share this post


Link to post
Share on other sites
malloc allocates a block of memory,
memset sets a number of bytes to a value, for example:

MyStruct ms;
memset(&ms, 0, sizeof(MyStruct));

Johan Ersvik

Share this post


Link to post
Share on other sites