Thread: Dynamic, multi-dimensional pointers!

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    207

    Dynamic, multi-dimensional pointers!

    Hi!

    I think I'm beginning to figure these pointy things out:

    Code:
    int loop;
    int x = 5;
    int y = 2;
    
    // create first subscript
    long **group_total;
    group_total = malloc (x * sizeof (long));
    
    if (!group_total)
    {
       printf ("\n\nError!  Not enough memory!\n\n");
    }
    
    //create second subscript
    for (loop = 0; loop < x; loop++)
    {
       group_total[loop] = malloc (y * sizeof (long));
    
       if (!group_total[loop])
       {
          printf ("\n\nError!  Not enough memory!\n\n");
       }
    }
    
    //free pointer
    for (loop = 0; loop < x; loop++)
    {
        free (group_total[loop]);
    }
    I was just wondering:

    1) Is this correct? It compiles and seems to run correctly, but there's no certainty there...

    2) Is this everything? ie: Do I need to free the first subscript also?
    Last edited by Lionmane; 07-05-2005 at 10:37 PM.

  2. #2
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    1) No. See second question.

    2) Your number of malloc() statements should equal your number of free() statements. So yes, after you free the second (y) subscripts, you should free the first (x). It must be after, however, for if you free the first subscript first, you will have no way to access the second subscripts and free them.
    Code:
    void function(void)
     {
      function();
     }

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    207
    Did I do anything wrong in my declaration?

    mw

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    and for the first malloc, the space to be allocated should be of type long *
    long **group_total;
    group_total = malloc (x * sizeof (long *));

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> // create first subscript
    >> long **group_total;
    >> group_total = malloc (x * sizeof (long));
    Close. What you need to create is an array of pointers to long. When you create an array of N elements, you need to allocate N times the size of each element. In this case, that's "x * sizeof(long*)".

    And as Jken Veina pointed out, you need to free that array last.

    gg

    [edit]Antigloss is a quick one

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    207
    How about this:

    Code:
    int loop, loop2;
    int x = 5;
    int y = 2;
    
    // create first subscript
    long **group_total;
    group_total = malloc (x * sizeof (long *));
    
    if (!group_total)
    {
       printf ("\n\nError!  Not enough memory!\n\n");
    }
    
    //create second subscript
    for (loop = 0; loop < x; loop++)
    {
       group_total[loop] = malloc (y * sizeof (long));
    
       if (!group_total[loop])
       {
          printf ("\n\nError!  Not enough memory!\n\n");
       }
    }
    
    //free pointer
    for (loop = 0; loop < x; loop++)
    {
        for (loop2 = 0; loop2 < y; loop2++)
        {
            free (group_total[loop][loop2]);
        }
    
        free (group_total[loop]);
    }
    Last edited by Lionmane; 07-05-2005 at 11:19 PM.

  7. #7
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    no. it should be
    Code:
    for (loop = 0; loop < x; loop++)
    {
            free (group_total[loop]);
    
    }
    free (group_total);//free the space allocated by the first malloc

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    207
    Sorry 'bout that. ONE MORE TIME... :-)

    Code:
    int loop;
    int x = 5;
    int y = 2;
    
    // create first subscript
    long **group_total;
    group_total = malloc (x * sizeof (long *));
    
    if (!group_total)
    {
       printf ("\n\nError!  Not enough memory!\n\n");
    }
    
    //create second subscript
    for (loop = 0; loop < x; loop++)
    {
       group_total[loop] = malloc (y * sizeof (long));
    
       if (!group_total[loop])
       {
          printf ("\n\nError!  Not enough memory!\n\n");
       }
    }
    
    //free second malloc
    for (loop = 0; loop < x; loop++)
    {
            free (group_total[loop]);
    }
    
    //free first malloc
    free (group_total);
    Last edited by Lionmane; 07-05-2005 at 11:20 PM.

  9. #9
    I typecast anyway.
    Join Date
    Jun 2005
    Posts
    25
    I haven't put it into my compiler to check it, but it looks like you've got the hang of it so far.

    EDIT: If you check a malloc and find there wasn't enough memory, you should probably do something about it instead of continuing to run through the program.

  10. #10
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ha! Yeah, good idea! :-)

    How do I create a 3D array by adding a "z-axis"?

    mw

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > How do I create a 3D array by adding a "z-axis"?
    Add another * to your variable - so its long ***group_total;
    Then add another loop to malloc each group_total[x][y]
    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.

  12. #12
    Registered User
    Join Date
    May 2005
    Posts
    207
    So would that be:

    Code:
    int loop, loop2;
    int x = 5;
    int y = 2;
    int z = 3;
    
    // create first subscript
    long ***group_total;
    group_total = malloc (x * sizeof (long *));
    
    if (!group_total)
    {
       printf ("\n\nError!  Not enough memory!\n\n");
       exit (1);
    }
    //create second subscript
    for (loop = 0; loop < x; loop++)
    {
       group_total[loop] = malloc (y * sizeof (long *));
    
       if (!group_total[loop])
       {
          printf ("\n\nError!  Not enough memory!\n\n");
          exit (1)
       }
    }
    
    //create third subscript
    for (loop = 0; loop < x; loop++)
    {
       for (loop2 = 0; loop2 < y; loop2++)
       {
          group_total[loop][loop2] = malloc (z * sizeof (long));
    
          if (!group_total[loop][loop2])
         {
             printf ("\n\nError!  Not enough memory!\n\n");
             exit (1)
         }
       }
    }
    
    //free third malloc
    for (loop = 0; loop < x; loop++)
    {
        for (loop2 = 0; loop2 < y; loop2++)
        {
            free (group_total[loop][loop2]);
        }
    }
    
    //free second malloc
    for (loop = 0; loop < x; loop++)
    {
            free (group_total[loop]);
    }
    
    //free first malloc
    free (group_total);
    I changed the second malloc to:
    group_total[loop] = malloc (y * sizeof (long *));

    Is that right?

    mw
    Last edited by Lionmane; 07-06-2005 at 02:53 PM.

  13. #13
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    but the first should be malloc(x * sizeof (long **))

  14. #14
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    And, you could combine the two loops.
    Code:
    int loop, loop2;
    int x = 5;
    int y = 2;
    int z = 3;
    
    // create first subscript
    long ***group_total;
    group_total = malloc (x * sizeof (long **));
    
    if (!group_total)
     {
      printf ("\n\nError!  Not enough memory!\n\n");
      exit (1);
     }
    
    //create second subscript
    for (loop = 0; loop < x; loop++)
     {
      group_total[loop] = malloc (y * sizeof (long *));
    
      if (!group_total[loop])
       {
        printf ("\n\nError!  Not enough memory!\n\n");
        exit (1)
       }
    
      //create third subscript
      for (loop2 = 0; loop2 < y; loop2++)
       {
        group_total[loop][loop2] = malloc (z * sizeof (long));
    
        if (!group_total[loop][loop2])
         {
          printf ("\n\nError!  Not enough memory!\n\n");
          exit (1)
         }
       }
     }
    
    for (loop = 0; loop < x; loop++)
     {
      //free third malloc
      for (loop2 = 0; loop2 < y; loop2++)
        free (group_total[loop][loop2]);
    
      //free second malloc
      free (group_total[loop]);
     }
    
    //free first malloc
    free (group_total);
    Code:
    void function(void)
     {
      function();
     }

  15. #15
    Registered User
    Join Date
    May 2005
    Posts
    207
    Thanks guys! Your help has been extremely efficient!

    mw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array of pointers?
    By baniakjr in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2006, 09:46 AM
  2. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  3. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 03:21 PM