我的程序哪里出界了?我收到“进程返回 -1073740940 (0xC0000374)”在最后
我真的看不出有什么问题,但话又说回来,我几周前才开始学习 C,作为一种获得比我正在使用的代码更快的代码的方法。我的猜测是这与我的内存分配有关。这很小,但最终我将使用这个过程,计数值高达 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;
}
I really can't see an issue, but then again, I just started learning C a few weeks ago, as a way to get faster code than what I was using.My guess is it has to do with my memory allocation. This is small, but eventually I will be using this process with Count values up to 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,
malloc(K)
分配K
字节的内存。如果您想分配一个由
T
类型的N
个元素组成的数组,则需要调用malloc(N * sizeof(T))
。Note that
malloc(K)
allocatesK
bytes of memory.If you'd like to allocate an array consisting of
N
elements of typeT
, you need to callmalloc(N * sizeof(T))
.