c++ - 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
解决方案
推荐阅读
- java - 如何从 GridPane 中删除 TextField?
- java - 将 Main 中发生的另一个类文件中的初始化数组对象添加到堆栈中
- python - 使用 uWSGI 部署 Flask 应用程序时“未找到 python 应用程序,请检查您的启动日志是否有错误”
- svn - Subversion 在同一个 URL 显示两个不同的存储库
- django - Django 高级查询 - 外键的对象?
- php - 如何通过复选框菜单从服务器显示标记文件?
- python - 如何使用 pip 安装?
- json - 使用 Node.js/Express/xml2js解析具有多个标签的 XML 文件
- javascript - 带有 socket.io 和 express 的游戏大厅
- reactjs - react-static /edit/:id /nested 路由在刷新/热加载时丢失对资产的引用