TinyURL widget - shorten your URL's for free!

Enter a long URL to make tiny:

Monday, May 25, 2015

How to point to a statically compiled variable aiming at dynamic heap object with double indirection and get the value not the address: use a surrogate local variable



typedef struct list_t
{
    //! \var OpenGL-friendly current item count as large integer
    GLuint item_count;   
    //! \var OpenGL-friendly current size_t of total list.
    GLsizei current_max_size;
    //! \var growable - the type of list: growing or not ; in most cases we want growing
    char growable;
    //! \var items  generic variable items without type
    void **items;
    //! \var names variable "items without type"s' names
    char **names;   
}list_t;


void * generic_create_object()
{
    int ** p = malloc (sizeof(int **));
   
    return p;
}
     
Say you statically create a local variable  at compile time

    list_t  test_list;

then make dynamic variables on the heap and aim them into the static structure
 int ** c = generic_create_object();   
        list_add_item(&test_list, c, name);

 How does one get the pointed to value in test_list.items[i]? Not the address?

If you call test_list.items[i] you will get the address.



Simple. make a temp variable in the right syntax and equivalence operation it.

 int ** ptr = test_list.items[i];

printf("list index:%d addr:%u value: %u\n", i, &(test_list.items[i]), **(ptr)  );

Now the pointed to pointed to value will appear properly and the dereference   next to the address will get the right address value.

You can do it by making a temp variable in a local loop like


    int i = 0;
    for (i = 0; i < test_list.item_count;  i ++ )
    {
        int ** ptr = test_list.items[i];
        printf("list index:%d addr:%u value: %u\n", i, &(test_list.items[i]), **(ptr)  );
        if ((i > 5) && (**ptr != 13))
            return -1; // failed to have the right value
    }
and then point at it properly to get the value not the reference. It will be a stack register type cast in the proper variable. It can then be optimized and the compiler will probably remove it.


Complete code:

/***\file test-list.c   *********************************************************
                          
 H.File   $Header:  $
    
 Date Started: -2015

 Date:     $Date:  $
 *
 Author:  $Name:   $

 Purpose:  The list "object" for object-oriented C programming.

 Version:  $Id: doublerow.h,v 1.2 2007/08/24 14:27:29 cvsuser Exp $

 Revision: $Revision:  $
 *
 *
 Log:   $Log:   $
***************************************************************************/

 /*
 * For information on the open science consortium, go to
 *
 * openscienceiscool.blogspot.ca/2014/12/open-science-consortium-beginning.html
 *
 * */

#include "list.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

// This test program is for the underlying list items that are used by the object data ..
// The goal is to understand how this works and then how the second tier functions use it.
/*!
 * typedef struct list_t
{
    //! \var OpenGL-friendly current item count as large integer
    GLuint item_count;   
    //! \var OpenGL-friendly current size_t of total list.
    GLsizei current_max_size;
    //! \var growable - the type of list: growing or not ; in most cases we want growing
    char growable;
    //! \var items  generic variable items without type
    void **items;
    //! \var names variable "items without type"s' names
    char **names;   
}list_t;
 * */

void * generic_create_object()
{
    int ** p = malloc (sizeof(int **));
   
    return p;
}

#define DE
int main (void)
{
    list_t  test_list;
   
    char * name;
   
    int *     pointer;
    int *     pointer2;   
    int      a = 5;
    int     bb = 13;
   
    pointer = &a;
    pointer2 = &bb;   
    name = malloc (sizeof(char)*20);
   
    strcpy( name, "TEST\n");
       
    // makes a list of 10 items  to start that is growable            
    list_make(&test_list, 10, '1');
   
    int ** b = generic_create_object();
    int ** c = generic_create_object();   
    int ** d = generic_create_object();   
   
    *b = pointer;
   
     *c = *d = pointer2;
   
    // add items to the list.
    list_add_item(&test_list, b, name);
    list_add_item(&test_list, b, name);   
    list_add_item(&test_list, b, name);       
    list_add_item(&test_list, c, name);       
    list_add_item(&test_list, c, name);       
    list_add_item(&test_list, d, name);       
    list_add_item(&test_list, d, name);       
    list_add_item(&test_list, d, name);       
    list_add_item(&test_list, d, name);       
    list_add_item(&test_list, d, name);       
    // test the grow function
    list_add_item(&test_list, d, name);           
   
                       
    printf( "number of max items count: %d \n", test_list.current_max_size);   
    // show all the indices
    list_print_indices(&test_list);
    //
    printf( "number of list items count: %d \n", test_list.item_count);
    //    simple print out
    list_print_items(&test_list);
   
    // print out all values
    int i = 0;
    for (i = 0; i < test_list.item_count;  i ++ )
    {
        int ** ptr = test_list.items[i];
        printf("list index:%d addr:%u value: %u\n", i, &(test_list.items[i]), **(ptr)  );
        if ((i > 5) && (**ptr != 13))
            return -1; // failed to have the right value
    }
   
   
    // testing the delete functions
    list_delete_all(&test_list);
    // succeed and return 0 success other wise failed beforehand with a 1
    return 0;
}