Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Learn how to fix error: cast from pointer to integer of different size
Next
Answered
Noa O'Neill
  • 29
Noa O'Neill
Asked: May 18, 20222022-05-18T18:49:15+00:00 2022-05-18T18:49:15+00:00In: c

Learn how to fix error: cast from pointer to integer of different size

  • 29

. Advertisement .

..3..

. Advertisement .

..4..

I am tired of fixing the problem: cast from pointer to integer of different size in the c; even if I get the reference from another forum, it still returns an error:

error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]

To identify the problem, I will show you the detail here:

void* foo(void *dst, ...) {
  // some code
  unsigned int blkLen = sizeof(int); // this line ok.
  unsigned int offset = (unsigned int) dst % blkLen; // warning here!
  // some code cont...
 }
#if __linux__ // or #if __GNUC__
  #if __x86_64__ || __ppc64__
  #define ENVIRONMENT64
  #else
  #define ENVIRONMENT32
  #endif
 #else
  #if _WIN32
  #define ENVIRONMENT32
  #else
  #define ENVIRONMENT64
  #endif
 #endif // __linux__
 
 #ifdef ENVIRONMENT64
  #define MAX_BLOCK_SIZE unsigned long long int
 #else
  #define MAX_BLOCK_SIZE unsigned long int
 #endif // ENVIRONMENT64
unsigned int offset = (MAX_BLOCK_SIZE) dst % blkLen;

How do I do that? Could you support me in improving this problem?

pointers
  • 2 2 Answers
  • 75 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-13T12:36:28+00:00Added an answer on June 13, 2022 at 12:36 pm

    The cause: The compiler is warning you because it thinks you’re trying to round-trip a pointer through int and back. This was typical practice prior to the introduction of 64-bit computers, however it is neither secure nor sensible.

    The solution: You’ll need to include stdint.h or inttypes.h to have uintptr_t available.

    unsigned int offset = (uintptr_t) dst % blkLen;
    • 8
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Noah Martins
    2022-05-25T20:48:47+00:00Added an answer on May 25, 2022 at 8:48 pm

    Problem is, converting void* pointser to unsigned int is not portable.

    This is just one aspect of the problem. This part of the problem can easily be solved using uintptr_t. It is a type that has been defined in <stdint.h> or <inttypes.h>. uintptr_t is wide enough to ensure that you can convert a void* into uintptr_t and back again to get the original pointer value or at least one that is equal to the original. A type intptr_t is also available, which is signed. Usually unsigned types are more appropriate for this type of thing. uintptr_t and intptr_t cannot be guaranteed to exist but should exist on any implementation (C99 or later), that uses the appropriate integer types.

    Even if your integer type is large enough to hold a converted pointeder, it doesn’t necessarily mean that the result will be meaningful for any other purpose than converting back into a pointer.

    In a footnote non-normative to the C standard, it states that:

    The mapping functions for converting a pointer to an integer or an
    integer to a pointer are intended to be consistent with the addressing
    structure of the execution environment.

    This is useless unless you know the addressing structure.

    It seems that you are trying to figure out the offset of void* relative to blkLen‘s next lower multiple. In other words, you want to find out how the pointer value is in relation to blkLen-sized blocks.

    It’s okay to decide that it’s sensible to do on your system . However, you need to be aware that operations on integers resulting in pointer conversions are not portable.

    One concrete example: I have worked on Cray vector machines, where a void* refers to a 64 bit machine address (which points at a 64 bit word), with a 3-bit offset added by software to the high order 3 bits. Simply copy the representation to convert a pointer into an integer. If this representation is not taken into consideration, any integer arithmetic using such an integer will likely yield meaningless results.

    Conclusions:

    1. uintptr_t is a better choice than using preprocessor tricks to determine the type of integer you can use. Your compiler’s implementer has already determined an integer type that can safely store a converted pointer value. This is the only way to go. (Caveat – <stdint.h> was introduced to C in 1999 by the ISO standard. You might need to use #ifdef hacks if you are stuck with an old compiler that doesn’t implement it. If uintptr_t is available, I recommend it. __STDC_VERSION__ >= 199901L can be used to check for C99 conformance. However, some compilers may not fully support C99.

    2. Converting a pointer into an integer and manipulating its value are not possible. This doesn’t mean you shouldn’t try it. C has the ability to support nonportable codes when you need it.

    • 5
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.