I read a large value(300000000000) from command line and pass it to the function RR. The correct value is printed in main but incorrect value is printed in RR. From what I understand, the value printed in main is the 64 bit value but the value printed in RR is a 32bit int version. However that does not really make sense since I pass a int64_t as an argument and RR receives int64_t as an argument.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<fstream>
#include<vector>
using namespace std;
struct Process
{
int64_t id, arrivalTime, burstTime;
};
void RR(vector<Process> &vec, int64_t timeSlice, int64_t optimal_step){
printf("timeslice: %d\n", timeSlice);
exit(0);
}
vector<Process> parseInputFile(string filename, vector<Process> &vec){
ifstream inFile;
int64_t arrival;
int64_t burst;
inFile.open(filename);
if (!inFile) {
cerr << "Unable to open file ";
cout<<filename<<endl;
exit(1); // call system to stop
}
for ( int64_t i = 0; i>=0; i++) {
inFile >> arrival;
inFile >> burst;
if(!inFile){
inFile.close();
return vec;
}
vec.push_back({i, arrival, burst});
}
return vec;
}
int main(int argc, char * const argv[]){
string filename = argv[1];
int64_t timeSlice = -1;
vector<Process> vec;
int64_t optimal_step = 1;
vec = parseInputFile(filename, vec);
if(argc == 3){
timeSlice = strtoll(argv[2], nullptr, 10);
cout<<"timeslice: "<<timeSlice<<endl;
RR(vec, timeSlice, optimal_step);
}
}
Expected output
timeslice: 300000000000
timeslice: 300000000000
Actual output:
timeslice: 300000000000
timeslice: -647710720
How to run:
./a.out [.txt file] 300000000000