简体   繁体   English

C —(void *)转换为int

[英]C — (void*) to int

I'm implementing a simple priority queue in C for a kernel and so I can't use any standard libraries. 我正在C中为内核实现一个简单的优先级队列,因此我不能使用任何标准库。 The queue holds a head node and each node points to the next in the queue. 队列中有一个头节点,每个节点都指向队列中的下一个节点。

typedef struct node node;
struct node {
    node *next;
    void *data;
};

typedef struct  {
    node *head;
    int n;
} queue;

As you can see, each node holds it data in a void*. 如您所见,每个节点将其数据保存在void *中。 I'm having trouble converting this data to lets say an int when I pop the data off the stack. 当我从堆栈中弹出数据时,我无法将这些数据转换为int。

//push data
int int_data = 100;
push(q, &int_data);
//...
//pop data
node* popped = pop(q);
int *pop_data = popped->data;
printf("pop data (100): %d\n", *pop_data);

Why can't I get the original value here? 为什么我不能在这里获得原始值? I seem to be printing a pointer value. 我似乎正在打印一个指针值。 Alternatively, is there a better way to handle this? 或者,是否有更好的方法来处理此问题?

== edit (sorry should have included these): ==编辑(抱歉应该包括这些内容):

void push(queue *q, void *data)
{
    node new;
    new.data = data;
    node *new_ptr = &new;

    if(is_empty(q))
    {
        q->head = new_ptr;
        q->n++;
        return;
    }

    int i;
    node *curr = q->head;
    for(i=0; i<q->n; i++)
    {
        curr = curr->next;
    }
    curr->next = new_ptr;
    q->n++;
}

node* pop(queue *q)
{
    node *curr = q->head;
    q->head = curr->next;
    return curr;
}

Is your code all in one function? 您的代码是一个功能吗? If not, int int_data is getting popped off the stack (not your queue, the actual stack) which is probably why you are printing garbage; 如果没有, int int_data从堆栈中弹出(而不是队列,而不是实际的堆栈),这可能是您打印垃圾的原因。 you are storing the address of a local variable. 您正在存储局部变量的地址。

I would suggest changing void* data to int data . 我建议将void* data更改为int data (If you need to, you can store an address in an int and can cast it back to a pointer later.) (如果需要,您可以将地址存储在int中,以后再将其投射回指针。)

int int_data = 100;
push(q, int_data);

node* n = pop(q);
int num = n->data;

After reviewing your code again, you have the same problem when adding a new node. 再次检查代码后,添加新节点时也会遇到相同的问题。 node new falls out of scope at the end of the function, so basically all of your nodes in your queue are pointing to invalid memory. node new在函数末尾超出范围,因此基本上您队列中的所有节点都指向无效的内存。

If the "pop" operation is in a different function: 如果“弹出”操作的功能不同:

The problem is likely because you're pushing a local variable into your queue. 该问题可能是因为您要将局部变量推入队列中。

When you go to pop, this address is no longer valid (or at least not pointing to an int value), so you're printing something strange. 当您弹出时,该地址不再有效(或至少不指向int值),因此您正在打印一些奇怪的东西。 As the data is no longer pointing to your int, it probably looks like a memory address. 由于数据不再指向您的int,它可能看起来像一个内存地址。

are you setting data = int_data (ie int --> void*) or data = &int_data (ie int* --> void *) ? 您是在设置data = int_data (即data = int_data > void *)还是data = &int_data (即int *-> void *)? In the former case, you have to write printf("pop data (100): %d\\n", pop_data); 在前一种情况下,您必须编写printf("pop data (100): %d\\n", pop_data);

You can use the glib GPOINTER_TO_INT macro: 您可以使用glib GPOINTER_TO_INT宏:

#define GPOINTER_TO_INT(p) ((gint)  (glong) (p))

But please, take note with the doc note: 但是,请注意文档说明:

YOU MAY NOT STORE POINTERS IN INTEGERS. 您可能不会在实体店中存储指针。 THIS IS NOT PORTABLE IN ANY WAY SHAPE OR FORM. 此内容不以任何形式或形式移植。 These macros ONLY allow storing integers in pointers, and only preserve 32 bits of the integer; 这些宏仅允许将整数存储在指针中,并且仅保留整数的32位。 values outside the range of a 32-bit integer will be mangled. 超出32位整数范围的值将被破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM