Keeping old data in a multidimensional array

Something like 6 months ago I made a class that encapsulated an array of any number of dimensions using cstdarg, but I never finished it because I couldn't figure out how to keep the old data. Sometime today someone asked about variable dimension arrays, which perked up my interest again. While fixing the code, I figured out how to keep the old data, but I couldn't figure out how to implement it... I would have to use memcpy for every dimension except x with every possible index. For example, an array like this:

1 2 5 6
3 4 7 8

When adding a 4th dimension, would need to copy the positions (0,0,0), (0,1,0), (0,0,1), and (0,1,1) to keep those values. How would I do this?

Almost forgot, the code vvv
http://pastebin.com/LXCruUNi
Last edited on
I only took a peek at the source so I can't really be of big help.

What I did notice though is that your memcpy clone is bad.

See problems here:

1
2
3
4
5
6
		static void* memcpy(void* dst,const void* src,unsigned len) const{  // void pointers?  wtf?
			unsigned x=0;
			while(x<len){
				src[x]=dst[x];  // illegal, can't dereference a void pointer
				++x;}   // also, you probably meant dst[x] = src[x];
			return dst;}


Basically you just shouldn't use void pointers. Even if the above code compiled (which I'm sure it wouldn't because of the illegal dereference), you would have problems due to you not knowing the type. For example, what if you try to memcpy a complex type, like a string? *Explode*

void pointers are bad.

Of course you could always just use std::copy instead of memcpy or a memcpy clone.
Last edited on
Whoops =P and OK.

I also think I've figured out how to do it... bad thing is that it requires a quadruple-nested for loop, so there's probably a better algorithm for this. I'll post it when I'm done.
Blaargh, just realized that my solution wouldn't work...
Topic archived. No new replies allowed.