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

This is a discussion on Dynamic memory ... how is this producing Debug assertions in Visual Studio within the C++ Programming forums, part of the General Programming Boards category; Well, ok ... not sure what is going on here with this rather innocuous code: Code: int main(bleh blah){ int ...

  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
    2,589
    Guess from a C programmer

    delete p; delete q; /* wrong */
    delete r; delete q; /* what I think is right */
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    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 06: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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 07:14 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 02:09 PM

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