0

I was solving a problem from hackerrank in VSCode. I thought I had finally figured out the solution, so I copied it over to the hackerrank compiler. I hit compile and it pops up an "Abort Called" error. Here's the code:

#include <iostream>
#include <bits/stdc++.h>

void printEvenArray(char charArray[], int length)
{
    for(int i = 0; i < length; i++)
    {
        if(i == 0 || i % 2 == 0)
        {
            std::cout << charArray[b];
        }
    }

    std::cout << ' ';
}

void printOddArray(char charArray[], int length)
{
    for(int i = 0; i < length; i++)
    {
        if(i != 0 && i % 2 != 0)
        {
            std::cout << charArray[i];
        }
    }
    std::cout << '\n';
}

int main() {
    int numOfSubjects, stringLength = 0;

    std::cin >> numOfSubjects;

    std::string subjectString[numOfSubjects];
    char stringToCharArray[stringLength + 1];

    for(int i = 0; i < numOfSubjects; i++)
    {
        std::cin >> subjectString[i];
    }

    for(int x = 0; x < numOfSubjects; x++)
    {
        stringLength = subjectString[x].length();
        strcpy(stringToCharArray, subjectString[x].c_str());
        printEvenArray(stringToCharArray, stringLength);
        printOddArray(stringToCharArray, stringLength);
    }

    return 0;
}

This code compiles fine in VSCode. It gives me the desired outcome, but as soon as I bring it over to hackerrank, it gives an "Abort Called" error. I've read up online that abort called only shows up when either I try to use memory I don't have access to or is read only, or if I use a certain macro, am I'm not using any macros. I'm also relatively knew to C++, and clueless to memory management if that's what the issue is here. I appreciate any help a whole lot.

  • Be careful when you use stuff you do not understand. The odds of surprises go way, way up. – user4581301 Aug 19 '20 at 03:35
  • 1
    Where did `b` come from? – user4581301 Aug 19 '20 at 03:36
  • 3
    Translated into English, `std::cin >> numOfSubjects; std::string subjectString[numOfSubjects];` means, "Please overflow the stack" – user4581301 Aug 19 '20 at 03:38
  • 2
    Note: Hackerrank and other online judges can be fun and practice at decomposing and solving problems along with improving your understanding of data structures and the finer points of abusing the boundaries of C++. They do not make a particularly good teaching tool for language fundamentals. The best way to get the fundamentals down is with guided learning from a competent instructor or good text books backed up with good language references. Can't help you with finding a good instructor, but SO keeps [a curated list of good books](https://stackoverflow.com/questions/388242), – user4581301 Aug 19 '20 at 03:45
  • The trick to memory management is don't do it. Let the system do it for you. Use Automatic variables. They manage themselves. Use [containers](https://en.cppreference.com/w/cpp/container) to organize and manage collections of data. Use [Smart Pointers](https://docs.microsoft.com/en-us/cpp/cpp/smart-pointers-modern-cpp?view=vs-2019) to manage more free-form data that can't be automatically allocated for one reason or another. Only manually allocate when you absolutely have to, and promptly [wrap the allocation with RAII](https://stackoverflow.com/questions/2321511) so others don't have to worry – user4581301 Aug 19 '20 at 03:53
  • 1
    `char charArray[],` Don't do this. Ude std::string throughout your code. `std::string subjectString[numOfSubjects];` Don't do this. Use std::vector. `stringLength = subjectString[x].length();` This does NOT resize an array you declared earlier. – n. m. could be an AI Aug 19 '20 at 05:24

1 Answers1

2

char stringToCharArray[stringLength + 1];

So, stringToCharArray has length 1. Nothing in the loop changes that. Your strcpy is much longer than one character, so it just overwrites whatever was next in memory. On one compiler you got away with this behavior, but the other (probably deliberately set to look for boundary violations like this) aborted.

Read about https://en.wikipedia.org/wiki/Buffer_overflow.

There are some other improvements you can make. For instance, i+=2 can step through a loop two at a time without checking whether i be odd or even.

Andrew Lazarus
  • 18,205
  • 3
  • 35
  • 53