Getting runtime error

Hey guys, I wrote this program which compiles and runs properly on my computer. However when others run/compile it, they get a runtime error. Since I get no errors, I cannot see what is causing the runtime error so I cannot fix it. Here is the link http://ideone.com/fX5IkG

Someone please help

E: Can someone explain what runtime error is
Last edited on
A run time error is am error that occurs at the time you run the program.

Its most likely caused by writing to or reading from memory that you don't control. Consider checking your memory creation.

You could also start outputting markers in your code to determine where it stopped.
 
std::cout<<"creating Dorm object<<"<<std::endl;


this helps in tracking where the code broke.
Last edited on
E: Can someone explain what runtime error is


My guess is that you didn't supply input with your code, and since it requires input, it just hung until it was killed.
i tried to debug this but got annoyed with predicting what to enter in the console :)
This is for codeforces problem 254 E, http://www.codeforces.com/contest/254/problem/E

The first page of the problem gives some input.

My guess is that you didn't supply input with your code, and since it requires input, it just hung until it was killed.

But the problem is that I'm not the one supplying input, I submit it and it is the website that gives it input and tests it. It works fine for me, but when I submit, they just spit back runtime error; so that's just confusing

You could also start outputting markers in your code to determine where it stopped.

It doesn't stop for me, just when I submit it hence my dilema. I don't want to go asking on the website because frankly no one there cares enough, all they do it down-vote your post...it's nonsense
Last edited on
First, what's with the pointers and pointers-to-pointers where they aren't needed? Use container classes.

1
2
        action[c] = new (nothrow)int[n+1];
        memset(action[c], 0, sizeof action[c]);


I don't think this is doing what you think it's doing.

1
2
        action[c] = new int[n+1] ;  // Don't use nothrow if you aren't going to check the return value.
        memset(action[c], 0, sizeof(action[c][0]) * (n+1)) ;


That likely resulted in for (int i = 0; action[p][i] >= 0; ++i) exceeding the bounds of the array. It may still exceed it, of course, I haven't spent much time examining your code.

Doesn't:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
typedef std::vector<int> i_vec ;
typedef std::vector<std::vector<int>> iv_vec ;

struct Dorm
{
   int foodNeed, numDays, Pop, p, i;

   i_vec food_per_day ;
   iv_vec action ;


   FI(frend) it;
public:
   bool operator() (frend, frend);
   
   Dorm (int n, int f)
   :foodNeed(f), numDays(n), food_per_day(n), Pop(0), action(n)
   {
       for ( iv_vec::iterator it = action.begin(); it !=action.end(); ++it )
           it->resize(n+1) ;

       for ( unsigned i=0; i<n ; ++i )
       {
           scanf( "%d", &food_per_day[i] ) ;
           food_per_day[i] -= foodNeed ;
       }
   }
    
   vector <frend> Berland;
   
   void Print();
   void Get();
   
};


look a little less messy? Btw, i, p, and operator() have no business being members of Dorm.
Last edited on
Thanks cire, now I just have to eliminate the new bugs and hopefully I don't get runtime error

E: you were right. It was hanging waiting for input, apparently I had to do something like this:

1
2
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);


To get it to read input...wierd. Even fscanf(stdin...) didn't work
Last edited on
Topic archived. No new replies allowed.