访问struct字段给出地址而不是值

时间:2015-03-09 15:05:28

标签: c pointers void-pointers

我有一些嵌入式c代码,它使用void指针和指针算法来实现通用链表。我正在使用该列表来存储stopwatch_t类型的结构。我遇到的问题是,当我从列表中提取项目并将它们从void指针转换为stopwatch_t指针时,它们的行为并不像预期的那样。当我尝试访问结构的字段时,我得到的是地址而不是值。有关详细信息,请参阅下面代码中的最后一条评论。

完整的链接列表代码;

h file

typedef uint16_t list_index_t;

typedef struct
{
    void *              p_values;
    uint16_t *      p_links;
    uint16_t            el_size;
    uint16_t            list_size;
    list_index_t    first;
    list_index_t    last;
    bool                    empty;
} list_t;

typedef struct 
{   
    list_t * p_list;
    bool before_first;  
    list_index_t current;
} list_enumerator_t;


#define LIST_INIT(P_LIST, EL_SIZE, LIST_SIZE)                                                      \
        do                                                                                                                                                                                       \
        {                                                                                                                                                                                            \
                static uint8_t values[LIST_SIZE*EL_SIZE];                                                                                                  \
              static uint16_t links[LIST_SIZE];                                                                                                              \
              static list_t list;                                                                               \
                uint32_t err = list_init(&list, values, links, EL_SIZE, LIST_SIZE);                                      \
                APP_ERROR_CHECK(err);                                                                                                                                          \
                (*P_LIST) = &list;                                                                         \
        } while (0);    

c file

#include <string.h>
#include "compiler_abstraction.h"
#include "list.h"

static __INLINE list_index_t get_successor(list_t * p_list, list_index_t current);
static __INLINE void set_successor(list_t * p_list, list_index_t index, list_index_t successor_index);
static __INLINE list_index_t get_predecessor(list_t * p_list, list_index_t target);
static __INLINE void * get_value(list_t * p_list, list_index_t index);

uint32_t list_init( list_t *    p_list, 
                                        void * p_values, 
                                        uint16_t * p_links, 
                                        uint16_t    el_size, 
                                        uint16_t    list_size)
{
    p_list->p_values = p_values;
    p_list->p_links = p_links;
    p_list->el_size = el_size;
    p_list->list_size = list_size;
    p_list->first = 0;
    p_list->last = 0;
    p_list->empty = true;

    // link all nodes
    for (uint16_t i = 0; i < (list_size - 1); i++)
    {
        p_links[i] = i+1;
    }
    p_links[list_size-1] = 0;

    return NRF_SUCCESS;
}

bool list_empty(list_t * p_list)
{
    return (p_list->empty);
}

bool list_full(list_t * p_list)
{
    list_index_t first = p_list->first;
    list_index_t last = p_list->last;
    list_index_t after_last = get_successor(p_list, last);
    return (first == after_last);
}

// Precondition: !list_full(p_list)
list_index_t list_insert(list_t * p_list, void * p_value)
{
    // determine where new value will go
    list_index_t new_index = 
        p_list->empty ? 
            p_list->last : 
            get_successor(p_list, p_list->last);

    // copy value into list
    void * p_new_value = get_value(p_list, new_index);
    memcpy(p_new_value, p_value, p_list->el_size);

    // update links
    p_list->last = new_index;

    p_list->empty = false;

    return new_index;
}

void * list_lookup(list_t * p_list, list_index_t index)
{
    return get_value(p_list, index);
}

// Precondition: !list_empty(p_list)
void list_delete(list_t * p_list, list_index_t del)
{
    // update empty status
    // note: the only valid way to get an empty list is to delete an
    //       item from a list with only one item.
    p_list->empty = p_list->first == p_list->last;

    // delete items from beginning of list
    if (p_list->first == del)
    {
        p_list->first = get_successor(p_list, del); 
    }

    // delete item from end of list
    else if (p_list->last == del)
    {
        p_list->last = get_predecessor(p_list, del);
    }

    // delete item from middle of list
    else
    {
        // remove node from chain
        list_index_t before_del = get_predecessor(p_list, del);
        list_index_t after_del = get_successor(p_list, del);
        set_successor(p_list, before_del, after_del);

        // insert node back into chain after last
        list_index_t after_last = get_successor(p_list, p_list->last);
        set_successor(p_list, del, after_last);
        set_successor(p_list, p_list->last, del);       
    }
}

static __INLINE list_index_t get_predecessor(list_t * p_list, list_index_t target)
{
    // start at first populated index
    list_index_t x = p_list->first;
    list_index_t y = get_successor(p_list, x);
    while (y != target)
    {
        x = y;
        y = get_successor(p_list, y);
    }
    return x;
}

static __INLINE list_index_t get_successor(list_t * p_list, list_index_t current)
{
    return (p_list->p_links)[current];
}

static __INLINE void set_successor(list_t * p_list, list_index_t index, list_index_t successor_index)
{
    (p_list->p_links)[index] = successor_index;
}

static __INLINE void * get_value(list_t * p_list, list_index_t index)
{
    return (void *)
        ((uint32_t)(p_list->p_values) + (index * p_list->el_size));
}

list_enumerator_t list_enumerate(list_t * p_list)
{
    list_enumerator_t enumerator;
    enumerator.before_first = true;
    enumerator.p_list = p_list;
    return enumerator;
}

bool enumerator_move_next(list_enumerator_t * p_enumerator)
{
    bool result;
    list_t * p_list = p_enumerator->p_list;

    if (p_enumerator->before_first)
    {
        p_enumerator->current = p_list->first;
        p_enumerator->before_first = false;
        result = !list_empty(p_list);
    }
    else if (p_enumerator->current == p_list->last)
    {
        result = false;
    }
    else
    {
        p_enumerator->current = get_successor(p_list, p_enumerator->current);
        result = true;
    }
    return result;
}

void * enumerator_current(list_enumerator_t * p_enumerator)
{
    list_t * p_list = p_enumerator->p_list;
    list_index_t index = p_enumerator->current;
    return get_value(p_list, index);
}

秒表的相关部分;

static list_t * p_slots;

typedef struct
{
    uint16_t start;
    uint32_t rollovers;
} stopwatch_t;

uint32_t stopwatch_init(void)
{
    stopwatch_t stopwatch;
    LIST_INIT(&p_slots, sizeof stopwatch, MAX_STOPWATCHES);
    ...
    return OK;
}

stopwatch_id_t stopwatch_start(void)
{
    stopwatch_t stopwatch;
    stopwatch.start = timer1_now();
    stopwatch.rollovers = 0;
    ...
    stopwatch_id_t id = list_insert(p_slots, (void *)(&stopwatch));
    ...
    return id;
}

uint64_t stopwatch_get_elapsed_time(stopwatch_id_t id)
{
    ...
    stopwatch_t * p_stopwatch = list_lookup(p_slots, (list_index_t)id);
    uint16_t start_ticks = (p_stopwatch->start);
    uint32_t rollovers = (p_stopwatch->rollovers);
    /* PROBLEM: variables 'start_ticks' & 'rollovers' now contain the addresses */
    /*          of the values I want, rather than the values themselves.        */
    ...
}

这是我第一次使用void指针。我哪里出错了?

1 个答案:

答案 0 :(得分:1)

问题与使用指针或指针算法无关。事实证明,函数stopwatch_get_elapsed_time(..)的返回表达式存在错误。此错误意味着未使用变量start_ticksrollovers。据推测,编译器已经意识到变量没有被使用并且将它们优化掉。 这解释了为什么变量在调试器中观察时显示奇怪的值。