Thread: Memory Allocation :: C/C++

  1. #1

    Memory Allocation :: C/C++


    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?


  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    GlobalAlloc() //windows API
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    You can still use new to construct an array of char (guarenteed to be 1 byte) with the sizeof a class, and then use placement new to construct an object in that memory.

    I'd stay away from it though unless you really need to do some hardcore memory optimization (like you have one object that dies before another is created, and you don't want to dealloc and realloc, so you just hold onto the memory and use inplace new.)

    #include <iostream>
    class SomeClass {
    	int a;
    	double b;
    	SomeClass() :
    		std::cout << "SomeClass()" << std::endl;
    	~SomeClass() {
    		std::cout << "~SomeClass()" << std::endl;
    int main() {
    	// allocate enough to fit a SomeClass
    	char* blah = new char[sizeof(SomeClass)];
    	// use in place operator new to construct SomeClass without allocating more memory
    	SomeClass* pSC = new(blah) SomeClass;
    	blah = 0; // make pSC sole pointer to dynamic class
    	std::cout << pSC->a << ' ' << pSC->b << std::endl;
    	delete pSC;
    	return 0;
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Okay. Thanks.


  5. #5
    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()?


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM