Thread: ** - Pointer to a pointer

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    104

    ** - Pointer to a pointer

    Hello,

    I am new to using Pointer to pointers rather than 2 dimensional arrays, but it is about time i needed dynamic memory.

    Anyways, I typedef-ed a struct as follows:
    Code:
    typedef struct {
        float **vertex;
        float **face;
    } MODEL;
    And I have a function that is called from main() which takes a 'MODEL' pointer as parameters. I need to allocate memory for it to be similar to, if using arrays, array[3][valuesRecieve], and here is what I did:
    Code:
     
        /* I want vertex to be [3][vertices]*/
       *(m->vertex) = malloc(sizeof(float)*3);
        m->vertex = malloc(sizeof(float)*vertices);
    
        /* I want face to be [2][faces]*/
        *(m->face) = malloc(sizeof(float)*2);
        m->face = malloc(sizeof(float)*faces);
    But the program encounters error and exits. Any idea why?

    PS: 'm' is the pointer to a MODEL in the parameters of the function.

    Thank you,
    abraham2119

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since you have a fixed bound in both cases, it would be simpler to write:
    Code:
    typedef struct {
        float *vertex[3];
        float *face[2];
    } Model;
    Note that I renamed MODEL to Model since fully capitalised names are reserved for macros by convention.

    Now, to dynamically allocate each of the inner arrays you might write something along the lines of:
    Code:
    m->vertex[0] = malloc(sizeof(*m->vertex[0]) * num_vertices);
    The problem that you faced is partially due to allocating the wrong amount of space due to taking the sizeof the wrong thing, and partially due to doing things in the wrong order.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by laserlight View Post
    Since you have a fixed bound in both cases, it would be simpler to write:
    Code:
    typedef struct {
        float *vertex[3];
        float *face[2];
    } Model;
    Note that I renamed MODEL to Model since fully capitalised names are reserved for macros by convention.

    Now, to dynamically allocate each of the inner arrays you might write something along the lines of:
    Code:
    m->vertex[0] = malloc(sizeof(*m->vertex[0]) * num_vertices);
    The problem that you faced is partially due to allocating the wrong amount of space due to taking the sizeof the wrong thing, and partially due to doing things in the wrong order.
    Thank you very much, but I think I did not express myself correctly.

    I want to have an array(which size is dynamic) of float[3] for vertex and an array(which size is dynamic) of float[2] for face.

    I think what you are giving me is the opposite, is it not?
    Last edited by abraham2119; 01-16-2009 at 02:43 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by abraham2119
    I want to have an array(which size is dynamic) of float[3] for vertex and an array(which size is dynamic) of float[2] for face.

    I think what you are giving me is the opposite, is it not?
    Yes, but you can change it to:
    Code:
    typedef struct {
        float (*vertex)[3];
        float (*face)[2];
    } Model;
    Likewise, the code for dynamic allocation would change to:
    Code:
    m->vertex = malloc(sizeof(*m->vertex) * num_vertices);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by laserlight View Post
    Yes, but you can change it to:
    Code:
    typedef struct {
        float (*vertex)[3];
        float (*face)[2];
    } Model;
    Likewise, the code for dynamic allocation would change to:
    Code:
    m->vertex = malloc(sizeof(*m->vertex) * num_vertices);
    Thank you very much, but why is it that my code did not work?

    And can you explain the logic of how (*vertex)[3] is the opposite of *vertex[3]?

    Thank you!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by abraham2119
    Thank you very much, but why is it that my code did not work?
    • You dereference m->vertex before initialising it by writing *(m->vertex)
    • You did not allocate memory for all the inner arrays
    • You used sizeof(float) when sizeof(float*) was appropriate (though they might be equal, in which case it would merely be semantically incorrect)
    • The same goes for m->face.


    Quote Originally Posted by abraham2119
    And can you explain the logic of how (*vertex)[3] is the opposite of *vertex[3]?
    The syntax just means: "vertex is a pointer to an array of 3 floats" as opposed to "vertex is an array of 3 pointers to float".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM