2

In one of my large projects I encountered problem with deleting arrays that were initialized with no specified size.
I wrote a simple program to check what is going wrong, here is the code

#include "stdafx.h"

class Checker
{
public:
    Checker()
    :myI(i++){}

virtual ~Checker(){
    printf("%i " , myI);
    fflush(stdout);
}
private:
    int myI;
    static int i;
};

int Checker::i = 0;


int _tmain(int argc, _TCHAR* argv[]){
    Checker* somePointer;
    Checker* anotherPointer;

    somePointer = new Checker[4]{
        Checker(), Checker(), Checker(), Checker()};

    anotherPointer = new Checker[]{
        Checker(), Checker(), Checker(), Checker()};

    delete[] somePointer;

    delete[] anotherPointer; //approach A
    delete anotherPointer; //approach B
    //in approach C anotherPointer is not deleted

    return 0;
}

As You can see anotherPointer is initialized without explicitly defined size.
Of course, only one of the lines marked as approach is active at once.
In approach A output looks like that (< crash> means that program ends unexpectedly)

 3 2 1 0 <crash> 

In approach B output is

 3 2 1 0 4 <crash> 

In approach C some time the output is 3 2 1 0 and other time application crashes without printing anything.

As far as I know initialization without specifying size of array ends with different memory allocation, but i don't know how to solve problem with application that crashes at the end and this is my question.

I'm using Visual Studio Pro 2013 Update 4 (MSVC++)

EDIT
In case that there is no solution to that problem other than explicitly specifying size, my question is
Why that "feature" is implemented in MSCC++ at all?

5
  • What do you mean by ends unexpectedly? Do you get an error message? Commented May 5, 2015 at 23:36
  • 1
    g++ fails to compile your sample code, even after getting rid of the obvious MS-ism; issuing a diagnostic for "new Checker[]{". This appears to be relying on non-portable, compiler-specific feature or behavior, and not standard C++. Commented May 5, 2015 at 23:38
  • 1
    Standard C++'s grammar does not seem to allow new Checker[] with empty brackets. Commented May 5, 2015 at 23:40
  • Accepting it looks like a pretty clear-cut compiler bug. For what it's worth, the preview of VC++ 2015 rejects it with an error message saying: error C3078: you cannot 'new' an array of unknown bounds. Commented May 6, 2015 at 6:35
  • @Jefffrey: It might (or might not) be worth noting that even with the empty brackets, it is almost amazingly close to being syntactically correct (but as a lambda expression). Commented May 6, 2015 at 6:44

1 Answer 1

2

Currently C++ standard doesn't allow allocation of arrays without explicitly specified size (like you can in Java or C#). GCC C++ and Clang both give an error while trying to compile you code (even in C++14 mode). Visual Studio 2015 RC also produces error C3078: you cannot 'new' an array of unknown bounds. Summarizing, it's not a feature, it's a bug in VS 2013 what apparently was fixed in VS 2015.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.