I'm having the following code (C++ Builder 10.3.3):
bool isPrime(int n){
    if (n <= 1)
        return false;
    for(int i = 2; i < n/2; i++)
        if(!(n%i))
            return false;
    return true;
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    auto start_t = high_resolution_clock::now();
    _di_ITask task[50000];
    int count = 50000;
    // _di_ITask* task = new _di_ITask[count]; // compiles, but gives incorrect end result
    vector<int> prim;
    for(int i = 0; i < count; i++){
        task[i] = TTask::Create([i, this, &prim](){
        if(isPrime(i)){
            EnterCriticalSection(&cs);
            prim.push_back(i);
            LeaveCriticalSection(&cs);
            };
        });
        task[i]->Start();
    }
    TTask::WaitForAll(task,(sizeof(task)/sizeof(task[0])-1));
    auto end_t = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(end_t - start_t).count();
    ShowMessage("Prime count: " + String(prim.size()) + " Time: " + IntToStr(duration) + " ms.");
}
When running this code I always get 5134. But, I want to set the number of tasks (_di_ITask objects) dynamically and not to have it fixed. So, instead
_di_ITask task[50000];
i want to have something like this:
int count = 50000;
_di_ITask* task = new _di_ITask[count]();
This compiles, but at the end I always get incorrect (random) result. What am I doing wrong?
