Thread: Side effects

  1. #1
    I lurk
    Join Date
    Aug 2002

    Side effects

    Hmm, would there be any terrible side effects if delete[] was called on something allocated with new? (as in, calling delete[] on a single entity)

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    What I was told by one of my C++ teachers, way back when, is that when you allocate with new[], the size you allocated is stored somewhere in the allocated block so that when delete[] is called it knows how much to free. If that is indeed the case then whatever value is in that spot is how much will be freed.

    So, that would in fact be a terrible side effect... Someone please correct me if I am wrong.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas

    Re: Side effects

    Originally posted by Eibro
    Hmm, would there be any terrible side effects if delete[] was called on something allocated with new? (as in, calling delete[] on a single entity)
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  4. #4
    Registered User
    Join Date
    May 2003

    I tried compiling this under VC++ 7.1 (about the most standardized compiler out there)

    #include <iostream>
    int i;
    class C{
    		std::cout << "Construct." << std::endl;
    		i = 0;
    		std::cout << "Destruct " << ++i << std::endl;
    int main(){
    	C* myC = new C;
    	delete[] myC;
    Without the [], I get the proper output:

    Destruct 1

    With the [], a debug build fails an assertation. In a release build the destructor keeps getting called over and over; I terminated the program when it was well into the 50,000s.

    Bottom line: Under some compilers, it might work, but under most, it probably won't. And the C++ standard makes no guarantees about anything; it is undefined behavior to use delete[] on something allocated with new, just as it's undefined to use delete on something allocated with new[].

    Note that it would be perfectly legal if I used "C* myC = new C[1];". In fact, if I used that form, it would be illegal to call delete instead of delete[].

    As an aside, it is even legal to say "new C[0]". It doesn't do anything, but in many cases it makes code simpler to just allow zero-length arrays versus having to check for positive lengths.
    Last edited by Cat; 07-22-2003 at 11:37 PM.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    On some older compilers the delete [] does not even compile which is strange. I think Turbo C++ does use it, but Borland C++ 4.52 for Windows does not - or the other way around, I forget.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL example cube is black, white, grey only
    By edwardtisdale in forum Windows Programming
    Replies: 7
    Last Post: 09-22-2007, 02:37 PM
  2. Strange side effects?
    By _Elixia_ in forum C Programming
    Replies: 4
    Last Post: 08-16-2005, 03:25 PM
  3. Stupid Logic Problem Need Outside Viewpoint
    By RP319 in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2005, 10:59 PM
  4. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 01:58 AM
  5. printSquare( side ); ???
    By o0o in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2004, 05:50 AM