warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

44,364
(int *) client_sockfd

client_sockfd is not a pointer. It's an int, which is not the same size as an int *. And it's telling you that.

The last argument to pthread_create() is a void * and is meant to be a pointer to data that you want to pass into that particular thread. It appears you're trying to convert the integer value of client_sockfd to a pointer and pass that. That's generally not something you would do, but if you really want to and avoid the warning then you need to use something that is the same size as a pointer, which is what intptr_t gives you. More than likely int is 4 bytes and intptr_t (and void *) is eight bytes on your system, but that is platform dependent. While you can safely go from 32->64->32 bits, the compiler is warning you that you have different sizes.

Share:
44,364
user1097772
Author by

user1097772

"I know one thing, that I know nothing" Socrates

Updated on February 13, 2020

Comments

  • user1097772
    user1097772 about 4 years

    part of my code:

    int server_sockfd, client_sockfd; //socket file descriptors of client and server
    
    // ... some code 
    
    if(pthread_create(&vlakno, NULL, handle_client, (int *) client_sockfd) != 0) {
                perror("Error while creating thread.");
            }
    

    I'm getting "warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]"

    prototype of my function:

    void *handle_client(void *args);
    

    I found this question:
    link

    The first answer said something like that he should use intptr_t instead of int.
    I have two questions:
    What is difference between int and intptr_t in my case?
    What should I do?

    I have 2 ideas:
    1st: (changing type of file descriptors)

     int server_sockfd, client_sockfd; //socket file descriptors of client and server
    
        // ... some code 
    
        if(pthread_create(&vlakno, NULL, handle_client, (intptr_t *) client_sockfd) != 0) {
                    perror("Error while creating thread.");
                }
    

    or 2nd idea: (changing type only in casting function pthread_create)

    intptr_t server_sockfd, client_sockfd; //socket file descriptors of client and server
    
            // ... some code 
    
            if(pthread_create(&vlakno, NULL, handle_client, (int *) client_sockfd) != 0) {
                        perror("Error while creating thread.");
                    }
    

    EDIT:
    in function handle_client i want to do this:

    int clientSocket;
    clientSocket = (int)args;
    

    I'm really sorry to user cnicar or something like that .. he unfortunatelly deleted his answer but it was ok.
    His solution was use (void *), it firstly casted same same error but it was caused probably bad behaviour of eclipse :( So message for him:
    Ok thanks it looks thats fine now ... Eclipse still throwed this warning but when I turned it on and off twice afer edit it fine with your edit :) thanks a lot