0

I have an array that inserts a value and shifts elements. Although it compiles, I have some warnings. Here is my code:

#include <iostream>

void display_array(int arr[], int max_length = 10);
void insert_array(int arr[], int insert_num, int index_num, int max_length = 10);

int main() {
    
    int arr_length = 10;
    int new_score, index;
    int test_scores[arr_length] = {86, 71, 92, 84, 88, 96};

    display_array(test_scores);

    std::cout << "Enter new test score: ";
    std::cin >> new_score;

    std::cout << "\nEnter index position: ";
    std::cin >> index;

    insert_array(test_scores, new_score, index);
    display_array(test_scores);

    return 0;
}

void display_array(int arr[], int max_length) {
    std::cout << "Test scores in array: ";
    for(int i = 0; i < max_length; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << "\n";
}

void insert_array(int arr[], int insert_num, int index_num, int max_length) {
    for(int i = max_length; i > index_num; i--) {
        arr[i] = arr[i - 1];
    }
    arr[index_num] = insert_num;
}

In VS Code I get the warning: "variable "test_scores" may not be initializedC/C++ (145)"

I changed my 'arr_length' variable to constant and it went away, but although my code still compiles after the change and performs all of its functions, at the end of my program I get

*** stack smashing detected ***: terminated
Aborted (core dumped)

And gdb gives me something like this:

Program received signal SIGABRT, Aborted.
0x00007ffff7acd615 in raise () from /usr/lib/libc.so.6

I checked this link When does a process get SIGABRT (signal 6)? and it seems like it is a memory issue. But when I look at how I structured my array and my for loop they seem to be in bounds and it doesn't look like I'm trying to access something that isn't there... what is causing this error?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Knauxu
  • 13
  • 2

2 Answers2

2

Your code does this

for(int i = max_length; i > index_num; i--) {
    arr[i] = arr[i - 1];
}

where max_length equals 10. But your array has size 10 so the maximum valid index is 9. Change to

for(int i = max_length - 1; i > index_num; i--) {
    arr[i] = arr[i - 1];
}
john
  • 85,011
  • 4
  • 57
  • 81
  • That was it. I know this, but sometimes I don't always recognize it in my programs. Thank you for the help! – Knauxu Dec 25 '20 at 09:59
1

Stack buffer overflow bugs are caused when a program writes more data to a buffer located on the stack than what is actually allocated for that buffer. gcc adds a protection mechanism to detect stack overflow errors.

gcc compiler adds protection variables called canaries, which have known values. The value of the canary changes, if you write beyond the alloted memory. This will trigger stack smashing (modifying canary variable).

for(int i = max_length; i > index_num; i--) 
{
    arr[i] = arr[i - 1];
}

Here, max_length = 10, so arr[i] mean arr[10] which is outside the size of the array. You're essentially accessing out of bound memory. This will modify the value of canary variable. That's why you got stack smashing error.

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26