Freopen not writing output after a function call

389 Views Asked by At

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!

2

There are 2 best solutions below

2
Öö Tiib On BEST ANSWER

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.

4
Serge Ballesta On

Disclaimer: it may not be a true answer, and is certainly not the expected answer. But it is the most I can do here, and it would not fit in a comment...


It is time to find a good tutorial on C++...

This instruction is terrible:

    vector<int>* touch_circles = (vector<int>*)malloc(sizeof(vector<int>)*size);
  1. you should never use malloc in C++, because it only allocates memory but does not construct the objects. You should only use new and even new should be only be used with extreme caution
  2. it does not make sense to dynamically allocate a vector. A vector is a container that takes care of dynamic allocation for the objects it will contain
  3. unless for special use cases, you should avoid to use raw pointer in C++. Returning a pointer to a dynamically allocated object is a code smell.

That is not all. Vectors are arrays of variable size. When you have a compile time constant size, you should use a std::array.

Said differently, your current code consistently uses the bad container: raw arrays or pointers where you should use vectors and vectors where you should use a std::array.

This is really too terrible code for me to understand what you are trying to do and help you to fix it. Please write it in acceptable C++ in a new question.

I do not want to be offensive here. I just want to advise you to learn good practices, because it is really essential if you want to learn C++.