Why use double indirection? or Why use pointers to pointers?

前端 未结 18 2062
失恋的感觉
失恋的感觉 2020-11-22 10:37

When should a double indirection be used in C? Can anyone explain with a example?

What I know is that a double indirection is a pointer to a pointer. Why would I ne

18条回答
  •  失恋的感觉
    2020-11-22 11:42

    Pointers to pointers also come in handy as "handles" to memory where you want to pass around a "handle" between functions to re-locatable memory. That basically means that the function can change the memory that is being pointed to by the pointer inside the handle variable, and every function or object that is using the handle will properly point to the newly relocated (or allocated) memory. Libraries like to-do this with "opaque" data-types, that is data-types were you don't have to worry about what they're doing with the memory being pointed do, you simply pass around the "handle" between the functions of the library to perform some operations on that memory ... the library functions can be allocating and de-allocating the memory under-the-hood without you having to explicitly worry about the process of memory management or where the handle is pointing.

    For instance:

    #include 
    
    typedef unsigned char** handle_type;
    
    //some data_structure that the library functions would work with
    typedef struct 
    {
        int data_a;
        int data_b;
        int data_c;
    } LIB_OBJECT;
    
    handle_type lib_create_handle()
    {
        //initialize the handle with some memory that points to and array of 10 LIB_OBJECTs
        handle_type handle = malloc(sizeof(handle_type));
        *handle = malloc(sizeof(LIB_OBJECT) * 10);
    
        return handle;
    }
    
    void lib_func_a(handle_type handle) { /*does something with array of LIB_OBJECTs*/ }
    
    void lib_func_b(handle_type handle)
    {
        //does something that takes input LIB_OBJECTs and makes more of them, so has to
        //reallocate memory for the new objects that will be created
    
        //first re-allocate the memory somewhere else with more slots, but don't destroy the
        //currently allocated slots
        *handle = realloc(*handle, sizeof(LIB_OBJECT) * 20);
    
        //...do some operation on the new memory and return
    }
    
    void lib_func_c(handle_type handle) { /*does something else to array of LIB_OBJECTs*/ }
    
    void lib_free_handle(handle_type handle) 
    {
        free(*handle);
        free(handle); 
    }
    
    
    int main()
    {
        //create a "handle" to some memory that the library functions can use
        handle_type my_handle = lib_create_handle();
    
        //do something with that memory
        lib_func_a(my_handle);
    
        //do something else with the handle that will make it point somewhere else
        //but that's invisible to us from the standpoint of the calling the function and
        //working with the handle
        lib_func_b(my_handle); 
    
        //do something with new memory chunk, but you don't have to think about the fact
        //that the memory has moved under the hood ... it's still pointed to by the "handle"
        lib_func_c(my_handle);
    
        //deallocate the handle
        lib_free_handle(my_handle);
    
        return 0;
    }
    

    Hope this helps,

    Jason

提交回复
热议问题