Thread: Let's allocate a moving mesh...

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    Let's allocate a moving mesh...

    Hello C Forum,

    I'm trying to build a moving mesh. What's a moving mesh, you ask. Brilliant, it's a mesh that rearranges the grid points to have a higher density of points in a localized region. So let's let talk about my node structure:

    Code:
    struct node {
    
       struct particle *parts;
    
    
       double p[3];
       double *spacing; //0 and 1 : negative and positive z
                          //2 and 3 : " " " x
                          //4 and 5 : " " " y
    };
    As you can see, my nodes need positions (for particles) and there's a spacing which governs the distance between each node in all 6 possible Cartesian directions.

    And let's look at my allocation procedure:

    Code:
    #define gl 128#define len 635
    struct node*** build_mesh(void) {
    
       struct node ***mesh;
       mesh = malloc(gl*sizeof(*mesh));
    
    
       for (int i=0; i<gl; i++) {
    
    
          mesh[i] = malloc(gl*sizeof(*mesh[i]));
          for (int j=0; j<gl; j++) {
    
    
             mesh[i][j] = malloc(gl*sizeof(*mesh[i][j]));
             for (int k=0; k<gl; k++) {
    
    
                double *soft;
                soft = malloc(6*sizeof(*soft));
                for (int x=0; x<6; x++) {
    
    
                   soft[x] = len/(gl-1);
                }
    
    
                mesh[i][j][k].spacing = soft;
    
    
                mesh[i][j][k].p[0] = k*len/(gl-1);
                mesh[i][j][k].p[1] = j*len/(gl-1);
                mesh[i][j][k].p[2] = i*len/(gl-1);
                //printf("%e, %e, %e\n", mesh[i][j][k].p[0], mesh[i][j][k].p[1], mesh[i][j][k].p[2]); 
             }
          }
       }
    
    
       return mesh;
    }
    
    
    void free_mesh(struct node ***mesh) {
    
    
       for (int i=0; i<gl; i++) {
          for (int j=0; j<gl; j++) {
             for (int k=0; k<gl; k++) {
    
    
                free(mesh[i][j][k].spacing);
             }
             free(mesh[i][j]);
          }
          free(mesh[i]);
       }
    
    
       free(mesh);
    }
    This code seems to allocate a 3D grid with proper positions and spacings but I'm unsure if I'm allocating this correctly. I have to dynamically allocate the spacing because I was getting stack overflows or whatever error you get when the stack size has been exceeded. Basically, this is eating up a crap ton of memory and I want to be sure that I'm doing this the way it's supposed to be done.

    Edit: Note that this code, when run, eats up ~1.85 GB of RAM so be careful if you try to make it go. I could just lower the size but I have plenty to spare.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    I don't see the need to dynamically allocate the spacing array. if that causes stack overflows you have a problem somewhere that is not shown in this code. you must have a big array of nodes on the stack somewhere.
    other than that, it appears to be right as far as allocating a 3d array.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Edit: Note that this code, when run, eats up ~1.85 GB of RAM
    Are you running this on a 32-bit or 64-bit machine?

    If it's 32-bit, then it can easily run out of space.
    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. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    No, I'm using 64 bit Arch and I have 8 GB total so I'm not worried about anything like that.

    I will say one thing though, my grid is 128^3 elements and each grid element has a spacing array. So that's like 2 million times the size of a 6 element 1d array. I don't know if that'll exceed the stack when put in combination with the other static allocations but I was very surprised that my spacing array didn't work unless it was dynamically allocated.

    I have no leaks so I'm managing my memory well but it's just that I don't think I should have to manually manage this.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    don't use the stack for the grid. way too big. but 128^3 is only 2,097,152 x 72 bytes for your nodes = 150,994,944 bytes. nothing close to 1.8 GB. if you cannot have the 4 doubles for spacing in your node structure, and the whole thing is dynamically allocated, then something is wrong in your code.
    edit: put together a simple test program with just a main routine and a call to your grid allocation function. see if that 'works'.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Actually, you just have to add 128^3*size of a double and add it to get the rest. That comes from my spacing array. The actual allocation should be like 1.5 but because I allocate out the spacing array manually I think it counts for the total.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm not sure what your question is then (apart from not liking having to do it).

    > double p[3];
    > double *spacing; //0 and 1 : negative and positive z
    It seems odd that you have an array of 3 doubles, but you go to the trouble of allocating a pointer to 6 doubles (with all the attendant overheads of that), especially when 6 is compile-time constant.
    You may as well have
    double p[3];
    double spacing[6];
    and be done with it.


    There is one short-cut, if you know all the minor dimensions of your array are constants, which seems to be the case here.

    You can do this
    Code:
       struct node (*mesh)[gl][gl];
       mesh = malloc(gl*sizeof(*mesh));
       for (int i=0; i<gl; i++) {
          for (int j=0; j<gl; j++) {
             for (int k=0; k<gl; k++) {
                for (int x=0; x<6; x++) {
                   mesh[i][j][k].spacing[x] = len/(gl-1);
                }
                mesh[i][j][k].p[0] = k*len/(gl-1);
                mesh[i][j][k].p[1] = j*len/(gl-1);
                mesh[i][j][k].p[2] = i*len/(gl-1);
                //printf("%e, %e, %e\n", mesh[i][j][k].p[0], mesh[i][j][k].p[1], mesh[i][j][k].p[2]);
             }
          }
       }
    You're gonna love this...
    Code:
    void free_mesh(struct node (*mesh)[gl][gl] ) {
       free(mesh);
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With Terrain Mesh Collisions
    By DCLXVI47 in forum Game Programming
    Replies: 7
    Last Post: 06-16-2012, 04:17 AM
  2. need help with D3DXVec3TransformCoord on second mesh
    By Anddos in forum Game Programming
    Replies: 5
    Last Post: 02-17-2008, 01:54 PM
  3. making a mesh have collision ,
    By Anddos in forum Game Programming
    Replies: 16
    Last Post: 10-04-2007, 11:16 PM
  4. Mesh Release() Problem
    By Morgul in forum Game Programming
    Replies: 6
    Last Post: 03-07-2006, 02:50 PM
  5. a mesh tool for directx
    By darcome in forum Game Programming
    Replies: 2
    Last Post: 03-11-2003, 06:27 AM