Jump to content

Hello there, I am quite new to coding, basically just started in C a few weeks ago and I was tasked with filling a 40 slots array with random numbers then allow the user to select a slot and view whats in it. The program is doing what I'm asking of it but when its done it crashes and says there was a corrupted stack. I guess its coming from the loop filling the array but I can't really pinpoint it.

 



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

int nombre_hazard(int iMin, int iMax)
{
    return (iMin + (rand () % (iMax - iMin+1)));
}

void main()
{
    srand (time (NULL)); 
    
    int i, iChoix, iMin = 1, iMax = 10;
    int tiTableau_1[40];

    for (i = 0; i <=40; i++)
    {
        tiTableau_1[i] = nombre_hazard(iMin,iMax);
    }

    printf("Which slot would you like to see? ");
    scanf("%i", &iChoix);
    printf("This slot contains the number: %i\n", tiTableau_1[iChoix]);
    
    system("pause");
}

[code]

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/741283-corrupted-stack-help/
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

for (i = 0; i <=40; i++)

An array with size 40 starting at index 0 means the max index is 39. By going up to 40, you're overwriting some other value on the stack leading to it being corrupted.

right so i < 40

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/741283-corrupted-stack-help/#findComment-9397162
Share on other sites

Link to post
Share on other sites

1 hour ago, fizzlesticks said:

for (i = 0; i <=40; i++)

An array with size 40 starting at index 0 means the max index is 39. By going up to 40, you're overwriting some other value on the stack leading to it being corrupted.

What about calling the value of 2D array slot (that the user selects)? Im getting a windows unauthorized storage location error. Its the same program, just with a 2D array.

 



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

int nombre_hazard(int iMin, int iMax)
{
    return (iMin + (rand () % (iMax - iMin+1)));
}

void main()
{
    srand (time (NULL));
    int i, j, iRow = 0, iColumn = 0, iMin = 1, iMax = 40;
    int tiTableau_1[10][10];

    

    for (i = 0; i < 10; i++)
    {
        for (j = 0; j < 10; i++)
        {
            tiTableau_1[i][j] = nombre_hazard(iMin,iMax);
        }
    }
    
    printf("Please enter desired row and column: ");
    scanf("%i %i", &iRow, &iColumn);
    printf("The selected slot value is: %i\n", tiTableau_1[iRow][iColumn]);

    system("pause");

}

[code]

 

 

 

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/741283-corrupted-stack-help/#findComment-9397551
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

Your second loop has a typo.

facepalm.jpg~c200

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/741283-corrupted-stack-help/#findComment-9397590
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×