Re: dynamically allocate array variable of type LPWSTR
In article <ln63n5xo05.fsf @nuthaus.mib.or g>, Keith Thompson
<kst-u@mib.orgwrote:
You snipped the context. This is going in circles now :)
My point was that if the expression being assigned to is more unweildy
than the type name itself, one might choose to use sizeof(type) rather
than sizeof *expr_being_ass igned_to. In that case, casting the result of
malloc to type* ensures that the type being assigned to is compatible with
the type you're allocating for. Thus, if you had
foo [i]->bar->baz [j]->boo [k]->blarg =
malloc( n * sizeof *foo [i]->bar->baz [j]->boo [k]->blarg );
you might prefer to just name the type of blarg, let's say int* here
foo [i]->bar->baz [j]->boo [k]->blarg = malloc( n * sizeof (int) );
but if it really was long* , you'd have an undiagnosed error. But if you
also cast the result
foo [i]->bar->baz [j]->boo [k]->blarg = (int*) malloc( n * sizeof (int) );
you'd get a compile error if it wasn't an int*. Yes, you could mistakenly
cast it to a long* and use sizeof(int), introducing an error anyway, but
the lesser distance between the inconsistencies is such that it can be
easier to track down. It's only a few characters, while the distance
between a mismatch in the second example that lacks the cast might be
hundreds of lines, or span files.
In article <ln63n5xo05.fsf @nuthaus.mib.or g>, Keith Thompson
<kst-u@mib.orgwrote:
blargg.h4g@gish puppy.com (blargg) writes:
[...]
>
long *p = malloc(n * sizeof *p); // no error for the compiler to catch
[...]
And while we're on the topic, using a cast is actually a GOOD idea if one
is naming the type in sizeof, since it will catch the error of the
destination type being different than you expect:
long* p = malloc( n * sizeof (int) ); // oops, but no error
long* p = (int*) malloc( n * sizeof (int) ); // compiler catches it
is naming the type in sizeof, since it will catch the error of the
destination type being different than you expect:
long* p = malloc( n * sizeof (int) ); // oops, but no error
long* p = (int*) malloc( n * sizeof (int) ); // compiler catches it
long *p = malloc(n * sizeof *p); // no error for the compiler to catch
My point was that if the expression being assigned to is more unweildy
than the type name itself, one might choose to use sizeof(type) rather
than sizeof *expr_being_ass igned_to. In that case, casting the result of
malloc to type* ensures that the type being assigned to is compatible with
the type you're allocating for. Thus, if you had
foo [i]->bar->baz [j]->boo [k]->blarg =
malloc( n * sizeof *foo [i]->bar->baz [j]->boo [k]->blarg );
you might prefer to just name the type of blarg, let's say int* here
foo [i]->bar->baz [j]->boo [k]->blarg = malloc( n * sizeof (int) );
but if it really was long* , you'd have an undiagnosed error. But if you
also cast the result
foo [i]->bar->baz [j]->boo [k]->blarg = (int*) malloc( n * sizeof (int) );
you'd get a compile error if it wasn't an int*. Yes, you could mistakenly
cast it to a long* and use sizeof(int), introducing an error anyway, but
the lesser distance between the inconsistencies is such that it can be
easier to track down. It's only a few characters, while the distance
between a mismatch in the second example that lacks the cast might be
hundreds of lines, or span files.
Comment