Thread: Double pointer to int for a dynamic array

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    18

    Double pointer to int for a dynamic array

    Dear C experts,

    The below program compiles without any error and prints values as expected.

    However, dumps core. I could do this without dumping core on a int *i, however, int **i is giving a core dump.

    I would much appreciate if anyone could provide their views on dynamic allocation of such arrays.

    The same thing on a char with same sizeof(int) works without a core dump.

    Thanks
    Nethaji

    Code:
    #include <stdio.h>
    #include <malloc.h>
    int main()
    {
     int **i, j, k;
     i = calloc(10, sizeof(int));
    
     for(j = 0; j < 10; j++)
       {
        i[j] = calloc(30, sizeof(int));
       }
    
     for(j = 0; j < 10; j++)
       {
        for(k = 0; k < 30; k++)
          {
    printf("%d == ", k);
           i[j][k] = (k + 10);
    printf("%d\n", i[j][k]);
          }
       }
    
     /*Free memory:*/
     for(j = 0; j < 10; j++)
       {
        free(i[j]);
       }
     free(i);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > i = calloc(10, sizeof(int));
    This should be sizeof(int*)

    Also, malloc/calloc are defined properly in stdlib.h
    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.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Depending on your system, "sizeof(int)" may be different to "sizeof(int*)". In the way you've implemented it, "i" is an array of pointers to int, and every one of those pointers is an array of ints, I'm talking about line #6 btw.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    18
    Dear GReaper and Salem,

    Thank you both for your views.

    Salem, I tried your sizeof(int*) statement and the program does not dump core now. Everything is fine.

    Could you please explain what difference it makes in the allocation of memory.

    Example:
    int *i; is fine with sizeof(int)
    while int **i; is fine with sizeof(int*)

    The reason is my search to find the answer returned that the difference is sizeof(int) is 4 bytes while sizeof(int*) is 8 bytes.

    Why does a simple int needs 8 bytes of memory to store in a double pointer for each of the pointers?

    Your time is much appreciated.

    Many thanks
    Nethaji

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    18
    Dear GReaper and Salem,

    Just an update. The sizeof(int*) is needed only for the allocation call on line number 6, but not for the line 10. However, having sizeof(int*) for line 6 and 10 does not harm the output.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's important to get the right size of the thing you're allocating.

    If you always use this form
    p = malloc( n * sizeof(*p) );
    you can't go wrong.

    > Why does a simple int needs 8 bytes of memory to store in a double pointer for each of the pointers?
    Because the first thing you allocate is an array of pointers (type int*).

    Then each of those pointers in turn points to an array of int (type int).

    Your original program crashed because (as you've found out) on your machine, pointers are bigger than ints, so it ran off the end of the memory allocated.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2017
    Posts
    18
    Thank you very much for the explanation Salem.
    Best regards
    Nethaji

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Pointer, dynamic allocation help
    By Trey Brumley in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2013, 07:35 AM
  2. can anybody give me an example on a double pointer array?
    By torquemada in forum C Programming
    Replies: 4
    Last Post: 10-22-2011, 05:25 AM
  3. Which do you prefer, dynamic array or double linked list?
    By ITAmember in forum C Programming
    Replies: 14
    Last Post: 06-03-2009, 01:46 AM
  4. double pointer to 2-dim array?
    By vex_helix in forum C Programming
    Replies: 2
    Last Post: 04-06-2004, 12:31 AM
  5. Dynamic double array
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2002, 08:02 PM

Tags for this Thread