首页 > 解决方案 > int64_t 在传递给函数时更改值

问题描述

我从命令行读取了一个大值(300000000000)并将其传递给函数 RR。正确的值打印在 main 中,但不正确的值打印在 RR 中。据我了解, main 中打印的值是 64 位值,但 RR 中打印的值是 32 位 int 版本。然而,这并没有真正意义,因为我将 int64_t 作为参数传递,而 RR 接收 int64_t 作为参数。

#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);
  }


}

预期产出

timeslice: 300000000000
timeslice: 300000000000

实际输出:

timeslice: 300000000000
timeslice: -647710720

如何运行:

./a.out [.txt file] 300000000000

标签: c++command-line

解决方案


推荐阅读