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:
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.
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
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.