Re: Difference between variables and functions

From: Barry Margolin (barmar_at_alum.mit.edu)
Date: 08/30/05

  • Next message: Keith Thompson: "Re: Difference between variables and functions"
    Date: Mon, 29 Aug 2005 23:49:01 -0400
    
    

    In article <3nfsn0F1a6vtU2@individual.net>,
     Ulrich Hobelmann <u.hobelmann@web.de> wrote:

    > Barry Margolin wrote:
    > > In article <3nem0nF15bjvU1@individual.net>,
    > > Ulrich Hobelmann <u.hobelmann@web.de> wrote:
    > >
    > >> But C has first class functions, so you can store functions in variables
    > >> and use them as parameters or return them from functions. So I'd say
    > >> that calling the int variable "read" with three parameters should be
    > >> valid C, just not meaningful.
    > >
    > > The only variables that can be used this way are those of type pointer
    > > to function. Since the variable "read" is not of this type, it's not
    > > valid to use it in a function call context.
    >
    > Wow, I've never experienced C being that anal about types (except when
    > it doesn't know the size of a struct that it needs for allocation). I
    > assumed that it's all just memory addresses underneath, with some loose
    > checking on top.

    The analness is implementation-dependent. The language specification
    says that calling a non-function has undefined consequences, which means
    implementations can do anything. So some implementations with minimal
    type checking will allow it through (and hopefully you'll get a
    segmentation violation or something like that at runtime), while others
    will do the additional checking and report the error at compile time.
    Sounds like the OP was using one of the latter types of compilers.

    In general, over the years C compilers have gotten a bit better at this
    type of thing. In the early days of C, computers didn't have much
    memory and the compilers were deliberately kept simple. Now disk and
    memory are cheap, and bugs that could have been detected at compile time
    can result in security holes, so it's more common to do as much checking
    as the language allows.

    -- 
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    

  • Next message: Keith Thompson: "Re: Difference between variables and functions"

    Relevant Pages

    • Re: How to avoid stack overflow in C????
      ... but when i assign same memory at runtime through calloc, ... Most implementations have a limit on how much memory is available for ... Your compiler /may/ complain at compile time, ... Whenever you have to create large objects, use malloc. ...
      (comp.lang.c)
    • Re: "Sorting" assignment
      ... place depending on implementations, including implementations in which ... out on the systems and compilers you use". ... bus error doing int-sized XOR swaps on unaligned addresses. ... But computational complexity theory does. ...
      (comp.programming)
    • Re: interpreter vs. compiled
      ... a memory address to successive integers 0..9, ... Is there a reason why you're expecting c.l.p to be your personal tutor ... for Introduction to Compilers? ... Defining your own terms that don't ...
      (comp.lang.python)
    • Re: R: GOBACK (was: Perform Thru/Go to vs. Perform - Compile Speed
      ... >> one paragraph name to ENTRY, or breaking the program into smaller ones. ... >> what it looks like in the multithreading demo I just posted. ... All compilers will have the equivalent of reentrant, ... not sharing memory with other threads. ...
      (comp.lang.cobol)
    • Re: C Primitive Data Type Sizes
      ... >>implementations of some language which looks vaguely like C which do ... There may be implementations of some language which looks vaguely ... There are plenty of compilers which manage to implement C90. ... > complete C99 compiler systems in the world. ...
      (comp.lang.c)