Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C -- (void*) to int

Tags:

c

pointers

queue

I'm implementing a simple priority queue in C for a kernel and so I can't use any standard libraries. 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*. I'm having trouble converting this data to lets say an int when I pop the data off the stack.

//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;
}
like image 800
Jamie Curtis Avatar asked Feb 26 '23 02:02

Jamie Curtis


2 Answers

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; you are storing the address of a local variable.

I would suggest changing void* data to 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_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.

like image 156
Marlon Avatar answered Feb 27 '23 17:02

Marlon


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. As the data is no longer pointing to your int, it probably looks like a memory address.

like image 45
Reed Copsey Avatar answered Feb 27 '23 17:02

Reed Copsey