views:

100

answers:

5
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...

+2  A: 

C supports implicit casting, and you will also get a warning if size_t is less precise than CGSize for some reason.

maat
+4  A: 

You should use the proper type, which is probably CGFloat. size_t is something int'ish and inappropriate.

Eiko
Depends on what you're using it for. For one example, `CGBitmapContextCreate` takes a `size_t` for its width parameter.
Peter Hosey
+1  A: 

A cast is usually not needed (and sometimes wrong). C does the "right thing" most of the time.

In your case, the cast is not needed (but not wrong, merely redundant).

You need to cast

  • arguments to printf when they don't match the conversion specification
    printf("%p\n", (void*)some_pointer)
  • arguments to is*() (isdigit, isblank, ...) and toupper() and tolower()
    if (isxdigit((unsigned char)ch)) { /* ... */ }
  • (If I remember more, I'll add them here)
pmg
You should cast the argument of `is*/to*` to `(unsigned char)`, not plain `(unsigned)`.
schot
Thanks schot, changed
pmg
+2  A: 

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?

Steve Jessop
Actually, it's a CGFloat. On the iphone that means itr is *currently* a float but Apple may choose to make it something else at a later date (they have already changed it on the Mac OS X run time).).
JeremyP
@JeremyP: fair enough.
Steve Jessop
I guess I'll just use a CGFloat instead. The goal of this is to get an integer with no fractional digits.
openfrog
openfrog: Then you should use an integral type. `CGFloat` is, by definition, a floating-point type. `size_t` is appropriate if you're going to, say, pass it as the `width` parameter to `CGBitmapContextCreate` (which is typed as a `size_t`, which means the conversion *will* happen there if not earlier).
Peter Hosey
Steve Jessop: I can't speak for the questioner (who is targeting the iPhone), but on the Mac, an NSImage object returns its size in points (1/72 inch), not pixels. (Indeed, it may not even have pixels—an NSImage can be a vector image or even one that draws itself upon demand in Objective-C code.) As such, an image measured in cm, mm, meters, or pixels (the last when the resolution is not 72) may very well be a non-integral number of points.
Peter Hosey
It's easy to think of cases where the value is fractional - make sure you don't crash in that case just because for example a buffer you create is suddenly too small. Maybe look for the correct pixel value or take the scale factor into account.
Eiko
+1  A: 

The size_t type is for something completely different, you should not use it for such purposes.
Its purpose is to express the sizes of different types in memory. For example, the sizeof(int) is of type size_t and it returns the size of the int type.

As the others suggested, use the appropriate type for that variable.

Venemo