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

This is a discussion on [C++/WinAPI] Checking number of allocated memory within the C++ Programming forums, part of the General Programming Boards category; Hi. How can I check number of memory, allocated by malloc() function ?...

  1. #1
    Registered User jagi's Avatar
    Join Date
    Mar 2005
    Location
    Poland
    Posts
    21

    [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
    Posts
    4,913
    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
    Posts
    136
    This works for Visual C++:

    Code:
    //MemoryLeaks.h
    #ifndef MEMORY_LEAKS_01_H
    #define MEMORY_LEAKS_01_H
    #define CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    
    
    class CMemory_alloc
    {
    public:
       CMemory_alloc()
       {   };
      
       CMemory_alloc(long num)
       {    _CrtSetBreakAlloc(num);   };
       //if they want to break on whatever allocation.  this makes it easy
       
       ~CMemory_alloc()
       {	_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
    
    #endif
    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, 01:56 PM
  3. Help with an algorithm
    By Neo1 in forum C++ Programming
    Replies: 14
    Last Post: 09-19-2007, 04: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-26-2001, 12:45 AM

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