Sign in to follow this  

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 this post

Link to post
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 this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this