Error de tiempo de ejecución: archivo ilegal abierto (/dev/tty)

I've recently registered at USACO on-line training program and i'm currently on Problem 2. I've finished to code it and everything compile correctly. But when i submit the code, i keep getting the following error:

Run 1: Execution error: Your program had this runtime error: Illegal file open (/dev/tty). The program ran for 0.011 CPU seconds before the error. It used 3348 KB of memory.

I've tried to look for the problem in vain. However, i think that's a memory management problem as i used vectors of pointers. In USACO faq, they say:

Linux, on which our grading system runs, is much more picky on memory access than Windows. Thus, invalid array indexes and bad pointer dereferences which are allowed on Windows may cause your program to crash under Linux. Alternately, they will corrupt an internal data structure and give the most cryptic error messages like "Can't open /dev/[mumble]".

Aquí está el código:

ID: freebie1
PROG: gift1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;

class Person {
    Person():m_volatile(0) {}

    void setName(string name) { m_name = name; }
    string getName() { return m_name; }
    void setMoney(int money){ m_sMoney = money; }
    int getMoney() { return m_volatile; }
    void receive(int money) { m_volatile += money; }
    void giveGifts(vector<Person*> fTab){
        int splitMoney = m_sMoney/fTab.size();
        for(vector<Person*>::iterator it=fTab.begin();it!=fTab.end();it++) {

    string m_name;
    int m_sMoney; // starting money
    int m_volatile;


class FindFriend {
    FindFriend(string query):m_query(query){}
    bool operator()(Person &individu){
        if(individu.getName()==m_query) { return true; }
        else { return false; }
    string m_query;

vector<Person*> setFriends(vector<string> &namesTab,vector<Person> &personsTab){
    vector<Person*> tab;
    for(vector<string>::iterator it=namesTab.begin();it!=namesTab.end();it++) {
        FindFriend f(*it);
        vector<Person>::iterator trouve=find_if(personsTab.begin(),personsTab.end(),f);
    return tab;

int main() {
    ofstream fout("gift1.out");
    ifstream fin("");

    if(fin) {
        int np(0);
        fin>>np; // Number of persons

        // Each one is assigned a name...
        vector<Person> personsTab(np);
        for(vector<Person>::iterator it=personsTab.begin();it!=personsTab.end();it++){
            string namePerson;
        // and the friends we'll give the money to
            string name;
            int money(0),nFriends(0);
            FindFriend g(name);
            vector<Person>::iterator trouve=find_if(personsTab.begin(),personsTab.end(),g);
            trouve->setMoney(money); // Somme de depart
            // Amis
            if(nFriends!=0 || money!=0) {
                vector<string> friendsTab;
                for(int i(0);i<nFriends;i++) {
                    string chaine;
                // We create a vector of pointers to his friends
                vector<Person*> pFriends(nFriends);
                trouve->giveGifts(pFriends); // Each person share the money among his friends

        // We output the net loss/profit for each one
        for(vector<Person>::iterator it=personsTab.begin();it!=personsTab.end();it++) {
            string name=it->getName();
            int money=it->getMoney();
            fout<<name<<" "<<money<<endl;
    return 0;

And here is the input file

200 3
500 1
150 2
0 2
0 0

preguntado el 29 de julio de 12 a las 11:07

Need to see the content of to be sure. I think the most likely error is not the vector of pointers but that when you do find_if you never check to see if you have found what you're looking for. -

2 Respuestas

The problem is that you are not testing for end of file correctly. Using fin.eof() está mal porque eof() only becomes true after you have tried to read and failed, it does no predict whether the next read will fail. It's unbelievable how many beginners get this wrong.

Here's a better way to do it

   string name;
   while(fin >> name){
        int money(0),nFriends(0);

The result of your error is that you go round the while loop one more time than you should. On the last time round the loop name does not contain a valid value and your call to find_if fails and the program crashes shortly afterward.

Respondido 29 Jul 12, 14:07

I am throwing a guess here, but looking for that file description online I found out that /dev/tty is a special file used on UNIX to send text to the controlling terminal window of your process (see: What is special about /dev/tty?).

Odds are the submission mechanism is a service and does not have a parent terminal window to output text to, so there must be something that is trying to output to standard out that causes this issue.

There is most likely an error somewhere that is being hidden by this as it tries to send an error to the screen. If you are working on a windows machine I suggest getting a VMware player (free) and build a Linux VM (it should be really easy just download a free distribution and point VMware player to it), and compile and run your program in that environment so you can see how it behaves. This way you can see what it tried to tell you and work from there.

Bonne chance.

PS: I have a Linux machine and will try it myself in a little while, but you should go through the exercise of making a Linux VM for the other problems as you will most likely run into this issue again.

Ran your program as is on Linux and the output file had:

dave 302
laura 66
owen -359
vick 141
amr -150

It threw this exception:

*** glibc detected *** ./test.out: free(): invalid pointer: 0x0000000001b43510 ***
======= Backtrace: =========

Using gdb to step line by line, the exception happens as "personsTab" variable goes out of scope and gets destroyed.

contestado el 23 de mayo de 17 a las 11:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.