releasing memory doubt

This is a discussion on releasing memory doubt within the C++ Programming forums, part of the General Programming Boards category; what will be the real memory releasing behaviour of following code snippet. Will all memory release properly? - Code: Class ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    4

    releasing memory doubt

    what will be the real memory releasing behaviour of following code snippet. Will all memory release properly? -

    Code:
    Class A
    {
    };
    
    Class B : public A
    {
    };
    
    main()
    {
    
    A *ptr = new B;
    A *ptr1 = new B[10];
    
    delete ptr;
    delete [] ptr1;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    Both the delete and the delete[] result in undefined behaviour since A does not have a virtual destructor, yet you attempt to destroy an object (or array thereof) of B through a pointer to A.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,981
    main should return int. Omitting return types for functions is ill-formed in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions about releasing memory...
    By adarpodracir in forum C Programming
    Replies: 10
    Last Post: 04-07-2012, 04:10 PM
  2. Replies: 4
    Last Post: 11-10-2009, 05:47 PM
  3. Doubt about freeing memory
    By guillermoh in forum C Programming
    Replies: 9
    Last Post: 02-04-2008, 04:26 PM
  4. threads not releasing memory?
    By Spark in forum C Programming
    Replies: 3
    Last Post: 06-08-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