C++ 检查数组处的索引后程序崩溃
请注意:我不确定这是否适合这里,如果不适合,请移动到适当的论坛 所以我有一个程序试图解决旅行商问题,简称TSP 在我尝试使用33810个城市之前,我的代码似乎运行得很好。在33810个城市中,程序在尝试访问职位成本[69378120]后崩溃,它只是停止响应,并在不久后结束 我正在尝试以下代码:C++ 检查数组处的索引后程序崩溃,c++,windows,memory,C++,Windows,Memory,请注意:我不确定这是否适合这里,如果不适合,请移动到适当的论坛 所以我有一个程序试图解决旅行商问题,简称TSP 在我尝试使用33810个城市之前,我的代码似乎运行得很好。在33810个城市中,程序在尝试访问职位成本[69378120]后崩溃,它只是停止响应,并在不久后结束 我正在尝试以下代码: #include <iostream> #include <stdlib.h> #include <malloc.h> #include <fst
#include <iostream>
#include <stdlib.h>
#include <malloc.h>
#include <fstream>
#include <math.h>
#include <vector>
#include <limits>
using namespace std;
typedef long long int itype;
int main(int argc, char *argv[]) {
itype n;
ifstream fenter;
fenter.open(argv[1]);
ofstream fexit;
fexit.open(argv[2]);
fenter>> n;
double *x;
double *y;
x = (double*) malloc(sizeof(double)*n);
y = (double*) malloc(sizeof(double)*n);
cout<<"N : "<<n<<endl;
for (int p = 0; p < n; p++) {
fenter>> x[p] >> y[p];
}
fenter.close();
int *costs;
costs = (int*) malloc(sizeof(int)*(n*n));
for (int u = 0; u < n; u++) {
for (int v = u+1; v < n; v++) {
itype cost = floor(sqrt(pow(x[u] - x[v], 2) + pow(y[u] - y[v], 2)));
cout<<"U: "<<u<<" V: "<<v<<" COST: "<<cost<<endl;
costs[u*n + v] = cost;
cout<<"POS (u*n + v): "<<(u*n + v)<<endl;
cout<<"POS (v*n + u): "<<(v*n + u)<<endl;
costs[v*n + u] = cost;
}
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
typedef long long int类型;
int main(int argc,char*argv[]){
n型;
iffenter;
芬特开放(argv[1]);
流态铁矾土;
fexit.open(argv[2]);
芬特>>n;
双*x;
双*y;
x=(双*)malloc(sizeof(双)*n);
y=(双*)malloc(sizeof(双)*n);
cout如果以32位大小编译\u t为32位,则
1143116100*4
大于最大的32位数字,因此int溢出
64位编译
size_t siz = 1143116100;
std::vector<long long> big(siz);
std::cout << big.size() << ", " << big.max_size() << std::endl;
如果我把它改成
size_t siz = 1024*1143116100;
由于我的交换盘不够大,所以我得到了一个错误的分配。评论不适合进一步讨论;这段对话已经结束。这确实是x32和x64的问题所在,我改为Visual Studio Compliated for x64,这似乎解决了问题,感谢上面所指出的一切。
1143116100, 2305843009213693951
size_t siz = 1024*1143116100;