malloc()

This is a discussion on malloc() within the C++ Programming forums, part of the General Programming Boards category; I know how to use new to dynamically allocate memory, but how can we use malloc() wich I think it's ...

  1. #1
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    malloc()

    I know how to use new to dynamically allocate memory, but how can we use malloc() wich I think it's a C function?

    Can you give me an example.Thanks...

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Here is one example of malloc() implementation.

    Code:
    const int bufSize = 100;
    TCHAR *pDataBuf = NULL;
    
    pDataBuf = reinterpret_cast<TCHAR *>(malloc(bufSize + 1));
    pDataBuf[bufSize] = '\0';
    ...
    free(pDataBuf);
    Here is a reference website with information on malloc().

    http://www.cplusplus.com/ref/cstdlib/malloc.html

    Kuphryn

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    So free is like delete?

  4. #4
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    So free is like delete?
    it frees the dynamically allocated memory,yeah,like delete does..
    actually new calls malloc and delete calls free.

    /btq
    ...viewlexx - julie lexx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 04:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 06:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 09:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM

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