CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Knowing if a memory address has been malloc'd

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,595

    Knowing if a memory address has been malloc'd

    Is there a way to know if a memory address got assigned with malloc() / realloc() etc? Consider this example:-

    Code:
    char *string = malloc (20);
    
    free (string);
    
    // sometime later...
    
    free (string); // Will flag up an error (with MSVC)
    I've been given a program which is crashing in the above way (i.e. it seems to be trying to free memory which already got freed earlier). To some extent I can spot the problem like this:-

    Code:
    char *string = malloc (20);
    
    free (string);
    string = 0;
    
    // sometime later...
    
    if (string == 0)
          printf ("Something wrong here !!\n");
    else
          free (string);
    Unfortunately, that doesn't seem to be catching all the problems. I figured that something like this might help - but is there any technique that can achieve this..?

    Code:
    char *string = malloc (20);
    
    free (string);
    
    // sometime later...
    
    if ((string == 0) || (string_isnt_an_address_that_can_be_freed()))
          printf ("Something wrong here !!\n");
    else
          free (string);
    If there was some way to detect an address which isn't 'free-able' I could place a breakpoint at the printf() line and then step backwards through the code to figure out what's going wrong. But is there any way to know if a particular address is 'free-able'?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,144

    Re: Knowing if a memory address has been malloc'd

    In VS, you could try using _free_dbg instead of free and compiling in debug mode. If an error occurs in freeing memory then errno is set to an error code.
    See https://msdn.microsoft.com/en-us/library/16swbsbc.aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,595

    Re: Knowing if a memory address has been malloc'd

    Thanks 2kaud. I'm already building in Debug mode but it didn't occur to me to check errno. I'll give that a try.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Knowing if a memory address has been malloc'd

    Also try the memory corruption tools I suggested here:
    http://forums.codeguru.com/showthrea...-tool-for-MSVC
    Typically these tools will fail for large apps due to runaway memory usage, but there are some ways to get around that as well. Let me know if it does not work for you and I'll explain more.
    Nobody cares how it works as long as it works

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,595

    Re: Knowing if a memory address has been malloc'd

    Thanks guys. I eventually discovered that there's a library function called _CrtIsValidHeapPointer(). It can only be used in Debug mode but for my purposes it's exactly what I need.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,144

    Re: Knowing if a memory address has been malloc'd

    Nice one! Works a treat in Debug configuration

    Code:
    #include <crtdbg.h>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
    	char * pc = (char *)calloc(100, sizeof(char));
    
    	cout << "new pc: " << _CrtIsValidHeapPointer(pc) << endl;
    
    	if (_CrtIsValidHeapPointer(pc)) {
    		free(pc);
    		cout << "Good free 1" << endl;
    	}
    	else
    		cout << "Bad free 1" << endl;
    
    	if (_CrtIsValidHeapPointer(pc)) {
    		free(pc);
    		cout << "Good free 2" << endl;
    	}
    	else
    		cout << "Bad free 2" << endl;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)