Questions about pointers and malloc

This is a discussion on Questions about pointers and malloc within the C Programming forums, part of the General Programming Boards category; If I have Code: char **array = malloc (sizeof *array); What have I created? What's array pointing to? If I ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    Questions about pointers and malloc

    If I have
    Code:
    char **array = malloc (sizeof *array);
    What have I created? What's array pointing to?


    If I write
    Code:
    size_t row; /* defined before calling malloc */
    size_t size; /* defined later before calling malloc */
    .
    .
    .
    array[row] = malloc (size);
    .
    .
    .
    ++row;
    How many array[row] = malloc (size); can I write, given that row is increased each time? Is it correct to do that?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by H`eya View Post
    What have I created? What's array pointing to?
    A pointer to a pointer. Of whatever type "array" belongs.

    How many array[row] = malloc (size); can I write, given that row is increased each time? Is it correct to do that?
    This looks like something you might use this way:
    Code:
    int *array[10];
    for (row=0;row<10;row++) 
       array[row] = malloc(SIZE*sizeof(int))
    in which case you would have an array of ten pointers to arrays of SIZE ints.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Appreciate your prompt response.
    Allow me to explain further then, since I think I did not give you enough to clarify my concerns.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void )
    {
        size_t i;
        size_t row = 5, size = 32;
        char **array = malloc(sizeof *array);
        if (!array) {
            /* handle the error */
        }
    
        /*
         * This is the part I am interested in
         * knowing if it would be correct
         */
        for (i = 0, i < row, i++) {
            *array[i] = malloc(size); /* does **array have memory for *array[i]? */
            if (*array == NULL) {
               /* handle the error */ 
            }
        }
    
        return 0;
    }
    I apologize that I did not explain well in my previous post.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    malloc gives you the amount of memory asked for. You ask for enough memory to hold one of "* array" things. So you have enough room for one, not 5.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Many thanks, to both of you.
    I understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem - malloc, pointers and vector
    By DrSnuggles in forum C++ Programming
    Replies: 18
    Last Post: 08-14-2009, 10:28 AM
  2. Pointers + Malloc = Painloc
    By Chalks in forum C Programming
    Replies: 9
    Last Post: 10-19-2008, 01:10 PM
  3. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  4. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 11:05 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21