在C++中,我可以通过引用传递多少个参数,而不会出现异常行为

How many parameters can I pass by reference in C++, without getting abnormal behavior

本文关键字:异常 参数 多少 C++ 可以通过 引用      更新时间:2023-10-16

我遇到了一个函数问题:

int parsearRestricciones(char linea[], unsigned int& x, unsigned int& y, unsigned int& tiempo, char restric[])

在这个函数中,我解析linea[]。

输入包含:三个无符号整数和一个标点符号字符串。我需要这样读。当我将atoi(linea+offset(分配给变量tiempo时,问题就出现了。在函数外部(即main((中(,tiempo的值与其内部的值不同。我只在tiempo中遇到了问题(我用指向结构的指针替换了x、y和tiempo。它有效(

可能是什么问题?

谢谢你的帮助。

-----再次编辑

完整代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <cassert>
#define MAX_RESTRIC 3  // Tres sentidos. Si hay 4, se usa '+'
#define MAX_LINEA 80
#define entrada cin

using namespace std;
int parsearRestricciones(char *linea, unsigned int& x, unsigned int& y, unsigned int& t, char *restric) {
// ! solo funciona si x,y,t,restric estan en una misma linea (i.e. no hay CR LF)
int i=0, j=0;
//Parsea x de la casilla
x = atol(linea+i);//strtol(linea,(char**)NULL,10);
while (isdigit(linea[i])) i++;
while (isspace(linea[i])) i++;
//Parsea y de la casilla
y = atol(linea+i);
while (isdigit(linea[i])) i++;
while (isspace(linea[i])) i++;
if(linea[i] == '')
    return -1;
//Parsea tiempo
t = atol(linea+i);
cout << "---" << t << endl;
while (!ispunct(linea[i])) i++;
//Parsea restricciones
while (linea[i] != '' && linea[i] != ' '){
    restric[j] = linea[i];
    i++; j++;
}
restric[j]='';
return 1;
}

int main (int argc, char** argv)
{
// Sugerencia de argumentos
// --d  (por Dijkstra)
// --axe  (por A*, distancia euclideana)
// --axm  (por A*, distancia manhattan)
// y dos màs que veremos luego :)
unsigned int X, Y;
unsigned int xi, yi;
unsigned int xf, yf;
unsigned int x,y,tiempo;
char restricciones[MAX_RESTRIC + 1];

//Buffer para parsear las lineas con restricciones
char linea[MAX_LINEA + 1];
bool finCasos = false;
bool siguienteCaso = false;

while (!finCasos)
{
    if (siguienteCaso){
        // Se leyó otro mapa antes que éste (hubo parseo, y quedo en xcasilla,ycasilla)
        X = x;
        Y = y;
        siguienteCaso = false;
    } else {
        // Sino, lee por primera vez las dimensiones del mapa
        entrada >> X >> Y;
    }
    if ( X == 0  &&  Y == 0 )
        finCasos = true;
    else {
        entrada >> xi >> yi;
        entrada >> xf >> yf;
        // Lee restricciones hasta que encuentra una linea sin ellas (sin tiempo ni direccion)
        // se asumira, que corresponde a las dimensiones del siguiente caso, y los usará en la siguiente
        // iteracion
        while(!siguienteCaso) {
            cin.get(); //lee un '' que quedó (?)
            cin.getline(linea, MAX_LINEA+1);
            if ( parsearRestricciones(linea,x,y,tiempo, restricciones) == -1 ) {
                siguienteCaso = true;
            } else {
                cout << "X = " << x << endl;
                cout << "Y = " << y << endl;
                cout << "tiempo = " << tiempo << endl;
                cout << "restric = " << restricciones << endl;
                int j=0;
                cout << "restric = " ;
                while(restricciones[j]!=''){
                    cout << restricciones[j];j++;}
                cout << endl;
                //-- agregar datos al grafo/mapa
            }
        }
        // Resolver usando algun algoritmo
        //--- resolver(MAPA)
    }
}

return 0;
}

CFLAGS-墙-管道-ggdb-DONLINE_JUDGE-DNDEBUG(Makefile还为uvaonlinejudge构建了另一个源代码(

输入:

101
10
1
1
2
2
1000 10000  100000 +++++

输出:

---100000
X = 1000
Y = 10000
tiempo = 65579
restric = +++++
restric = +++++
^X^C (I did break)

我刚刚在Windows中测试了这个程序(使用代码::块,默认设置(,它工作了:/

顺便说一句,我在Virtualbox 中使用Ubuntu

你能告诉我我做错了什么吗?

在C++中,我可以通过引用传递多少个参数,而不会出现异常行为

想要多少就有多少!

没有这样的限制。然而,您的函数并没有以任何方式使用tiempo,而是使用了一些名为t的变量:t = atol(linea+i);