Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Function factories |
| Date | 2012-08-23 21:30 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <k16lft$dgl$1@dont-email.me> (permalink) |
| References | (4 earlier) <k157iq$vet$1@speranza.aioe.org> <503648B2.5000200@verizon.net> <k15nmc$a02$1@speranza.aioe.org> <k15ovj$d9h$1@speranza.aioe.org> <a0b04a35-e217-4e68-b95f-e3d92168ba71@13g2000vbf.googlegroups.com> |
On 8/23/2012 9:07 PM, eq mail wrote:
> On 23/08/2012, 20:24, jacob navia wrote:
>> Le 23/08/12 19:02, Chicken McNuggets a crit :
>>> This code on the other hand compiles with no warnings:
>>
>>> #include <stdlib.h>
>>
>>> int main(void)
>>> {
>>> int *ptr = (int*) malloc(sizeof(long));
>>> *ptr = 10;
>>> short blah = *ptr;
>>> free(ptr);
>>> exit(EXIT_SUCCESS);
>>> }
>>
>>> so please explain how C is a type safe language?
>>
>> Because you can assign a 64 bit integer into a short.
>
> Is that really an explanation for how C is a type safe language?
>
>> If there is an overflow the behavior is undefined
>> but it can't be any overflow in your example since
>> 10 will fit into a short.
>
> Integer conversions, such as the one above, do not yield undefined
> behaviour.
The program is (technically) at risk of undefined behavior
on any machine where sizeof(int) > sizeof(long). No such machine
has ever been rumored, but would be within the realm of possibility
for a C implementation: int might be 40 bits wide (including 15
padding bits) while long is 32 with no padding.
Also, there's the possibility that malloc() returns NULL.
But turning to the conversion: Yes, it's well-defined -- But
that's only because the value 10 is within both the range of int
and the range of short. Change the 10 to 123456789101112 and you
get quite a different picture.
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-21 20:36 +0000
Re: Function factories Nobody <nobody@nowhere.com> - 2012-08-21 21:55 +0100
Re: Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-22 16:36 +0000
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 13:01 -0400
Re: Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-22 18:33 +0000
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 22:26 -0400
Re: Function factories David Brown <david@westcontrol.removethisbit.com> - 2012-08-22 08:34 +0200
Re: Function factories Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-08-22 01:52 -0700
Re: Function factories David Brown <david@westcontrol.removethisbit.com> - 2012-08-22 11:34 +0200
Re: Function factories Gareth Owen <gwowen@gmail.com> - 2012-08-24 17:30 +0100
Re: Function factories Gareth Owen <gwowen@gmail.com> - 2012-08-24 17:29 +0100
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-22 12:32 +0200
Re: Function factories tom st denis <tom@iahu.ca> - 2012-08-22 07:40 -0700
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 11:05 -0400
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 11:08 +0200
Re: Function factories Chicken McNuggets <chicken@mcnuggets.com> - 2012-08-23 13:27 +0100
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 11:13 -0400
Re: Function factories "Heinrich Wolf" <invalid@invalid.invalid> - 2012-08-23 18:18 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 12:40 -0400
Re: Function factories Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2012-08-23 09:29 -0700
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 18:44 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 12:46 -0400
Re: Function factories Chicken McNuggets <chicken@mcnuggets.com> - 2012-08-23 18:02 +0100
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 19:24 +0200
Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 18:07 -0700
Re: Function factories Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-23 21:30 -0400
Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 21:46 -0700
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 13:35 -0400
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 20:13 +0200
Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 17:57 -0700
Re: Function factories Andrew Cooper <amc96@cam.ac.uk> - 2012-08-25 02:35 +0100
Re: Function factories Melzzzzz <mel@zzzzz.invalid> - 2012-08-23 21:21 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 15:36 -0400
Re: Function factories Melzzzzz <mel@zzzzz.invalid> - 2012-08-23 22:09 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 16:20 -0400
Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-23 22:33 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 17:07 -0400
Re: Function factories Keith Thompson <kst-u@mib.org> - 2012-08-23 14:51 -0700
Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-24 00:09 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 19:01 -0400
Re: Function factories Keith Thompson <kst-u@mib.org> - 2012-08-23 16:31 -0700
Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-24 18:31 +0200
Re: Function factories Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-08-24 02:37 -0700
csiph-web