Salem;
I've tried this on my code and it seems to work perfectly. That's a really nice way of doing things in the 4D case; nice work. You don't know how much time you've just saved me. Thanks a million.
Salem;
I've tried this on my code and it seems to work perfectly. That's a really nice way of doing things in the 4D case; nice work. You don't know how much time you've just saved me. Thanks a million.
I don't think any of the code here can be called nice, really.
Actually, I'd do stuff like this like so:
I hope I made no mistakes.Code:typedef struct { float *data; int w, x, y, z; } FARRAY_4D; #define FA4D_ALLOC(ar, pw, px, py, pz) { \ (ar).w = (pw); (ar).x = (px); (ar).y = (py); (ar).z = (pz); \ (ar).data = malloc(sizeof(float) * (ar).w * (ar).x * (ar).y * (ar).z); \ } #define FA4D_MEM(ar) (ar).data #define FA4D_ACCESS(ar, pw, px, py, pz) \ (*((ar).data + ((pw) * (ar).x * (ar).y * (ar).z) + \ ((px) * (ar).y * (ar).z) + ((py) * (ar).z) + (pz))) #define FA4D_FREE(ar) free((ar).data)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Ugh!
Macro mayhem!
*shudder*
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Well, I'd still prefer it to that heap of pointers you had
And of couse, I prefer C++![]()
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
> Well, I'd still prefer it to that heap of pointers you had
Yeah, but at least I still get to use [index] notation for all my array accesses.
This is but just one way of allocating multiple dimensional arrays.
What would you do if you wanted a sparse array?
> And of couse, I prefer C++
Which means you can overload operator [] and make the whole mess go away in whatever way you choose, and still have a nice comfy [index] notation feeling.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Exactly.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Yes, but it doing it the way you've suggested dosn't give them what they first implied. It was my understanding that they wanted:Originally Posted by Salem
So that every single allocated block falls one right after the other. This would only work with one single malloc call. Otherwise:Code:char ****magical4Darray; char * walker; magical4Darray = magicallocation( d1, d2, d3, d4 ); walker = magical4Darray[0][0][0][0]; displayAddressof( walker ); /* say it's 0x00000001 */ displayAddressof( walker + 1 ); /* say it's 0x00000002 */ .... all the way through the very last element ... displayAddressof( walker + whatever ); /* 0x00001234 */
If we were to print the addresses of...Code:int ****create_a_foo ( int max_x, int max_y, int max_r, int max_c ) { int ****all_x = MY_MALLOC( max_x * sizeof *all_x ); int ***all_y = MY_MALLOC( max_x * max_y * sizeof *all_y ); int **all_r = MY_MALLOC( max_x * max_y * max_r * sizeof *all_r ); int *all_c = MY_MALLOC( max_x * max_y * max_r * max_c * sizeof *all_c ); int ****result = all_x; int x, y, r;
They implied at least that they wanted every single block to fall one right after another. However, with more than one malloc call, you're not guarinteed to get this. Everything you get with that one call will fall one after another, however, the next call you make could jump to any place where there's enough free memory to do what you want.Code:displayAddressof( all_x ); /* 0x00000001 */ ... sequentially ... displayAddressof( all_x + putusatlastx ); /* 0x00000045 */ displayAddressof( all_y ); /* could very well start at... 0x12340000 */ ... sequentially ... displayAddressof( all_y + putusatlasty ); /* 0x12340678 */ ...and so on...
I agree that your method is the way to go, however, it's not what they stated they wanted.
Quzah.
Hope is the first step on the road to disappointment.
>> int *all_c = MY_MALLOC( max_x * max_y * max_r * max_c * sizeof *all_c );
All the int's of the matrix are allocated by that single call to malloc - making them contiguous.
gg
Why are you bumping a 4-month old thread?
If you understand what you're doing, you're not learning anything.
Holy smokes!! My bad.
I was reading:
http://cboard.cprogramming.com/showthread.php?t=65819
which has a link to this thread - then something about Star Wars came on TV (so I had watch) - came back and posted a reply
gg