Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: pointer = &membuff[-2]; |
| Date | 2011-06-10 08:10 -0700 |
| Organization | None to speak of |
| Message-ID | <ln1uz1bu38.fsf@nuthaus.mib.org> (permalink) |
| References | <isrcm7$q94$1@speranza.aioe.org> <ln8vtabj64.fsf@nuthaus.mib.org> <isstm2$p9k$1@dont-email.me> |
Noob <root@127.0.0.1> writes:
> Keith Thompson wrote:
>> Converting the offset value from unsigned int to int is probably
>> cleaner:
>>
>> int offset = n1 - n2;
>> sp = (struct dummy *) &membuff[offset];
>
> Cleaner, perhaps, but it has implementation-defined behavior
> when n1 < n2
>
> "When [...] an unsigned integer is converted to its corresponding
> signed integer, if the value cannot be represented the result is
> implementation-defined."
D'oh!. You're right -- and for reasons that I emphasized in the same
article.
I actually can't think of a good clean way to compute the difference
between two unsigned int values as a signed int. You can convert
both values to int:
int offset = (int)n1 - (int)n2;
but that potentially fails if either value exceeds INT_MAX. Or you can
write:
int offset = (n1 >= n2) ? (n1 - n2) : -(n2 - n1);
(the equivalent if statement might be more legible), but that's ugly.
You could convert to a wider type, but there's no guarantee that there
is a wider type.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
pointer = &membuff[-2]; "Bo S." <bo@boo.invalid> - 2011-06-09 21:07 +0000
Re: pointer = &membuff[-2]; Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-09 16:47 -0600
Re: pointer = &membuff[-2]; BGB <cr88192@hotmail.com> - 2011-06-09 22:59 -0700
Re: pointer = &membuff[-2]; pete <pfiland@mindspring.com> - 2011-06-10 06:28 -0400
Re: pointer = &membuff[-2]; Keith Thompson <kst-u@mib.org> - 2011-06-09 17:54 -0700
Re: pointer = &membuff[-2]; Noob <root@127.0.0.1> - 2011-06-10 13:03 +0200
Re: pointer = &membuff[-2]; Keith Thompson <kst-u@mib.org> - 2011-06-10 08:10 -0700
Re: pointer = &membuff[-2]; Keith Thompson <kst-u@mib.org> - 2011-06-10 08:12 -0700
Re: pointer = &membuff[-2]; Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-09 21:02 -0400
Re: pointer = &membuff[-2]; James Kuyper <jameskuyper@verizon.net> - 2011-06-09 22:25 -0400
Re: pointer = &membuff[-2]; Shao Miller <sha0.miller@gmail.com> - 2011-06-10 01:10 -0500
Re: pointer = &membuff[-2]; Shao Miller <sha0.miller@gmail.com> - 2011-06-10 01:13 -0500
Re: pointer = &membuff[-2]; Shao Miller <sha0.miller@gmail.com> - 2011-06-10 01:20 -0500
Re: pointer = &membuff[-2]; Shao Miller <sha0.miller@gmail.com> - 2011-06-10 03:33 -0500
Re: pointer = &membuff[-2]; James Kuyper <jameskuyper@verizon.net> - 2011-06-10 08:23 -0400
Re: pointer = &membuff[-2]; Sherm Pendley <sherm.pendley@gmail.com> - 2011-06-10 09:30 -0400
Re: pointer = &membuff[-2]; ImpalerCore <jadill33@gmail.com> - 2011-06-10 07:15 -0700
csiph-web