Memory Allocation :: C/C++

This is a discussion on Memory Allocation :: C/C++ within the C++ Programming forums, part of the General Programming Boards category; Hi. I am becoming more familiar with Win32 API especially for winsock and core Windows including threads, processes, and DLL. ...

  1. #1
    kuphryn
    Guest

    Memory Allocation :: C/C++

    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

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    malloc()
    calloc()
    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
    Location
    NJ
    Posts
    1,141
    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.)

    Code:
    #include <iostream>
    
    class SomeClass {
    public:
    	int a;
    	double b;
    	SomeClass() :
    		a(42),
    		b(3.14159265)
    	{ 
    		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
    kuphryn
    Guest
    Okay. Thanks.

    Kuphryn

  5. #5
    kuphryn
    Guest
    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

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, 10: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, 04:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 07:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21