Storing a pointer's address in an unsigned int

2020-04-06 23:43发布

问题:

Is it possible to cast a pointer to an unsigned int, then later cast it back to a pointer? I'm trying to store the pointer to a struct in a pthread_t variable, but I can't seem to get it to work. Here's some snippets of my code (I'm creating a user-level thread management library). When I try to print out the tid of the thread it gives me some long garbage number.

Edit: Never mind, I got it to work.

I changed

thread = (pthread_t) currentThread;

to

*thread = (pthread_t) currentThread;

Figured it was something stupid like that.


Test program:

pthread_t thread1;
pthread_t thread2;

pthread_create(&thread1, NULL, runner, NULL);
pthread_create(&thread2, NULL, runner, NULL);
pthread_join(&thread2, NULL);

My library:

typedef struct queueItem
{
    int tid;
    ucontext_t context;

    int caller;

    struct queueItem *joiningOn;
    struct queueItem *nextContext;
} queueItem;

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
{
    thread = (pthread_t) currentThread;
}

...

int pthread_join(pthread_t thread, void **retval)
{
    queueItem *t = (queueItem *) thread;

    if(runningContext->joiningOn != NULL) // Current thread is already waiting on another
        return EINVAL;
    if(t == NULL) // If thread to join on is invalid
        return 0;

    fprintf(stdout, "JOINEE: %d\n", t->tid); // Prints weird number

    runningContext->caller = JOIN;
    runningContext->joiningOn = t;
    swapcontext(&(runningContext->context), &scheduleContext);
}

回答1:

Sure it's possible, if you make sure your unsigned int is the same size as a void* on your system.

If you have some code that's not working, post it.

Edit: You should read about intptr_t, e.g. here: Why / when to use `intptr_t` for type-casting in C?



回答2:

No. On many systems pointer type is bigger than int type. If you have a problem to use pthread_t, ask about it, int is not the answer.

For example, on my machine, the following code:

#include <stdio.h>

int main() {
        printf("unsigned int = %lu\n", sizeof(unsigned int));
        printf("pointer = %lu\n", sizeof(void*));
        return 0;
}

outputs:

unsigned int = 4
pointer = 8