-1

Consider the following function in C++:

 set<int> g2(int l){
    cerr<<"g2 "<<l<<" started"<<' '<<endl;
    set<int> jav;
    jav.clear();

    if(l==greed){
        cerr<<"g2 "<<l<<" ended"<<endl;
        return jav;
    }
    if(l==0){
        cerr<<"g2 "<<l<<" ended"<<endl;
        return jav;
    }
     if(sg2[l].size()){
        cerr<<"g2 "<<l<<" ended"<<endl;
        return sg2[l];
    }
   // cerr<<"alan injam"<<endl;
    set<int> y=g2(l-1);
    cerr<<"injam"<<endl;
    jav.insert(y.begin(),y.end());
    jav.insert(s[l].begin(),s[l].end());
    int t_l=tl(l);
    set<int> rr;
    rr.clear();
    rr=alt(l,t_l);
    jav.erase(rr.begin(),rr.end());
    sg2[l]=jav;
    cerr<<"g2 "<<l<<" ended"<<endl;
  //  cerr<<jav.size()<<endl;
    return jav;
}

Many times( but not always), the command set<int> y=g2(l-1), throws this exception:

Process returned -1073740940 (0xC0000374)

Do you know what is the reason and how can I solve it?

Please consider that operator = is defined for a set in C++ STL.

Here is the whole code:

https://github.com/SoroushVahidi/Scheduling-problem/blob/main/withset.cpp

  • 1
    In `set jav; jav.clear();`, `jav.clear();` is useless. – Jarod42 May 24 '22 at 17:22
  • That is not an exception (in the C++ sense). It is your program crashing. Given the line it is crashing on, it is possible that you are simply exhausting the stack by recursive calls. What value of `l` are you trying to call the function with? If it is too large, it will crash. (That's why recursive functions like this are usually a bad idea.) – user17732522 May 24 '22 at 17:24
  • 1
    `jav.erase(rr.begin(),rr.end());` is UB, as iterators don't belongs to the container. – Jarod42 May 24 '22 at 17:25
  • What are `greed`, `sg2`, `y`? – Jarod42 May 24 '22 at 17:26
  • @Jarod42 I want to initialize jav. I am not sure when I define a new set in a function, it is always empty. Moreover, can clearing that set to be the reason for my problem? – Soroush Vahidi May 24 '22 at 17:26
  • 1
    Extra `jav.clear();` is not the reason of the crash, but it is useless. Default constructor of `std::set` is enough. – Jarod42 May 24 '22 at 17:27
  • @user17732522 Thank you for your reply. I have mentioned in the post which line has the problem. I have checked it. However, I do not know why it sometimes crashes on that line and the solution. Moreover, the test case that it crashes on, is not large. – Soroush Vahidi May 24 '22 at 17:29
  • @Jarod42 What approach do you suggest for erasing the elements of a set from another set? – Soroush Vahidi May 24 '22 at 17:30
  • @Jarod42 greed is an integer number. The 2 others are defined in the code. sg2 is the name of the function, and y is an integer set. – Soroush Vahidi May 24 '22 at 17:32
  • There is no function `sg2` in your code. And `sg2[l]` makes no sense for a function. One more: what is `alt`. Don't show just parts of the code. – Goswin von Brederlow May 24 '22 at 17:37
  • @GoswinvonBrederlow So sorry. sg2 is defined for saving the answer for a specific input, and the next time we need the answer for that input, we do not need to calculate that again. It is called memoization. Here is the whole code: https://github.com/SoroushVahidi/Scheduling-problem/blob/main/withset.cpp – Soroush Vahidi May 24 '22 at 17:54
  • Not sure if there's anything better than `for(auto element : rr) { jav.erase(element); }` or `jav.erase_if([&rr](int element) { return rr.count(element) != 0; });` The later may yield a slightly better performance... – fabian May 24 '22 at 17:55
  • @SoroushVahidi Why do you believe that recursion is a good way to solve this problem? I see too many times where it would be far simpler to write a simple loop. That way, you are not potentially exhausting the stack, and probably would be easier to solve. I also see too many times, new programmers who visit those "online competitive coding" sites simply think that recursion "looks cool", but are unaware of the consequences. – PaulMcKenzie May 24 '22 at 18:04
  • @SoroushVahidi *Do you know what is the reason and how can I solve it?* -- Assuming you wrote the code, the person who could better answer that is yourself. You do this by [debugging](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) your code and seeing where the program goes against the plan you had. If you did that you will conclude 1) The program does not follow the plan, so you will alter/fix the program to follow the plan, or 2) Your original plan was flawed, so you need to change the plan and change the program accordingly. – PaulMcKenzie May 24 '22 at 18:09
  • @fabian It is strange. It does not crash anymore after I did what you said. Thank you! – Soroush Vahidi May 24 '22 at 18:13
  • So what is the output for 0, 1, 2, ...? When does it crash or when does the output diverge from the expected output? What is the expected output of this? You have this big ball of spaghetti code with not explanation of what it's supposed to do. – Goswin von Brederlow May 24 '22 at 18:42
  • @PaulMcKenzie I knew which line is not working, and I have also written it in my question. My question was why this does not work. The point that fabian said, solved the problem, but still, I do not know why his solution worked. – Soroush Vahidi May 24 '22 at 21:55
  • @PaulMcKenzie It is part of a paper. I want to implement it exactly equal to the paper, and the paper has defined it recursive, so I also defined it recursively. – Soroush Vahidi May 24 '22 at 21:57
  • @SoroushVahidi -- *and the paper has defined it recursive, so I also defined it recursively* -- What you see on paper may not be well-suited for a computing, stack-based machine. Formulas on paper do not mean you must implement what you see on the paper verbatim. Not only does this go for recursion, but also for schoolbook math formulas. Recursion is no different in this regard -- if you know that the routine will call itself hundreds of times, then that recursive solution looks good on paper, but impractical and maybe impossible to implement "by the book". – PaulMcKenzie May 24 '22 at 22:01

0 Answers0