Well I would imagine your average C++ developer would not wish to have [some][crazy][multi][dimensional][array][mess][such][as][this][anyway]. But in C people ask all the time....
Well I would imagine your average C++ developer would not wish to have [some][crazy][multi][dimensional][array][mess][such][as][this][anyway]. But in C people ask all the time....
Indeed. I think the biggest use is an array of strings which is abstracted away with std::string or string classes in C++.
I have found little use for multi-dimensional arrays in C++ and when I've had the use of one, it's been for only 2D.
Of course I haven't worked on games, so my mileage may vary on this topic.
Well I try not to use multidimensional arrays in games due to the fact they are inefficient. Though most will argue "that is what my optimizer is for" I can only site that an optimizer is like a car or a power drill. Its a tool. Its not perfect. Where I can help it along, I do prefer to do so. People too frequently cut so many corners based on the preface that their compiler will fix everything. I have found in my experience that calculating one array index once and applying it to multiple arrays is much faster than multidimensional arrays.
This is something I made in C:
(It works like the PHP explode().)
It leaves some unfreed memory though.Code:char** str_explode(separator, str) const char separator; char* str; { int len = strlen(str); int nSeparators = 0; int* length = (int*) malloc( sizeof(int) * len ); int i = 0; for(; i < len; ++i) *(length + i) = 0; i = 0; for(; i < len; ++i) { if( *(str + i) == separator ) ++nSeparators; else *(length + nSeparators) += 1; } int start = 0; char** results = (char**) malloc( sizeof(char) * (nSeparators + 1) ); i = 0; for(; i <= nSeparators; ++i) { *(results + i) = (char*) malloc( sizeof(char) * (*(length + i) + 1) ); memcpy(*(results + i), str + start, *(length + i) + 1); *(*(results + i) + *(length + i)) = '\0'; start += *(length + i) + 1; } free(length); *(results + nSeparators + 1) = NULL; return results; }
Last edited by rudyman; 04-24-2008 at 09:39 AM.
Let's use ANSI C function style...Code:char** str_explode(const char separator, char* str) { int len = strlen(str); int nSeparators = 0; int* length = (int*) malloc( sizeof(int) * len ); int i = 0; for(; i < len; ++i) *(length + i) = 0; i = 0; for(; i < len; ++i) { if( *(str + i) == separator ) ++nSeparators; else *(length + nSeparators) += 1; } int start = 0; char** results = (char**) malloc( sizeof(char) * (nSeparators + 1) ); i = 0; for(; i <= nSeparators; ++i) { *(results + i) = (char*) malloc( sizeof(char) * (*(length + i) + 1) ); memcpy(*(results + i), str + start, *(length + i) + 1); *(*(results + i) + *(length + i)) = '\0'; start += *(length + i) + 1; } free(length); *(results + nSeparators + 1) = NULL; return results; }
I decided against this idea in C because it would mean you have to do return a 2D allocated array, which meant you had to free it later, which would be bad.