Home > OS >  Where is my program going out of bounds? I get "Process returned -1073740940 (0xC0000374)"
Where is my program going out of bounds? I get "Process returned -1073740940 (0xC0000374)"

Time:03-06

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;
}

CodePudding user response:

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;
}

  • Related