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

标签 c++ parameter-passing pass-by-reference undefined-behavior

我遇到了一个函数问题:

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] == '\0')
    return -1;

//Parsea tiempo
t = atol(linea+i);
cout << "---" << t << endl;

while (!ispunct(linea[i])) i++;

//Parsea restricciones
while (linea[i] != '\0' && linea[i] != ' '){
    restric[j] = linea[i];
    i++; j++;
}
restric[j]='\0';

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 '\0' 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]!='\0'){
                    cout << restricciones[j];j++;}
                cout << endl;

                //-- agregar datos al grafo/mapa
            }

        }

        // Resolver usando algun algoritmo
        //--- resolver(MAPA)

    }

}


return 0;
}

CFLAGS。 -Wall -pipe -g -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 中测试了该程序(使用 Code::Blocks,默认设置)并且它有效:/

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

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

最佳答案

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

想吃多少就吃多少!

关于c++ - 我可以在 C++ 中通过引用传递多少个参数,而不会出现异常行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6561245/

相关文章:

c++ - 函数调用中的堆栈分配

C++ 我应该如何解释函数参数 long(*pPointer)(OtherClass *const, long)?

c++ - 通过引用返回函数调用

c++ - 为什么 std::map::operator[] 赋值需要无参数构造函数?

C++:访问作为函数参数传递的双指针数组指向的变量值

php - 我应该如何在 PHP 的多个域之间安全地传输变量/参数?

Java - 如果可能的话,传递引用或使用硬编码对象是否更好

javascript - AJAX addEventListener - 将参数传递给另一个函数

amazon-web-services - 在 AWS Cloudformation 的 EC2 UserData 中使用 Parameter Store 参数

java - 为什么对字符串的引用与其他对象引用的行为不同?