简体   繁体   中英

Stack smashing error when trying to work with dynamic arrays

I'm trying to create 2 dynamic arrays from the user input but the compiler is throwing a stack smashing array. This is my code:

#include <stdio.h>

#define _CRT_SECURE_NO_WARNINGS
#define NUMS 3

// Put your code below:

int main() {
    int high, low;
    int high_temp[3];
    int low_temp[3];
    int total_temp = 0;
    double median = 0;
    printf("---=== IPC Temperature Analyzer ===---\n");

    for (int i = 1; i <= NUMS; i++) {
        printf("Enter the high value for day %d: ", i);
        scanf("%d/n", &high);

        printf("Enter the low value for day %d: ", i);
        scanf("%d/n", &low);

        if(!((high > low && high < 41) && (low < high && low > -41))) {
            printf("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low");
            i = i - 1;
        }else{
            high_temp[i] = i;
            low_temp[i] = i;
        }
    }

    for (int i = 0; i < sizeof(high_temp); i++){
        //total_temp = total_temp + high_temp[i] + low_temp[i];

        printf("The high value: %d", high_temp[i]);
        printf("The low value: %d", low_temp[i]);
        printf("-----");
    }
}

And this is the output. The error happens when I'm trying to print out the elements in each array. I'm doing this to see if any of the illegal values creeped into the array. I'm suppose to take their medians afterwards.

---=== IPC Temperature Analyzer ===---
Enter the high value for day 1: 8
Enter the low value for day 1: -2
Enter the high value for day 2: 41
Enter the low value for day 2: -4
Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low
Enter the high value for day 2: 9
Enter the low value for day 2: -4
Enter the high value for day 3: 5
Enter the low value for day 3: 11
Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low
Enter the high value for day 3: 11
Enter the low value for day 3: 5
*** stack smashing detected ***: terminated

There are two major problems in your code, one in each for loop.

In the first, you need to remember that, in C , array indexes run from zero through to n - 1 (where n is the size of the array). So, the code:

for (int i = 1; i <= NUMS; i++) {
    //...

should be replaced with this:

for (int i = 0; i < NUMS; i++) { // Note that NUMS = 3 and high_temp[3] is out-of-bounds
    //...

The second for loop has a different error. Here, you are correctly running from zero to the n - 1 value, but you are miscalculating the n . So, instead of sizeof(high_temp) (which will give you the total size of the integer array) you need to divide that value by the size of one element (conventionally, use the first). So, use this, instead:

for (size_t i = 0; i < sizeof(high_temp)/sizeof(high_temp[0]); i++){
    //...

(I've changed the type from int to size_t as this is what the sizeof operator returns; it is generally an unsigned int or unsigned long , or some such.)

EDIT: In the first loop, with the modifications I have suggested, you can 'restore' the correct number in the printed questions by simply adding 1 to the value of i that is printed. So, like this:

    printf("Enter the high value for day %d: ", i + 1);

EDIT2: There is also an issue regarding the values you assign in the first loop. You are doing this:

        }else{
            high_temp[i] = i;
            low_temp[i] = i;
        }

but the values you actually want to assign have been read into the high and low variables a few lines earlier. So, use this, instead:

        } else {
            high_temp[i] = high;
            low_temp[i] = low;
        }

There are a few problems with the code.

  1. Your first for loop is wrong. The index should start at 0 and end before NUMS like this:

for (int i = 0; i < NUMS; i++)

This way the loop will go from from 0 to 2, since array indices start at 0 and your high_temp and low_temp arrays can only store 3 values. The way you have it written it will try to write to high_temp[3] and low_temp[3] which are not valid.

  1. The upper bound of your second loop is wrong. sizeof(high_temp) doesn't do what you think it does; it returns 12 not 3, so in your second loop you're trying to access elements outside the bounds of the array. In general you would use sizeof(high_temp) / sizeof(high_temp[0]) but in this case you have already have NUMS so use that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM