Dynamic Dimensions of Arrays/Pointers

Recommended Posts

Hi all, I have been developing a very _generic_ fuzzy logic implementation in C++, with hybrid abilities with Neural Networks. Problem: For fuzzy systems with three inputs, the rules set is three dimensional. such as: rules[][][]; I.e. the dimension of the rules array is proportional to the number of inputs to the system. My question is: How can I develop the 'rules' array so that it can change dimensions dynamically. e.g. So it becomes rules[][] with two inputs. Even with pointers you must define dimensions like: char ***rules; for three dimension. I know it is possible to define an 'n' dimensional array and program to only use 'm' of the dimensions (where m<=n). I have done this, but I am interested to know how it may be tackled dynamically. I know that PHP, for example, has these features inbuilt. That doesn't really tell me how it happens, though. Any help is appreciated. Thanks

Share on other sites
I would suggest simply using one array, and calculating the space required.

I'm not using STL here for simplicity, but you should use std::vector.

int first_dimension = 5;int second_dimension = 7;Rule * rules = new Rule[first_dimension * second_dimension];

And then to use "rules[4][2]", you would write instead:

rules[4 * second_dimension + 2]

Similarly with three dimensions you just calculate the third dimension when needed (or store it if used multiple times). This has the benefit of being only a "Rule *" pointer, which you can change if needed. Once again, use std::vector.

Share on other sites
I would *strongly* recommend looking into boost::multi_array. It sounds like you need the big guns here ;)

Create an account

Register a new account

• Forum Statistics

• Total Topics
628367
• Total Posts
2982284

• 10
• 9
• 13
• 24
• 11