throw_bad_alloc error with arrays


Salutations. I am new here, but I have seen that people here are quick to reply.

I have a problem when I'm handling some arrays in my program. I have an array like so:

1
2
3
4
5
namespace categories {

    std::string LightMachineGuns[11] = { "Colt LMG", "M60", "AUG HBAR", "MG36", "RPK12", "L86 LSW", "RPK", "HK21", "SCAR HAMR", "RPK-74", "MG3KWS"};

}



The code that handles this is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
          misc::nameReturn = "";
                misc::rankReturn = "";
                misc::categoryReturn = "";
                _ru::hideWindows(true, true, true, true, true, true, true, true, true, true);

                for (int i = 0; i <= sizeof(categories::LightMachineGuns) / sizeof(std::string); i++) {

                    //insert position code here
                    posArray[i] = i * size;
                    _ff::weaponDataGetSetter(categories::LightMachineGuns[i]);
                    nameStringArray[i] = misc::nameReturn; //name
                    rankStringArray[i] = "Rank: " + misc::rankReturn; //rank
                    categoryStringArray[i] = "Category: " + misc::categoryReturn; //category

                }


Please do note that the nameStringArray, rankStringArray, and categoryStringArrays are string arrays that contain the name, rank, and category of the arrays (that involves using a class, objects, and some more mumbo jumbo that would be too long to post here).

However, when I try to access the for loop in the code with my program, it throws an exception in a "throw_bad_alloc.cpp" file deep within the jungle of Visual Studio '19. The code that throws that exception is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#if defined MRTDLL && defined _M_CEE

    _CRT_SECURITYCRITICAL_ATTRIBUTE
    void __CRTDECL __scrt_throw_std_bad_alloc()
    {
        TerminateProcess(GetCurrentProcess(), 3);
    }

    _CRT_SECURITYCRITICAL_ATTRIBUTE
    void __CRTDECL __scrt_throw_std_bad_array_new_length()
    {
        TerminateProcess(GetCurrentProcess(), 3);
    }

#else // ^^^ Managed ^^^ // vvv Native vvv //

    __declspec(noreturn) void __CRTDECL __scrt_throw_std_bad_alloc()
    {
        throw std::bad_alloc{}; 
    }                             //exception thrown here

    __declspec(noreturn) void __CRTDECL __scrt_throw_std_bad_array_new_length()
    {
        throw std::bad_array_new_length{};
    }

#endif  
However, when I try to access the for loop in the code with my program, it throws an exception in a "throw_bad_alloc.cpp" file deep within the jungle of Visual Studio '19. The code that throws that exception is as follows:

You need to back trace the problem to somewhere in your code, the code of the implementation is the last place you should be looking for a problem.

The following is probably a good place to start looking for the source of the crash.
for (int i = 0; i <= sizeof(categories::LightMachineGuns) / sizeof(std::string); i++)
First instead of the magic number you used in defining the array, either use a vector or use a named constant for the size, this will eliminate the need for the sizeof() call. A vector would be much easier because a vector knows it's own size.

Second remember that vectors/arrays start at zero and end at size - 1, so using the "<=" operator is probably a buffer overrun.

As an aside, if the compiler tells you that the problem is somewhere deep in the standard library or elsewhere, a large portion of the time the problem is with your code, not the external library.

Also, I would use C++17's std::size() method for your array instead of the sizeof() operator. Its a bit cleaner and gives you the exact stack-based array extent. And of course fix the problem jlb mentioned above.
Last edited on
Thank you very much for the reply! I have replaced the <= operater with the regular < operater and put a number that is the array's size + 1 to compare with. It now works without crashing.

Only thing I want to know is how would I implement the array into a vector? I don't know much about C++ (still a novice) and I would like to know how I could use it for future use. It would help me very much! :D
Last edited on
Using a vector is actually quite simple:
1
2
3
4
5
#include <vector>
std::vector<std::string> LightMachineGuns = 
{ "Colt LMG", "M60", "AUG HBAR", "MG36", "RPK12", "L86 LSW", 
   "RPK", "HK21", "SCAR HAMR", "RPK-74", "MG3KWS"
};

http://www.cplusplus.com/reference/vector/vector/
Thank you very much for the reply! I have replaced the <= operater with the regular < operater and put a number that is the array's size + 1 to compare with.

This is still a probable buffer overrun problem. Arrays stop at size - 1, not size. But again use a std::vector and then in many cases you can use a range based loop which will not overrun the vector.

1
2
3
4
5
6
7
8
9
10
11
12
#include <vector>
#include <iostream>

int main()
{
    std::vector<std::string> lightMachineGuns{ "Colt LMG", "M60", "AUG HBAR", "MG36", "RPK12", "L86 LSW", "RPK", "HK21", "SCAR HAMR", "RPK-74", "MG3KWS"};
    
    for(auto& lightGun : lightMachineGuns)
    {
        std::cout << lightGun << '\n';
    }
}


Topic archived. No new replies allowed.