Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a pointer's address in an unsigned int in C

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);
}
like image 413
Anthony C. Avatar asked Apr 29 '12 00:04

Anthony C.


People also ask

How are unsigned integers stored in C?

In C programming language, unsigned data type is one of the type modifiers which are used for altering the data storage of a data type. In C, usually, we have integer (int) data type by default are signed where it can store values both negative and positive values.

How do you store the address of a pointer in C?

A pointer is a special kind of variable. Pointers are designed for storing memory address i.e. the address of another variable. Declaring a pointer is the same as declaring a normal variable except you stick an asterisk '*' in front of the variables identifier.

Are addresses signed or unsigned?

See (1.) Absolute addresses are unsigned and relative addresses (branches) are signed, to be able to jump backwards.

Is a pointer an unsigned int?

int * and unsigned int * are two different pointer types that are not compatible types. They are also pointers to incompatible types.


1 Answers

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
like image 105
MByD Avatar answered Sep 20 '22 11:09

MByD