How many parameters can I pass by reference in C++, without getting abnormal behavior
I had a problem with a function:
int parsearRestricciones(char linea[], unsigned int& x, unsigned int& y, unsigned int& tiempo, char restric[])
Inside that function I parse linea[].
The input consists in: three unsigned integers, and a string of punctuation characters. I need to read them that way. The problem ocurrs when I assign atoi(linea+offset) to variable tiempo. Outside the function (i.e., in main() ), the value of tiempo is not the same that it's inside. I had the problem only with tiempo (I replaced x,y and tiempo by a pointer to struct. It works)
What could be the problem?
Thanks for your help.
-----edit-again
The full code:
#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 (The Makefile also builds another source, for uvaonlinejudge)
Input:
101
10
1
1
2
2
1000 10000 100000 +++++
Output:
---100000
X = 1000
Y = 10000
tiempo = 65579
restric = +++++
restric = +++++
^X^C (I did break)
I just tested the program in Windows (using Code::Blocks, default settings) and it worked :/
By the way, I'm using Ubuntu in Virtualbox
Could you tell me what I'm doing wrong?
How many parameters can I pass by reference in C++, without getting abnormal behavior?
As many as you want to!
There is no such limit. However, your function does not use tiempo in any way, it uses some variable called t instead: t = atol(linea+i);
精彩评论