我的程序在哪里越界?我最后得到 "Process returned -1073740940 (0xC0000374)"
Where is my program going out of bounds? I get "Process returned -1073740940 (0xC0000374)" at the end
我真的看不出有什么问题,但话又说回来,我几周前才开始学习 C,作为一种获得比我以前更快的代码的方法 using.My 我猜这是必须要做的我的内存分配。这很小,但最终我将使用计数值高达 25 的这个过程。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int i;
int j;
int Count = 2; /* set number of bits */
int Combos = pow(2,Count); /* calculate count of all 6 bit binary numbers */
int SIZE = (Combos + 1) * (Count + 1); /* calculate number of array elements */
/* rows * columns */
/* rows = Combos + 1 */
/* columns = count +1 (row number + bits)*/
/* 0th spot will hold row number */
printf("SIZE = %d\n", SIZE); /* print number of array elements */
int (*a)[Count + 1] = malloc(SIZE); /* allocate memory for array based on size of */
/* int variable times SIZE */
/* (SIZE) is number of elements */
if (a == NULL) /* if not enough memory, print error message */
{
fprintf(stderr,"Could not allocate that much memory");
return 1;
}
/* do something with array */
for (i =0; i<= Combos; i++){
a[i][0] = i; /* set 0th element of this row to row number */
printf("a[%d][0] = %d ", i,a[i][0]); /* print 0th element of this row */
for (j =1; j<= Count; j++){ /* print the rest of the elements in this row */
a[i][j] = 1;
printf("a[%d][%d] = %d ", i,j,a[i][j]);
} /* end j loop */
printf("\n"); /* line feed */
} /* end i loop */
free(a); /* release memory allocated for array */
return 0;
}
请注意,malloc(K)
分配了 K
字节的内存。
如果你想分配一个由 N
个类型为 T
的元素组成的数组,你需要调用 malloc(N * sizeof(T))
.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j;
const size_t COUNT = 30;
const size_t COMBOS = (size_t)1 << COUNT;
const size_t SIZE = (COMBOS + 1) * (COUNT + 1);
printf("SIZE = %zu\n", SIZE);
int (*a)[COUNT + 1] = malloc(SIZE * sizeof(int));
if (a == NULL)
{
fprintf(stderr, "Could not allocate that much memory\n");
return 1;
}
for (i = 0; i <= COMBOS; i++)
{
a[i][0] = i;
printf("a[%d][0] = %d%s", i, a[i][0], " ");
for (j = 1; j <= COUNT; j++)
{
a[i][j] = 1;
printf("a[%d][%d] = %d%s", i, j, a[i][j], (j == COUNT ? "\n" : " "));
}
}
free(a);
return 0;
}
我真的看不出有什么问题,但话又说回来,我几周前才开始学习 C,作为一种获得比我以前更快的代码的方法 using.My 我猜这是必须要做的我的内存分配。这很小,但最终我将使用计数值高达 25 的这个过程。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int i;
int j;
int Count = 2; /* set number of bits */
int Combos = pow(2,Count); /* calculate count of all 6 bit binary numbers */
int SIZE = (Combos + 1) * (Count + 1); /* calculate number of array elements */
/* rows * columns */
/* rows = Combos + 1 */
/* columns = count +1 (row number + bits)*/
/* 0th spot will hold row number */
printf("SIZE = %d\n", SIZE); /* print number of array elements */
int (*a)[Count + 1] = malloc(SIZE); /* allocate memory for array based on size of */
/* int variable times SIZE */
/* (SIZE) is number of elements */
if (a == NULL) /* if not enough memory, print error message */
{
fprintf(stderr,"Could not allocate that much memory");
return 1;
}
/* do something with array */
for (i =0; i<= Combos; i++){
a[i][0] = i; /* set 0th element of this row to row number */
printf("a[%d][0] = %d ", i,a[i][0]); /* print 0th element of this row */
for (j =1; j<= Count; j++){ /* print the rest of the elements in this row */
a[i][j] = 1;
printf("a[%d][%d] = %d ", i,j,a[i][j]);
} /* end j loop */
printf("\n"); /* line feed */
} /* end i loop */
free(a); /* release memory allocated for array */
return 0;
}
请注意,malloc(K)
分配了 K
字节的内存。
如果你想分配一个由 N
个类型为 T
的元素组成的数组,你需要调用 malloc(N * sizeof(T))
.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j;
const size_t COUNT = 30;
const size_t COMBOS = (size_t)1 << COUNT;
const size_t SIZE = (COMBOS + 1) * (COUNT + 1);
printf("SIZE = %zu\n", SIZE);
int (*a)[COUNT + 1] = malloc(SIZE * sizeof(int));
if (a == NULL)
{
fprintf(stderr, "Could not allocate that much memory\n");
return 1;
}
for (i = 0; i <= COMBOS; i++)
{
a[i][0] = i;
printf("a[%d][0] = %d%s", i, a[i][0], " ");
for (j = 1; j <= COUNT; j++)
{
a[i][j] = 1;
printf("a[%d][%d] = %d%s", i, j, a[i][j], (j == COUNT ? "\n" : " "));
}
}
free(a);
return 0;
}