Thread: 4-dimensional array contiguous allocation

  1. #16
    Registered User
    Join Date
    Feb 2005
    Posts
    7
    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.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't think any of the code here can be called nice, really.

    Actually, I'd do stuff like this like so:
    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)
    I hope I made no mistakes.
    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

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    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.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    > 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.

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem
    > Well, I'm not exactly sure what you are referring too. C provides calloc() ("contiguous allocation")
    calloc means cleared, not contiguous. calloc is just malloc and a memset - nothing more.

    Each call to malloc is guaranteed to return a pointer to a block of contiguous memory of the size specified, or NULL if there is a problem.
    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:
    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 */
    So that every single allocated block falls one right after the other. This would only work with one single malloc call. Otherwise:
    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;
    If we were to print the addresses of...
    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...
    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.

    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.

  8. #23
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> 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

  9. #24
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why are you bumping a 4-month old thread?
    If you understand what you're doing, you're not learning anything.

  10. #25
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  3. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  4. memory allocation for flexible array member of struct
    By jcarouth in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 12:19 PM
  5. 2d arrays in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-20-2002, 11:09 AM