Thread: [C++/WinAPI] Checking number of allocated memory

  1. #1
    Registered User jagi's Avatar
    Join Date
    Mar 2005

    [C++/WinAPI] Checking number of allocated memory

    Hi. How can I check number of memory, allocated by malloc() function ?
    [I'm from Poland]
    [Sorry for my English, because I just learn]

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    malloc accepts as it's only parameter, a value provided by you to tell it how much space to allocate. It will return a pointer to that location. If it fails to allocate the amount of space you told it to, it will return NULL. Short of scanning that space in memory for the end of your data, that is the only way I can think of to check how much space was allocated. If this isn't what you're looking for, try and give us a little more detail as to what it is that you are trying to do.

    malloc() is part of the standard C library (also part of the standard C++ library - which you appear to be using). Because this particular question does not refer to the Windows API, I am moving this thread to the C++ board.

  3. #3
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    This works for Visual C++:

    #ifndef MEMORY_LEAKS_01_H
    #define MEMORY_LEAKS_01_H
    #define CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    class CMemory_alloc
       {   };
       CMemory_alloc(long num)
       {    _CrtSetBreakAlloc(num);   };
       //if they want to break on whatever allocation.  this makes it easy
       {	_CrtDumpMemoryLeaks();   };
       //dumps leaks
    };//this will report memory leaks accurately as long 
    //as the object of this class is declared as the very first thing in the file
    To use it, include this file in your project, and declare a CMemory_alloc object as the very first global var in your program. If there are any memory leaks when you run it from the IDE, they will be reported in the output window. Note that this ONLY works for Visual C++.

    I got sample code from the MSDN and made this class. It's been pretty useful to me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Help with an algorithm
    By Neo1 in forum C++ Programming
    Replies: 14
    Last Post: 09-19-2007, 03:25 PM
  4. Checking if memory has been dynamically allocated
    By Xzyx987X in forum C Programming
    Replies: 28
    Last Post: 03-14-2004, 06:53 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM