c++freopen

Freopen not writing output after a function call


I was doing a programming question and was using freopen to redirect the streams. The problem I am facing is that the printf command is not printing on the output file after stdout redirection. I even tried using fflush but couldn't get any results.

Here is my code

    #include<iostream>
    #include<vector>
    #include<cmath>
    #define fc(a) static_cast<float>(a)
    using namespace std;
    
    vector<int>* touch(const vector<int> arr[], int size)
    {
        vector<int>* touch_circles = (vector<int>*)malloc(sizeof(vector<int>)*size);
        int i1, i2, dis;
        for(i1 = 0; i1 < size; i1++)
        for(i2 = i1+ 1; i2 < size; i2++)
        {
            dis = static_cast<int>(ceil(pow(pow(fc(arr[i1][0]) - fc(arr[i2][0]),2) + pow(fc(arr[i1][1]) - fc(arr[i2][1]),2),0.5)));
            if (dis <= arr[i1][2] + arr[i2][2])
            {
                touch_circles[i1].push_back(i2);
                touch_circles[i2].push_back(i1);
            }
        }
        return touch_circles;
    }
    
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("D:\\C++\\input.txt","r",stdin);
        freopen("D:\\C++\\output.txt","w",stdout);
        freopen("D:\\C++\\output.txt","w",stderr);
        #endif
        int t, x, y, n;
        int itr, i, i1, i2;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d %d %d", &x, &y, &n);
            vector<int> arr[n];
            for(itr = 0; itr < n; itr++)
            {
                scanf("%d %d %d", &i1, &i2, &i);
                arr[itr].push_back(i1);
                arr[itr].push_back(i2);
                arr[itr].push_back(i);
            }

            //The 'fflush'es are just for trial, problem persists with or without any of them
            fflush(stdout);
            vector<int> *touch_list = touch(arr, n);
            fflush(stdout);
            printf("Expected");
            fflush(stdout);
        }
    }

Here is my input.txt

1
20 10
2
10 7 2
10 4 2

My ouput.txt is empty. The code compiles fine and there are no errors, it just runs and finishes without printing anything on the output file. One weird thing is that the output is printed on ouput.txt if I comment out the function call from main. I don't understand why it happens as I don't think there is anything inside the function that could possibly affect the file streams. Any help is appreciated, I'm totally out of ideas now!


Solution

  • The code like yours produces pointer to uninitialised memory cast into pointer of vector:

    vector<int>* touch_circles = (vector<int>*)malloc(sizeof(vector<int>*)*size);
    

    That is just undefined behaviour to use such vectors also the memory alloated is likely insufficient. It probably crashes or hangs your program and no output is produced. Do not use malloc in C++ code like that. If you need raw array of vectors then write:

    vector<int>* touch_circles = new vector<int>[size];
    

    Or even better have vector of vectors.