Thread: malloc pointer to pointer

  1. #1
    Registered User kunalnandi's Avatar
    Join Date
    Nov 2007
    Posts
    9

    Question malloc pointer to pointer

    how to allocate memory to any "pointer to pointer" variable using malloc.
    eg:
    Code:
    char** ch;

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Like you allocate memory for anything using malloc().

    Code:
        ch = malloc(whatever_number_of_pointers_required * sizeof(char *));
    or, idiomatically, using the fact that sizeof() evaluates the size of its argument, but does not actually evaluate the argument.
    Code:
           ch = malloc(whatever_number_of_pointers_required * sizeof(*ch));
    This allocates a number of pointers to char. Those pointers, if you wish to dereference them, also need to point to something valid. If you want to dynamically allocate each pointer to char as well (which is one way of making the pointers point at something valid) then you might do this.
    Code:
           ch = malloc(whatever_number_of_pointers_required * sizeof(*ch));
           if (ch != NULL)
           {
                  int i;
                  for (i = 0; i < whatever_number_of_pointers_required; ++i)
                  {
                      ch[i] = malloc(whatever_number_of_chars_required_for_i * sizeof(ch[i]));
    
                        /*  assume whatever_number_of_chars_required_for_i is at least 2 */
    
                      if (ch[i] != NULL)  strcpy(ch[i], "A");
                   }
           }
    It is necessary to #include <stdlib.h> in order to properly use malloc(), and <string.h> in order to use strcpy().

    The other thing to remember with malloc() is that it is good practice to match every call of malloc() with a call of free(), when your program no longer needs the memory allocated.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    grumpy, could you also use ch[i] = malloc(whatever_number_of_chars_required_for_i * (sizeof(ch*))
    in place of ch[i] = malloc(whatever_number_of_chars_required_for_i * sizeof(ch[i]))

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by camel-man View Post
    grumpy, could you also use ch[i] = malloc(whatever_number_of_chars_required_for_i * (sizeof(ch*))
    Not quite. It would be ch[i] = malloc(whatever_number_of_chars_required_for_i * (sizeof(*ch[i])). (I left out an asterix in my previous post)

    Equivalently the sizeof(ch[i]) expression may be replaced with sizeof(**ch).

    The basic idiomatic technique is that whatever = malloc(number_required * sizeof(*whatever)).

    Yes, it is true that sizeof(whatever[i]) and sizeof(*whatever) are always equivalent (assuming i has integral type). That is because, when evaluating sizeof(expression), the compiler only considers the TYPE of expression, not its value. So sizeof(whatever[i]) does NOT actually evaluate the values of either i or of whatever[i].
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-10-2010, 05:09 PM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. malloc pointer question..
    By transgalactic2 in forum C Programming
    Replies: 13
    Last Post: 10-21-2008, 06:03 AM
  4. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  5. quick malloc/pointer Q
    By Jase in forum Linux Programming
    Replies: 7
    Last Post: 05-25-2003, 11:05 PM