Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #24764

Re: Function factories

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Function factories
Date 2012-08-23 13:35 -0400
Organization A noiseless patient Spider
Message-ID <503669EC.90605@verizon.net> (permalink)
References (2 earlier) <58c40758-f7d1-4272-b4ce-ed5c185bc56b@p5g2000vbl.googlegroups.com> <5034F54B.9050403@verizon.net> <k157iq$vet$1@speranza.aioe.org> <503648B2.5000200@verizon.net> <k15nmc$a02$1@speranza.aioe.org>

Show all headers | View raw


On 08/23/2012 01:02 PM, Chicken McNuggets wrote:
> On 23/08/2012 16:13, James Kuyper wrote:
>> On 08/23/2012 08:27 AM, Chicken McNuggets wrote:
>>> On 22/08/2012 16:05, James Kuyper wrote:
>>>
>>>>
>>>> However, I think that compile() looks horribly type-unsafe.
>>>>
>>>
>>> Type safety in C? That's a novel concept :).
>>
>> Is it? Try compiling the following code:
>>
>> int main(void)
>> {
>> 	double d;
>> 	int *pi = &d;
>> 	return 0;
>> }
>>
> 
> Yes. It compiles with a warning as expected. Not a great example of type 
> safety.

It's a lot better than it would be if no warning were mandated. An
implementation is free to reject such code - whether or not it actually
does so is a matter of QoI, outside the scope of the standard.

> 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?

Type safety is not an absolute yes or no issue. Some languages have more
type safety than others. C has static type checking - in many different
contexts, a diagnostic is mandatory if different types are not
compatible. Implicit conversions are often safe conversions, the most
dangerous type conversions are required to be explicit (except those
pointer conversions which can be done using void* as an intermediate
type). There's plenty of room for improvement in C's type safety, but
it's still a lot better than having no type checking, or deferring type
checking until run-time (both of which I've seen in other languages).

> The fact that malloc() returns a void pointer that can be cast to any 
> type destroys any type safety C may have.

No, it doesn't. It just adds another type-unsafe mechanism to a language
that has only a moderate amount of type safety without it.

lcc-win32's compile() looks to me like it compromises type safety much
more than malloc() does, which is what prompted my original comment. A
pointer to a function that might point have the wrong function type
seems much more serious to me than a pointer to an object that might
have the wrong object type; there's a larger variety of ways you can
have a mis-match, and a correspondingly larger number of different
failure modes available. It's not good, either way.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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