Check if there is memory allocated

This is a discussion on Check if there is memory allocated within the C++ Programming forums, part of the General Programming Boards category; Hi I wondered if it's possible to check if a pointer have gotten memory allocated. I'm currently using this method, ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92

    Check if there is memory allocated

    Hi

    I wondered if it's possible to check if a pointer have gotten memory allocated. I'm currently using this method, but it feels a bit ugly and I feel that there got to be a better way to do it:
    Code:
    void md5Model::allocMeshes(int numMeshes)
    {
    	meshes =(md5Mesh*)malloc(sizeof(md5Mesh)*(numMeshes+1));
    	isAlloc = true;
    }
    void md5Model::clear()
    {
    	if (isAlloc)
    		free(meshes);
    }
    Do you know if there is any easier and/or better way to do this? I like malloc better than new, I don't know why though. Is it maybe better to use new, or doesn't it matter?

    Thanks in advance

    Daniel

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by The Wazaa
    I like malloc better than new, I don't know why though. Is it maybe better to use new, or doesn't it matter?
    Use new. new calls a constructor malloc doesn't.
    Kurt

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,200
    The way i do when i have to be sure that memory is allocated is to always set the pointer to NULL when it has been freed and in all constructors that doesnt allocate memory.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Okay, I see. Thanks for your replies


    Daniel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  3. Replies: 8
    Last Post: 10-12-2004, 11:41 AM
  4. Memory Check ?!?
    By Moni in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2003, 09:28 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

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