Thread: Malloc and pointers

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question Malloc and pointers

    I got the following code:
    Code:
    #define COLS 5
    
    typedef int RowArray[COLS];
    RowArray *rptr;
    
    int main(void)
    {
        int nrows = 10;
        int row, col;
        rptr = malloc(nrows * COLS * sizeof(int));
        for (row = 0; row < nrows; row++)
        {
            for (col = 0; col < COLS; col++)
            {
                rptr[row][col] = 17;
            }
        }
    
        return 0;
    }
    I am confused on this line: rptr = malloc(nrows * COLS * sizeof(int));

    Wouldnt that just create a like a long memory storage space? How does the compiler know that it has rows and cols??

    thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "Wouldnt that just create a like a long memory storage space?"

    It would create a space 200 bytes long, which is needed to store the eventual contents of *rptr.

    "How does the compiler know that it has rows and cols??"

    I don't think it matters whether the compiler can "know this" as long as it can multiply 10 x 5 x 4.

    This presumes sizeof(int) is 4, which it is on most systems.
    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
    Aug 2007
    Posts
    270
    so the compiler would know that the first 10 spaces is one row etc?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A "RowArray" here is an array of five ints. By mallocating enough space to *ptr, you can implicitly make it an array of whatever it points to -- eg, with a normal int:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    	int x, *ptr=&x, i;
    	ptr=malloc(12);
    	ptr[1]=7;
    	for (i=0;i<3;i++) printf("%d\n", ptr[i]);
    }
    So in your example, *rptr ends up as a 2 dimensional int array -- the first number is the element of the pointer array, the second the element of the array pointed to by the element of the pointer array.
    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

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    what does this mean the void:
    compare((void *)&p[j-1]

    Im confused on the void *
    Last edited by taurus; 10-25-2008 at 06:01 AM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by taurus View Post
    Why is this case dont i use ** for the pointer?
    Sorry, I should have written "*rptr effectively ends up as a 2 dimensional int array". *rptr literally is still just a pointer, but it points to a space in memory containing an array of RowArrays. A RowArray is an array of five ints.

    ** indicates a true multi-dimensional array, because it's a pointer to a pointer.
    Last edited by MK27; 10-25-2008 at 06:58 AM.
    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

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by taurus View Post
    what does this mean the void:
    compare((void *)&p[j-1]

    Im confused on the void *
    void* casts &p[j-1] as a void pointer.
    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

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Why do you do that? is it so that you can then cast back that data to whatever typ you need? something like that

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by taurus View Post
    Why do you do that? is it so that you can then cast back that data to whatever typ you need? something like that
    The casting doesn't change &p[j-1], but it does cause "compare" to consider it as a void pointer.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers + Malloc = Painloc
    By Chalks in forum C Programming
    Replies: 9
    Last Post: 10-19-2008, 01:10 PM
  2. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  3. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  4. Problems with pointers and malloc()
    By Deirdre in forum C Programming
    Replies: 3
    Last Post: 10-28-2007, 04:20 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM