Trying to write a C snippet to store and retrieve an array of structure pointers. I have copied my snippet bellow.

What I am trying to do here-

  1. method Xos_get_cb_info_from_handle() should return the pointer to the base address of the structure localclient.exit_cb. Which seem to work.
  2. After getting this base address, I would like to index into the array.
  3. As part of this exercise temp1 should store the address of the 0'th index (first element), temp2 should store the address of 1st index (second element).

My observations are: (look at the output at the end of the code)

  1. I get the base address correct here in temp1 which is 0x40300c
  2. However, the next element (index 1) should be 0x403010. This is in fact 0x40301c. It seems like its being incremented by the size of the structure.

What is going on here? As per pointer arithmetic shouldn't C be increment this by size of structure (but by size of the pointer.)

Can anyone help me shade some light on this scenario? Any code change suggestions are welcomed. Thanks!

#include <stdio.h>

#define X_PROCESS_DUMP_CB_MAX 3

typedef struct {
  void* cb_func;             /* pointer to callback function */
  unsigned cb_arg;           /* not interpreted by the framework*/
} X_cb_data_t;

typedef struct err_cb_t{
   X_cb_data_t Xos_cb;
   unsigned short priority;
   struct err_cb_t *next;
}Xos_exit_cb_t;

struct Xos_local_client {
    int x; //just placeholder
    Xos_exit_cb_t *exit_cb[X_PROCESS_DUMP_CB_MAX];
    int y; //just placeholder
};  

/*static global to this file, init this in main before accessing it*/
static struct Xos_local_client localclient;

int Xos_get_cb_info_from_handle(int client_handle, Xos_exit_cb_t** cb_head)
{
    *cb_head = (Xos_exit_cb_t *)&localclient.exit_cb[0];
    return 0; 
}

int main()
{
    int i = 0;

    /*init localclient here*/
    localclient.x =1;
    localclient.y =2;

    for(i =0; i< X_PROCESS_DUMP_CB_MAX; i++)
    {
        Xos_exit_cb_t *exit_cb_p = (Xos_exit_cb_t *)malloc(sizeof(Xos_exit_cb_t));
        localclient.exit_cb[i] = exit_cb_p;
        printf("&exit_cb_p: %p exit_cb_p: %p\n", (unsigned int)&localclient.exit_cb[i],(unsigned int)exit_cb_p);
    }

    /*Test code that fails*/
    Xos_exit_cb_t** exit_cb_head;   /*Pointer to pointer of exit_cb*/   

    Xos_exit_cb_t **temp1;
    Xos_exit_cb_t **temp2;

    if (!Xos_get_cb_info_from_handle(3, exit_cb_head)) {

        temp1 = &(*exit_cb_head)[0];
        temp2 = &(*exit_cb_head)[1];

        printf("temp1: %p, temp2: %p\n", temp1, temp2);
    }

    return 0;
}


/*Output*/
&exit_cb_p: 0x40300c exit_cb_p: 0xd18628
&exit_cb_p: 0x403010 exit_cb_p: 0xd426c0
&exit_cb_p: 0x403014 exit_cb_p: 0xd426d8
exit_cb_head: 0x40300c, temp1: 0x40300c, temp2: 0x40301c

1 Answers

0
kent chin On

In your for loop

for(i =0; i< X_PROCESS_DUMP_CB_MAX; i++)
    {
        Xos_exit_cb_t *exit_cb_p = (Xos_exit_cb_t *)malloc(sizeof(Xos_exit_cb_t));
        localclient.exit_cb[i] = exit_cb_p;
        printf("&exit_cb_p: %p exit_cb_p: %p\n", (unsigned int)&localclient.exit_cb[i],(unsigned int)exit_cb_p);
    }

you are iterating the pointer array. So you see an increment according to pointer arithmetic (increment by pointer size)

Now assuming your code paste here missed the copy paste mentioned in the comment by @reichhart i.e.

Xos_exit_cb_t** exit_cb_head = &(localclient.exit_cb[0]);

temp1 & temp2 (of type Xos_exit_cb_t *) are actual addresses in memory where structure is allocated. Hence, base address of adjacent elements will be separated by size of structure.

Hope that helps.