Thread: Dynamic memory ... how is this producing Debug assertions in Visual Studio

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Dynamic memory ... how is this producing Debug assertions in Visual Studio

    Well, ok ... not sure what is going on here with this rather innocuous code:

    Code:
    int main(bleh blah){
    	int *p(new int(124));
    	int *q = new int(40);
    	int *r = p;
    	p = q;
    	cout << *r << endl;
    	delete p; delete q;
    	p = q = r = nullptr;
    
            return whatever
    }
    All I am doing is dynamically assigning memory and then deleting it. r is set to a nullptr along with p and q. This is producing debug assertion problems in Visual Studio.

    I am already acquainted with smart pointers such as shared_ptr, unique_ptr, and weak_ptr so I am simply exploring managing dynamic memory myself.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Guess from a C programmer

    delete p; delete q; /* wrong */
    delete r; delete q; /* what I think is right */
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I know it's not important to you, but if you think that int main(blah blah) and return whatever and nullptr and stuff is cute, then you won't get good help. Since the post is incomplete, so's my knowledge, which ........s you over in the end.

    [edit] Also, pretty sure that you can delete p OR delete q OR delete r since they all point to the same region, and you leak memory anyway because of your assignments. [/edit]
    Last edited by whiteflags; 01-22-2013 at 07:48 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by whiteflags View Post
    ... is cute, then you won't get good help.
    My apologies ... not trying to be cute but rather terse as the parameters to main and what it returns are irrelevant to the question. I believe I have also figured it out.

    Code:
    int main(int argc, char *argv[]){
    	int *p(new int(124));
    	int *q = new int(40);
    	int *r = p;
    	delete p;
    	p = q;
    	r = q;
    	cout << *r << endl;
    	delete q;
    	p = q = r = nullptr;
    
    	return EXIT_SUCCESS;
    }
    A pointer had gone invalid somewhere in the previous example.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    int *p(new int(124));
    int *q = new int(40);
    int *r = p;
    p = q;

    The memory stored at location p is leaked after this point. Your problem showed up when you deleted the same region twice.

    delete p; delete q;

    p and q were the same.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by whiteflags View Post
    The memory stored at location p is leaked after this point. Your problem showed up when you deleted the same region twice.

    delete p; delete q;

    p and q were the same.
    Freeing/deleting the same memory twice is undefined behaviour in both C and C++.
    Last edited by manasij7479; 01-22-2013 at 08:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting Memory leak in Visual Studio (Debug mode)
    By elizas in forum Windows Programming
    Replies: 3
    Last Post: 05-06-2010, 11:14 AM
  2. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  3. Debug/Compile C in VISUAL STUDIO --need help!
    By bcgabriot in forum C Programming
    Replies: 4
    Last Post: 05-21-2007, 02:49 AM
  4. about some tricks in Visual Studio debug
    By George2 in forum Tech Board
    Replies: 1
    Last Post: 09-15-2006, 05:34 AM
  5. Visual Studio Debug
    By 4point5 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 03:09 PM