calloc给longlongint类型的数组分配内存会报警告,数组过长会出错。似乎是参数长度受限。
警告如下:
warning C4244: “参数”: 从“__int64”转换到“size_t”,可能丢失数据
完整代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
int main(){
long long int *A, *g, r;
long long int sub = 0;
long long int n, N, *f, lg, rg, lf, rf;
long long int length = 0;
scanf("%lld %lld", &n, &N);
r = N / (n + 1);
A = (long long int *)calloc(n + 2, sizeof(long long int));//报警告
A[0] = 0;
A[n + 1] = N;
f = (long long int *)calloc(N, sizeof(long long int));//报警告
g = (long long int *)calloc(N, sizeof(long long int));//报警告
for (int i = 1; i < (n + 1); i++){
scanf("%lld", &A[i]);
if (A[i] < N)f[A[i]] = i;
}
for (int i = 1; i < N; i++){//完成g[]和f[]
g[i] = i / r;
if (f[i] == 0)
f[i] = f[i - 1];
else continue;
}
/* for (int i = 0; i < N; i++){
sub += abs(g[i] - f[i]);
}*/
for (long long int j = 0; j < n + 1; j++){
for (long long int i = A[j]; i < A[j + 1]; i += length){
lg = i;
lf = i;
rf = A[j + 1] - 1;
rg = i + r - 1 - i%r;
length = (rf>rg ? rg : rf) - i + 1;
sub += length*(abs(f[i] - g[i]));
}
}
printf("%lld", sub);
system("pause");
return 0;
}
是否有方法给longlongint长度的数组动态分配内存,求解答