Thread: Dynamic, multi-dimensional pointers!

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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