Home > Net >  Run-Time Check Failure #2 - Stack around the variable 'array' was corrupted
Run-Time Check Failure #2 - Stack around the variable 'array' was corrupted

Time:09-17

I have this code here which is working but when it gets to the very end it throws a run time check and I'm not sure why, also trying to free the memory at the end will result in a different exception "Access violation reading location 0x00000024" and I can't figure out what the problem is

# include<stdio.h>
# include <stdlib.h>

int createTheArray(int *array) {
    int y;
    printf("Enter the number of random numbers you wish to create and store: ");
    scanf_s("%i", &y);
    array = (int *)calloc(y,sizeof(int));
    if (array == NULL)
        printf("Out of memory");
    else {
        printf("Memory allocated \n");
        
        return y;
    }
}

void populateArray(int* array, int y) {
    int x;
    for (x = 0; x < y; x  ) {
        array[x] = rand() % 100;
    }
    for (x = 0; x < y; x  ) {
        printf("array[%i]: %i\n", x ,array[x]);
    }
}

void operateArray(int* array, int y) {
    int i,sum=0,high=0,low=array[0];
    float average;
    for (i = 0; i < y; i  ) {
        sum = sum   array[i];
        if (array[i] > high)
            high = array[i];
        if (array[i] < low)
            low = array[i];
    }
    average = (float) sum / y;
    printf("\n");
    printf("Sum: %i \n", sum);
    printf("high: %i \n", high);
    printf("low: %i \n", low);
    printf("Average: %.1f \n", average);

}
main() {
    int* array = (int *)calloc(1,sizeof(int));
    int y;
    y = createTheArray(array);
    populateArray(array,y);
    operateArray(array, y);
    free(array);
    
    return 0;
}

CodePudding user response:

Here is the design I'm talking about. This avoids needing to have a function return two things. In general, a function should have one purpose ("create the array") and not have the additional hidden function ("ask the user for the size"). Here's what I mean:

#include <stdio.h>
#include <stdlib.h>

int * createTheArray(int size) {
    int *x = (int*)calloc(size, sizeof(int));
    if (x == NULL)
        printf("Out of memory");
    else
        printf("Memory allocated \n");

    return x;
}

void populateArray(int *array, int y) {
    int x,k;
    for (x = 0; x < y; x  ) {
        array[x] = rand() % 100;
    }
    for (k = 0; k < y; k  ) {
        printf("array[%i]: %i\n", k, array[k]);
    }
}

void operateArray(int* array, int y) {
    int i,sum=0,high=0,low=array[0];
    float average;
    for (i = 0; i < y; i  ) {
        sum = sum   array[i];
        if (array[i] > high)
            high = array[i];
        if (array[i] < low)
            low = array[i];
    }
    average = (float) sum / y;
    printf("Sum: %i \n", sum);
    printf("high: %i \n", high);
    printf("low: %i \n", low);
    printf("Average: %.1f \n", average);

}
main() {
    int *array;
    int y;

    printf("Enter the number of random numbers you wish to create and store: ");
    scanf("%i", &y);
    array = createTheArray(y);
    populateArray(array,y);
    operateArray(array, y);
    free(array);
    return 0;
}
  • Related