Anyone familiar with the Diamond - Square algorithm?

I have been testing the Diamond Square algoritm to generate voxel landscapes, using various snippets of source codes and youtube videos I found. The result is not bad but I’m running into a memory access violation on the Map, at “Map[i + HalfSize][j + HalfSize]”. The examples I used were Python, Java, Js, PHP, Lua and name it… All which probably deal differently with integer division. Leaving me with a memory violation when I try to acces the map on certain map sizes. Specific sizes but mostly bigger (10x10x10 +) maps. Is it an obvious mistake?

TMap<FIntVector, FS_VoxelGridAlgorithmOutput> Make_DiamondSquare(FIntVector InSize, const FS_VoxelGridConfig& InVoxelGridConfig) {
	/** Algorithm articles:
	* https://github.com/mlepage/heightmap/blob/master/heightmap.lua
	* https://www.youtube.com/watch?v=4GuAV1PnurU
	* https://github.com/klaytonkowalski/example-diamond-square/blob/main/example/go.script
	* https://en.wikipedia.org/wiki/Diamond-square_algorithm
	*/

	// Make a 2D array of X and Y, we drop Z.
	int MapSize = 0;
	{
		// This algorithm requires odd numbers per axis, so we might have to add rows to the temp array.
		int MapSizeX = (InSize.X % 2 == 0) ? (InSize.X + 1) : InSize.X;
		int MapSizeY = (InSize.Y % 2 == 0) ? (InSize.Y + 1) : InSize.Y;
		// Just pick the biggest number to generate our map as a square.
		MapSize = MapSizeX > MapSizeY ? MapSizeX : MapSizeY;	
		UE_LOG(LogXVoxels, Verbose, TEXT("InSize.X: (%d)"), InSize.X);
		UE_LOG(LogXVoxels, Verbose, TEXT("InSize.Y: (%d)"), InSize.Y);
		UE_LOG(LogXVoxels, Verbose, TEXT("Optimal map size for algorithm calculated: (%d)"), MapSize);
	}

	int** Map = new int *[MapSize];
	for(int i = 0; i < MapSize; i++) {
		Map[i] = new int[MapSize];
		for(int j = 0; j < MapSize; j++) {
			UE_LOG(LogXVoxels, Verbose, TEXT("Adding i: (%d), j: (%d) to the map."), i, j);
			Map[i][j] = 0;
		}
	}

	// Set the 4 corners of the 2D array to initial values
	// All equal corner heights should make the map tileable.
	const int CornerZHeight = FMath::Floor(InSize.Z / 2);
	Map[0]				[0]				= CornerZHeight;
	Map[0]				[MapSize - 1]	= CornerZHeight;
	Map[MapSize - 1]	[0]				= CornerZHeight;
	Map[MapSize - 1]	[MapSize - 1]	= CornerZHeight;

	// Next the algorithms:

	/* The width of each square in the current iteration - 1. The ChunkSize will never be an odd number. */

	int ChunkSize = MapSize - 1;
	/* The random range, anything preferred. Example: -2 to 2, -1 to 1 */
	//int Roughness = 2;

	while (ChunkSize > 1) {
		int HalfSize = FMath::Floor(ChunkSize / 2);
		UE_LOG(LogXVoxels, Verbose, TEXT("HalfSize (%d) of ChunkSize (%d)"), HalfSize, ChunkSize);

		// Square step:

		for (int i = 0; i < MapSize - 1; i += ChunkSize) {
			UE_LOG(LogXVoxels, Verbose, TEXT("SquareStep i: (%d) + ChunkSize: (%d)"), i, i + ChunkSize);
			for (int j = 0; j < MapSize - 1; j += ChunkSize) {
				UE_LOG(LogXVoxels, Verbose, TEXT("SquareStep j: (%d) + ChunkSize: (%d)"), j, j + ChunkSize);
				Map[i + HalfSize][j + HalfSize] = FMath::Floor((
						Map[i]				[j]
					+	Map[i]				[j	+ ChunkSize]
					+	Map[i + ChunkSize]	[j] 
					+	Map[i + ChunkSize]	[j	+ ChunkSize] 
					)	/ 4)
					// Make an average + random number
					+ FMath::RandRange(InVoxelGridConfig.RandomMin, InVoxelGridConfig.RandomMax);
			}
		}

		// Diamond step:

		for (int i = 0; i < MapSize; i += HalfSize) {
			UE_LOG(LogXVoxels, Verbose, TEXT("DiamondStep i: (%d)"), i);
			for (int j = FMath::Floor((i + HalfSize) % ChunkSize); j < MapSize; j += ChunkSize) {
				UE_LOG(LogXVoxels, Verbose, TEXT("DiamondStep j: (%d)"), j);
				int Sum = 0;
				int Count = 0;

				if (j - HalfSize > 0) {
					Sum += Map[i][j - HalfSize];
					Count++;
				}
				if (j + HalfSize < MapSize) {
					Sum += Map[i][j + HalfSize];
					Count++;
				}
				if (i - HalfSize > 0) {
					Sum += Map[i - HalfSize][j];
					Count++;
				}
				if (i + HalfSize < MapSize) {
					Sum += Map[i + HalfSize][j];
					Count++;
				}

				Map[i][j] = FMath::Floor(Sum / Count)
					+ FMath::RandRange(InVoxelGridConfig.RandomMin, InVoxelGridConfig.RandomMax
				);
			}
		}

		// half the chunk process sub squares / diamonds per iteration.
		ChunkSize = FMath::Floor(ChunkSize / 2);
	}

	// Build the output
	TMap<FIntVector, FS_VoxelGridAlgorithmOutput> Output;
	// Note that MapSizeX/Y could have an extra temp "odd" row here. Don't use it (out of bounds for InSize).
	for (int i = 0; i < InSize.X; i++) {
		for (int j = 0; j < InSize.Y; j++) {
			FS_VoxelGridAlgorithmOutput Data;
			for (int k = 0; k < InSize.Z; k++) {
				if ((!InVoxelGridConfig.bDisableUndergroundGeneration && k <= Map[i][j])
					|| (k == Map[i][j])
					) {
					Data.VoxelType = E_VoxelType::Ground;
				}
				else {
					Data.VoxelType = E_VoxelType::Empty;
				}

				// Add to the output
				Output.Add(FIntVector(i, j, k), Data);
			}
		}			
	}

	// Clean up
	for(int i = 0; i < MapSize; i++) {
		delete Map[i];
	}
	delete Map;

	// Return
	return Output;
};


FIntVector Size = FIntVector (14, 14, 1)

// :

[2022.07.26-00.37.12:665][614]LogXVoxels: Verbose: InSize.X: (14)
[2022.07.26-00.37.12:665][614]LogXVoxels: Verbose: InSize.Y: (14)
[2022.07.26-00.37.12:665][614]LogXVoxels: Verbose: Optimal map size for algorithm calculated: (15)
[2022.07.26-00.37.12:665][614]LogXVoxels: Verbose: Adding i: (0), j: (0) to the map.
[2022.07.26-00.37.12:665][614]LogXVoxels: Verbose: Adding i: (0), j: (1) to the map.
[2022.07.26-00.37.12:665][614]LogXVoxels: Verbose: Adding i: (0), j: (2) to the map.
[2022.07.26-00.37.12:665][614]LogXVoxels: Verbose: Adding i: (0), j: (3) to the map.
[2022.07.26-00.37.12:665][614]LogXVoxels: Verbose: Adding i: (0), j: (4) to the map.
[2022.07.26-00.37.12:665][614]LogXVoxels: Verbose: Adding i: (0), j: (5) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (0), j: (6) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (0), j: (7) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (0), j: (8) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (0), j: (9) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (0), j: (10) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (0), j: (11) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (0), j: (12) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (0), j: (13) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (0), j: (14) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (0) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (1) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (2) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (3) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (4) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (5) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (6) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (7) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (8) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (9) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (10) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (11) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (12) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (13) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (1), j: (14) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (0) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (1) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (2) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (3) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (4) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (5) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (6) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (7) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (8) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (9) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (10) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (11) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (12) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (13) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (2), j: (14) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (0) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (1) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (2) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (3) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (4) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (5) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (6) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (7) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (8) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (9) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (10) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (11) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (12) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (13) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (3), j: (14) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (0) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (1) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (2) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (3) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (4) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (5) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (6) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (7) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (8) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (9) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (10) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (11) to the map.
[2022.07.26-00.37.12:666][614]LogXVoxels: Verbose: Adding i: (4), j: (12) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (4), j: (13) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (4), j: (14) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (0) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (1) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (2) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (3) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (4) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (5) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (6) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (7) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (8) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (9) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (10) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (11) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (12) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (13) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (5), j: (14) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (0) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (1) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (2) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (3) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (4) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (5) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (6) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (7) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (8) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (9) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (10) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (11) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (12) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (13) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (6), j: (14) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (0) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (1) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (2) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (3) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (4) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (5) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (6) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (7) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (8) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (9) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (10) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (11) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (12) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (13) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (7), j: (14) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (8), j: (0) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (8), j: (1) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (8), j: (2) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (8), j: (3) to the map.
[2022.07.26-00.37.12:667][614]LogXVoxels: Verbose: Adding i: (8), j: (4) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (8), j: (5) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (8), j: (6) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (8), j: (7) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (8), j: (8) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (8), j: (9) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (8), j: (10) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (8), j: (11) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (8), j: (12) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (8), j: (13) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (8), j: (14) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (0) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (1) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (2) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (3) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (4) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (5) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (6) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (7) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (8) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (9) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (10) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (11) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (12) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (13) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (9), j: (14) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (0) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (1) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (2) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (3) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (4) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (5) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (6) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (7) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (8) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (9) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (10) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (11) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (12) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (13) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (10), j: (14) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (0) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (1) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (2) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (3) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (4) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (5) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (6) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (7) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (8) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (9) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (10) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (11) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (12) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (13) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (11), j: (14) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (0) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (1) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (2) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (3) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (4) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (5) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (6) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (7) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (8) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (9) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (10) to the map.
[2022.07.26-00.37.12:668][614]LogXVoxels: Verbose: Adding i: (12), j: (11) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (12), j: (12) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (12), j: (13) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (12), j: (14) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (0) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (1) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (2) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (3) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (4) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (5) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (6) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (7) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (8) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (9) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (10) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (11) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (12) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (13) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (13), j: (14) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (0) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (1) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (2) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (3) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (4) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (5) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (6) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (7) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (8) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (9) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (10) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (11) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (12) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (13) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: Adding i: (14), j: (14) to the map.
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: HalfSize (7) of ChunkSize (14)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: SquareStep i: (0) + ChunkSize: (14)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: SquareStep j: (0) + ChunkSize: (14)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep i: (0)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (7)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep i: (7)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (0)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (14)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep i: (14)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (7)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: HalfSize (3) of ChunkSize (7)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: SquareStep i: (0) + ChunkSize: (7)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: SquareStep j: (0) + ChunkSize: (7)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: SquareStep j: (7) + ChunkSize: (14)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: SquareStep i: (7) + ChunkSize: (14)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: SquareStep j: (0) + ChunkSize: (7)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: SquareStep j: (7) + ChunkSize: (14)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep i: (0)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (3)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (10)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep i: (3)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (6)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (13)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep i: (6)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (2)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (9)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep i: (9)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (5)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (12)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep i: (12)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (1)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: DiamondStep j: (8)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: HalfSize (1) of ChunkSize (3)
[2022.07.26-00.37.12:669][614]LogXVoxels: Verbose: SquareStep i: (0) + ChunkSize: (3)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (0) + ChunkSize: (3)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (3) + ChunkSize: (6)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (6) + ChunkSize: (9)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (9) + ChunkSize: (12)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (12) + ChunkSize: (15)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep i: (3) + ChunkSize: (6)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (0) + ChunkSize: (3)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (3) + ChunkSize: (6)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (6) + ChunkSize: (9)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (9) + ChunkSize: (12)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (12) + ChunkSize: (15)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep i: (6) + ChunkSize: (9)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (0) + ChunkSize: (3)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (3) + ChunkSize: (6)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (6) + ChunkSize: (9)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (9) + ChunkSize: (12)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (12) + ChunkSize: (15)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep i: (9) + ChunkSize: (12)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (0) + ChunkSize: (3)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (3) + ChunkSize: (6)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (6) + ChunkSize: (9)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (9) + ChunkSize: (12)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (12) + ChunkSize: (15)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep i: (12) + ChunkSize: (15)
[2022.07.26-00.37.12:670][614]LogXVoxels: Verbose: SquareStep j: (0) + ChunkSize: (3)
[2022.07.26-00.37.15:232][614]LogWindows: Error: === Critical error: ===
[2022.07.26-00.37.15:232][614]LogWindows: Error: 
[2022.07.26-00.37.15:232][614]LogWindows: Error: Fatal error!
[2022.07.26-00.37.15:232][614]LogWindows: Error: 
[2022.07.26-00.37.15:232][614]LogWindows: Error: Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0xffffffff00000014
[2022.07.26-00.37.15:232][614]LogWindows: Error: 
[2022.07.26-00.37.15:232][614]LogWindows: Error: [Callstack] 0x00007ff89dfbb75f UE4Editor-ART.dll!FS_VoxelGridAlgorithm::Make_DiamondSquare() [K:\Unreal Projects\ART_UE4\Source\ART\Public\World\Voxel\VoxelGridGenerator.h:158]
[2022.07.26-00.37.15:232][614]LogWindows: Error: [Callstack] 0x00007ff89dfbc34c UE4Editor-ART.dll!AVoxelGridGenerator::RegenerateVoxels() [K:\Unreal Projects\ART_UE4\Source\ART\Private\World\Voxel\VoxelGridGenerator.cpp:88]
[2022.07.26-00.37.15:232][614]LogWindows: Error: [Callstack] 0x00007ff89dfc50b2 UE4Editor-ART.dll!AVoxelGridGenerator::execRegenerateVoxels() [K:\Unreal

Isn’t Diamond-Square supposed to operate on powers-of-two plus 1 in both x and y dimensions? Neither the initial 14 nor the 15 after adding and additional row to make the initial size odd are valid inputs for the algorithm. But e.g. 17 would be valid as 2^4 + 1

1 Like

Perfect answer! No more crashes. I forgot to validate to (2^n)+1.

// Just pick the biggest number to generate our map as a square.
int MapSize = InDesiredSize.X > InDesiredSize.Y ? InDesiredSize.X : InDesiredSize.Y;	

if (MapSize < 3) {
	MapSize = 3;
}

// Test if the size is a (power of 2) + 1
// , else increase the map size to match 2^n+1 which the algorithm requires
if (!((MapSize - 1 != 0) && ((MapSize - 1 & (MapSize - 2)) == 0))) {
	int NewSize = 2;
	while (NewSize < MapSize) {
		NewSize *= 2;
	}
	MapSize = NewSize + 1;
}