我的程序哪里出界了?我收到“进程返回 -1073740940 (0xC0000374)”在最后

发布于 2025-01-11 23:06:00 字数 2353 浏览 2 评论 0原文

我真的看不出有什么问题,但话又说回来,我几周前才开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2025-01-18 23:06:00

请注意,malloc(K) 分配 K 字节的内存。
如果您想分配一个由 T 类型的 N 个元素组成的数组,则需要调用 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;
}

Note that malloc(K) allocates K bytes of memory.
If you'd like to allocate an array consisting of N elements of type T, you need to call 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;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文