© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
19 replies
Tal

Memory Referrence Issue In Recursive Algorithm

Hello, I have a problem with the algorithm in the picture. What it does is take the values from a single dimensional array and puts them into an N dimensional array (additional explanation in the picture).

This algorithm is recursive, and my problem I have is with the 'indexerArr' array of integers, where when the function calls itself again, then returns to itself, the function will remember the changes that were done to the indexer array in the self call.

For example, let's say that the function receives the following parameters:
arr = { 1, 2, 3, 7, 8, 9 }
arrToAssemble = { {3, 2, 1}, {9, 8, 7} }
indexerArr = { 0, 0 }
currRank = 0

The function will enter the first for loop, call itself, and in that call the second for loop will be executed. And the 'arrToAssemmble' array returned will be { {1, 2, 3}, {9, 8, 7} } and the indexer array which is NOT returned will be { 0, 3 }, then it will become { 1, 3 }, and upon calling itself again, it will arrive at the second for loop which will not run because of the index being out of range.

It's like the function returning to itself a parameter which is not supposed to return.
Any suggestions to solve this?
recursive_algorithm_with_problem.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Debugging recursive algorithms
C#CC# / help
4y ago
Recursive method and memory problems
C#CC# / help
3y ago
Fibonacci in recursive ?
C#CC# / help
4y ago
KMP-Algorithm in C#
C#CC# / help
2y ago