【问题标题】:Accessing struct field gives address rather than value访问结构字段给出地址而不是值
【发布时间】:2015-03-09 15:05:28
【问题描述】:

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

链表的完整代码;

h 文件

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 文件

#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 指针。我哪里做错了?

【问题讨论】:

  • 如何初始化列表?你怎么知道startrollovers 是地址?请提供更多信息或MCVE
  • 您的list_t 看起来不像一个链接列表。它看起来像一个动态分配的固定大小元素数组。
  • 推荐大家看看linux内核中链表是如何实现的。这非常简洁。你说这是一个链表实现,为什么我看不到任何next 类型的指针?
  • @zoska 我正在调试器中运行代码。调试器允许我查看变量的地址和它们的值。奇怪的是,我评估了观察窗口中的表达式,它们运行良好。但是运行代码会得到不同的结果。
  • 这看起来像一个数组,而不是一个链表。在链表中,每个元素都有指向下一个元素的指针。您是否记得将p_list-&gt;el_size 设置为sizeof stopwatch(并为p_values 分配内存)?

标签: c pointers void-pointers


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    相关资源
    最近更新 更多