{"record":{"id":"ff7336cf2b4afc0a","repo":"stride3d/stride","slug":"the-destination-array-must-be-of-same-length-or-larger-ff7336","errorCode":null,"errorMessage":"The destination array must be of same length or larger length than the source array.","messagePattern":"The destination array must be of same length or larger length than the source array\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/Vector2.cs","lineNumber":854,"sourceCode":"    /// least stable.</para>\n    /// </remarks>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"source\"/> or <paramref name=\"destination\"/> is <c>null</c>.</exception>\n    /// <exception cref=\"ArgumentOutOfRangeException\">Thrown when <paramref name=\"destination\"/> is shorter in length than <paramref name=\"source\"/>.</exception>\n    public static void Orthogonalize(Vector2[] destination, params Vector2[] source)\n    {\n        //Uses the modified Gram-Schmidt process.\n        //q1 = m1\n        //q2 = m2 - ((q1 ⋅ m2) / (q1 ⋅ q1)) * q1\n        //q3 = m3 - ((q1 ⋅ m3) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m3) / (q2 ⋅ q2)) * q2\n        //q4 = m4 - ((q1 ⋅ m4) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m4) / (q2 ⋅ q2)) * q2 - ((q3 ⋅ m4) / (q3 ⋅ q3)) * q3\n        //q5 = ...\n\n        if (source == null)\n            throw new ArgumentNullException(nameof(source));\n        if (destination == null)\n            throw new ArgumentNullException(nameof(destination));\n        if (destination.Length < source.Length)\n            throw new ArgumentOutOfRangeException(nameof(destination), \"The destination array must be of same length or larger length than the source array.\");\n\n        for (int i = 0; i < source.Length; ++i)\n        {\n            Vector2 newvector = source[i];\n\n            for (int r = 0; r < i; ++r)\n            {\n                newvector -= (Vector2.Dot(destination[r], newvector) / Vector2.Dot(destination[r], destination[r])) * destination[r];\n            }\n\n            destination[i] = newvector;\n        }\n    }\n\n    /// <summary>\n    /// Orthonormalizes a list of vectors.\n    /// </summary>\n    /// <param name=\"destination\">The list of orthonormalized vectors.</param>","sourceCodeStart":836,"sourceCodeEnd":872,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/Vector2.cs#L836-L872","documentation":"Vector2.Orthogonalize (Gram-Schmidt without normalization) writes one output per source element, so the destination array must be at least as long as the source. A shorter destination throws ArgumentOutOfRangeException with this message.","triggerScenarios":"Calling Vector2.Orthogonalize(sourceArray, destinationArray) where destination.Length < source.Length, e.g. allocating destination from the wrong count constant or reusing a smaller scratch buffer.","commonSituations":"Allocating the output with the same count as a previous (different-sized) batch; buffer reuse across loop iterations with shrinking allocations; copy-paste between Orthogonalize/Orthonormalize calls with mismatched arrays.","solutions":["Allocate destination with length >= source.Length: new Vector2[source.Length].","Slice the source to destination.Length if intentional truncation is desired (then pass equal-length arrays).","Check any cached/shared scratch buffer for the correct capacity before the call."],"exampleFix":"// before\nvar dest = new Vector2[source.Length - 1];\nVector2.Orthogonalize(source, dest);\n// after\nvar dest = new Vector2[source.Length];\nVector2.Orthogonalize(source, dest);","handlingStrategy":"validation","validationCode":"if (source == null || destination == null)\n    throw new ArgumentNullException(source == null ? nameof(source) : nameof(destination));\nif (destination.Length < source.Length)\n    throw new ArgumentException(\"destination must be at least as long as source.\", nameof(destination));\nVector2.Orthogonalize(source, destination);","typeGuard":null,"tryCatchPattern":"try\n{\n    Vector2.Orthogonalize(source, destination);\n}\ncatch (ArgumentOutOfRangeException ex)\n{\n    destination = new Vector2[source.Length];\n    Vector2.Orthogonalize(source, destination);\n}","preventionTips":["Always allocate the destination with new Vector2[source.Length].","Never reuse scratch buffers sized for other batches without checking capacity.","Validate array lengths in a helper wrapper around Orthogonalize/Orthonormalize."],"tags":["mathematics","argument-out-of-range","array-length","gram-schmidt"],"backgroundTag":"argument-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}