An array is out of bound crash

Hello, I would like to ask you for help with a crash problem.

My code compiles with no issues, but when I try to call it, the game crash, because:

Array index out of bounds: 0 from an array of size 0.

I have checked that all of the input arrays have the same length (400).

I tried the rebuild solution stated here: [link text][1], but it did not work.

Any idea what could be causing it?

The code of the problematic function is:

void UOperaceSeStverci::C_Vapor(TArray<float> Hummidity, TArray<float> FreeWater, TArray<float> Temperature, float VaporRate, TArray<float>& NewHummidity, TArray<float> & NewFreeWater)
{
	float ToVapor;
	float a;
	float b;
	float water;
	TArray<float> T;
	float vapored;
	TArray<float> H;
	TArray<float> FW;
	TArray<float> NH;
	TArray<float> NFW;

	T = Temperature;
	H = Hummidity;
	FW = FreeWater;
	a = -0.00045; // constants calculated for linear regresion - approx 5% for 24 hour with averange temperature 0°C and 21% for averange 20°C.
	b = 0.989;
	for (int i = 0; i != T.Num(); ++i)
	{
		ToVapor = VaporRate * a * T[i] + b;
		if (FW[i] == 0)
		{
			NH[i] *= ToVapor;
			NFW[i] = 0;
		}
		else
		{
			water = H[i] + FW[i];
			vapored = water * ToVapor;
			FMath::Clamp(vapored, 0.0f, H[i] * ToVapor);
			if (vapored <= FW[i])
			{
				NH[i] = H[i];
				NFW[i] -= vapored;
			}
			else
			{
				NFW[i] = 0;
				NH[i] = H[i] - vapored + FW[i];
			}
		}
		NH[i] = floor(NH[i]);
		NFW[i] = floor(NFW[i]);
	}
	NewHummidity = NH;
	NewFreeWater = NFW;
}

This is how I call it in the blueprint (GameState class):

The full error message is :

Assertion failed: (Index >= 0) & (Index < ArrayNum) [File:C:\Program Files\Epic Games\UE_4.26\Engine\Source\Runtime\Core\Public\Containers/Array.h] [Line: 674] Array index out of bounds: 0 from an array of size 0

UE4Editor_Core
UE4Editor_Core
UE4Editor_PokusySeCtverci_4003!DispatchCheckVerify >() [c:\program files\epic games\ue_4.26\engine\source\runtime\core\public\misc\assertionmacros.h:165]
UE4Editor_PokusySeCtverci_4003!UOperaceSeStverci::C_Vapor() [c:\hryl\unreal\pokus_ctvercovasit\pokusysectverci\source\pokusysectverci\operacesestverci.cpp:178]
UE4Editor_PokusySeCtverci_4003!UOperaceSeStverci::execC_Vapor() [c:\hryl\unreal\pokus_ctvercovasit\pokusysectverci\intermediate\build\win64\ue4editor\inc\pokusysectverci\operacesestverci.gen.cpp:133]
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll

Any of your array overflows, especially you are taking base of your T array.
Lets assume that T.num() = 30 and H.num(),FW.num(),NH.num(),NFW.num() should bigger than ‘30’.

What if they smaller, lets assume that your T.num() = 30 and H.num() = 6 and your index goes until 30. When your index i=7 will check for H[7], but H only have 6 members, there is no H[6] ‘kaboom’. Index out of bounds…
Little note here : indexes starts from 0. 0,1,2,3,4,5 = 6 members.

Please debug if your other arrays bigger than it.

Or… Add line as :

 for (int i = 0; i != T.Num(); ++i)
     {
         // this will break your for loop in case of problem in overflow.
         // you can do different checks in different places in for loop.
         // rest is related with your design.
         if (i >= H.num() || i >= FW.num() || i >= NH.num() || i >= NFW.num())
              break

         ToVapor = VaporRate * a * T[i] + b;
         if (FW[i] == 0)
         {
             NH[i] *= ToVapor;
             NFW[i] = 0;
         .....
1 Like

you are using the bracket operator [] to set the new values in the arrays T…NFW defined inside your function,

but I don’t see any memory allocation or size definition before…