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 a question.

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1101071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:58:50+00:00 2026-05-17T00:58:50+00:00

size_t pixelsWidth = (size_t)bitmapSize.width; Or is it totally fine to do without the casting

  • 0
size_t pixelsWidth = (size_t)bitmapSize.width;

Or is it totally fine to do without the casting to size_t? bitmapSize is of type CGSize…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:58:51+00:00Added an answer on May 17, 2026 at 12:58 am

    In this case, the type of bitmapSize.width is CGFloat (currently float on iPhone).

    Converting from float to size_t has undefined behavior (according to the C standard – not sure whether Apple provides any further guarantees) if the value converted doesn’t fit into a size_t. When it does fit, the conversion loses the fractional part. It makes no difference whether the conversion is implicit or explicit.

    The cast is “good” in the sense that it shows, in the code, that a conversion takes place, so programmers reading the code won’t forget. It’s “bad” in the sense that it probably suppresses any warnings that the compiler would give you that this conversion is dangerous.

    So, if you’re absolutely confident that the conversion will always work then you want the cast (to suppress the warning). If you’re not confident then you don’t want the cast (and you probably should either avoid the conversion or else get confident).

    In this case, it seems likely that the conversion is safe as far as size is concerned. Assuming that your CGSize object represents the size of an actual UI element, it won’t be all that big. A comment in the code would be nice, but this is the kind of thing that programmers stop commenting after the fiftieth time, and start thinking it’s “obvious” that of course an image width fits in a size_t.

    A further question is whether the conversion is safe regarding fractions. Under what circumstances, if any, would the size be fractional?

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

Sidebar

Related Questions

Is there any way to give printf a size_t without either casting it first
I have a variable of type size_t , and I want to print it
The C standard guarantees that size_t is a type that can hold any array
matrix* make_matrix(size_t width, size_t height, size_t k, options opt){ matrix *m= malloc(sizeof(matrix)); if(m==NULL) return
I want to print out a variable of type size_t in C but it
Is size_t a companion type like size_type? What don't they standardize the companion type
In several C++ examples I see a use of the type size_t where I
Consider the following code: template<size_t head,size_t ... Dims> struct make_vec { typedef typename make_vec<Dims...>::type
const size_t size = 5; int *i = new int[size](); for (int* k =
Is size_t only in C++ standard or C standard as well? I cannot find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.