Declaring array depth

Lets say that I need setup an array to store some information in a file and the file tells me how many dimensions this data wants and how long each array is in size.
Would I be able to setup an array depth somehow? I know that using the "/clr" command in VS12 enables the Visual part of C++ where I have access to the CLI namespace to use array<type, depth> [id] but then that breaks my intellisense for some reason and I hate to say that I'm rather dependant on it because I'm doing fairly experimental work.

Anyone know of an equivalent method for declaring array depth?
Personally I think that declaring fixed-length arrays (via the std::array <T,N> template) is a throwback to the original Pascal's broken array type handling.

You are better off just using a std::vector <T>. It sounds to me like you are dealing with variable-length array data anyway... but even if you aren't, it isn't too much just to put in a runtime check that you have the proper number of elements in your array.

Hope this helps.
Not really :(

What I mean by depth is lets say I have a depth of 3.

array<type, 3> name should be the same as <type> name[][][]
And array<type, 5> name same as <type> name[][][][][]
Last edited on
At the moment I'm attempting to use a recursive function (so it keeps getting called per depth) to initialise an array on the memory heap and assign it data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<class T> T* handleArray(int depth, int size[], string data){
    if(depth < 1)
        return NULL;
    if(depth == 1){
        T* temp = new T[size[0]];
        for(...){
            //initialise temp[i] using data;
        }
    }
    else{
        T* temp = new T[size[0]];
        for(...){
            temp[i] = handleArray(depth -1, size+1, data);
        }
    }
}
Simulate an array with the rank (number of dimensions) known only at run-time by allocating a single linear array large enough to hold all the elements. And then perform indices to offset computations at run-time.

Or use a library which does that. Google threw up this one (appears to be non-viral):
http://www.andres.sc/marray.html

Yes this thought did cross my mind, but the idea is that I'm writing this function in case at some point I may need I may need large depthed arrays for clear separation of data, I can't just setup a 3/4 dimension array without having to write a function to go with that specific array to convert from a single dimension into multiple.
My intention is to write perhaps a slightly complex function now, and save trouble later... Isn't that what most of programming is when it comes to the real problems?
> My intention is to write perhaps a slightly complex function now, and save trouble later...
> Isn't that what most of programming is when it comes to the real problems?

When it comes to real problems, the best code is quite often the code that you do not write. Try to use a good library.
@JLBorges, I don't think you can really say that, after all... Who rights the libraries?
Someone who has come across a problem and tried there own method to solve it. But I shall take a look, I don't suppose you know if Boost have anything like what I want here because I've already got there libraries installed to my compiler
closed account (Dy7SLyTq)
i think he means that there is usually well tested, well defined code that is known to work and relatively bug free
Yeah I know what he meant, but I'm just saying that the fact is sometimes there just aren't libraries made for some things and other have to make them (or just a function for themselves)... Whether they're the "best" or not won't be known because everyone will have different unpublished methods.

Anyways, anyone got any actual idea to whether this'll work or not?
Code is here (Sorry it's in tab indentation rather than 4-space):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Types noted in caps are taken from an enum variable
// The class Filed, is a helper for creating new types, it includes functions for data loading which are designed to be overloaded
// Data is where the data for the array is stored, other functions will be provided for writing this data correctly depending on the type

template<class T> T* handleArray(char type = UNKNOWN, int depth, int size[], std::string& data, T** out){
	if(depth < 1 || size[0] < 1)
		return NULL;
	if(depth == 1){
		T* temp = new T[size[0]];
		for(int i=0; i<size[0]; i++){
			switch(type){
			case CHAR:{
				sscanf_s(data.substr(i, 1).c_str(), "%c", temp[i]);
				break;
			}
			case SHORT:{
				sscanf_s(data.substr(i, 2).c_str(), "%hd", temp[i]);
				break;
			}
			case INT:{
				sscanf_s(data.substr(i, 4).c_str(), "%d", temp[i]);
				break;
			}
			case FLOAT:{
				sscanf_s(data.substr(i, 4).c_str(), "%f", temp[i]);
				break;
			}
			case DOUBLE:{
				sscanf_s(data.substr(i, 8).c_str(), "%lf", temp[i]);
				break;
			}
			default:{
				if(type & FILED){
					Filed* t = dynamic_cast<Filed*>(temp[i]);
					t->loadFromString(data, true);
				}
				break;
			}
			}//switch
		}//for
	}//if
}//func 
Last edited on
Topic archived. No new replies allowed.