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


Groups > comp.lang.c > #28915

Re: What's new in C?

From glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups comp.lang.c
Subject Re: What's new in C?
Date 2012-12-04 20:51 +0000
Organization Aioe.org NNTP Server
Message-ID <k9lnp9$i6m$1@speranza.aioe.org> (permalink)
References (6 earlier) <lnboeeaoro.fsf@nuthaus.mib.org> <6oednaqUAZ5VKyDNnZ2dnUVZ_qadnZ2d@supernews.com> <lnehj6qkah.fsf@nuthaus.mib.org> <Vs-dnda_D-OXUCDNnZ2dnUVZ_rednZ2d@supernews.com> <0.7b0242de075459b63082.20121204125043GMT.874nk20yn0.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> Cal Dershowitz <cal@example.invalid> writes:

(snip)
>> sure.  I can think of fortran, where one has allocatable.  
>> I can think of perl, where it happens automagically.

>> I've read more of chuck's realloc posts than I can shake a stick at.

>> What functionality would vla's provide over k&r2 C?
 
> There are two things involved in what goes by the shorthand of VLA: the
> objects themselves and their types.  What most people call "a VLA" is a
> variable length array created using a declaration involving what is
> called a variably modified type, like this:
 
>  void f(unsigned sz)
>  {
>      int data[sz];
>      ...
>  }
 
> This is not possible K&R2 C (which is basically ANSI C, or C90).

Well, in C90 you just say:

   int *data=malloc(sz*sizeof(*data));

(and don't forget to free() it later.)

One advantage is that it is much easier to read in VLA form,
the other is the compiler has to remember to free it, not you.
(Even if you put a return in the middle of the function.)
 
> The discussion you commented on was about whether 'data' must be
> allocated on "the stack" and the answer is no -- all that's required by
> the language is that 'data' be created when the declaration is
> encountered (executed if you like) and that it is destroyed when f
> returns.  In fact, even that's not required.  The program must behave as
> if that's what's happening, but a clever compiler might be able to do
> something else.

Isn't it allocated on function entry? So, any values needed for the 
dimensions must be known at entry.

(PL/I has BEGIN blocks such that you can allocate inside a procedure,
but C doesn't have those.)
 
(snip on malloc() allocation of VLAs.)

> The other great boon of these types is when writing functions.  In C99
> you can write:
 
>  void f(unsigned n, unsigned m, int matrix[n][m])
>  {
>      ...
>  }
 
> and you can then access the elements of 'matrix' using plain array
> indexing notation (matrix[i][j] for example).  The first size, n, is not
> really important because arrays are passed as pointers: you could just
> as well have written either of

This has been in Fortran since Fortran 66. There was no dynamic
allocation in Fortran until Fortran 90, but variable dimensions like
this were allowed for subroutine and function arguments.
 
>  void f(unsigned n, unsigned m, int matrix[][m])
>  void f(unsigned n, unsigned m, int (*matrix)[m])

In Fortran 66, it was common to use 1 for the rightmost
subscript (Fortran is column major) assuming no bounds
checking. Fortran 77 added the * for assumed size.

It was usual, then, to pass to a called routine both the
allocated size and the actual size. One might, for example, 
declare a 10 by 10 matrix, but only store the data for
a 5 by 5 matrix. 
 
> but the second size, m, is a great help.

It is. Well, reasonably common in C was instead to allocate
an array of pointers and pass that. Then the called routine
could index through the pointer array. It isn't so obvious
which is faster on either traditional or current processors.

In mixed Fortran/C code, though, the array of pointers is
pretty inconvenient.
 
> The object you pass (as a pointer) to such a function does not need to
> be a VLA -- you can call f in any of these ways:
 
>  int mat[3][3] = {...};
>  f(3, 3, mat);
  
>  int (*mptr)[m] = malloc(n * sizeof *mptr);
>  /* fill array */
>  f(n, m, mptr);
 
>  int vmat[n][m];
>  /* fill array */
>  f(n, m, vmat);

All these happen natually if a pointer to the array is passed.
 
> The latest standard, C11, has made VLA's optional.  I think this is a
> shame, but I think it is particularly a shame that the *types* have been
> made optional, because they are useful way beyond the automatic
> allocation of variable length arrays.  If it was the existence of
> environments in which such an allocation is not convenient, then only
> the array declarations should have been removed from C11.

So, compilers can claim to follow both C11 and C01, such that
users know they have features from both standards.

-- glen
 

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


Thread

What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-11-28 19:41 -0700
  Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-11-28 19:15 -0800
    Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-06 02:43 -0700
      Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-06 07:45 -0500
  Re: What's new in C? Noob <root@127.0.0.1> - 2012-11-29 10:36 +0100
    Re: What's new in C? Weland <weland@sdf.lonestar.org> - 2012-11-29 10:17 +0000
    Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-04 01:45 -0700
  Re: What's new in C? Weland <weland@sdf.lonestar.org> - 2012-11-29 09:48 +0000
    Re: What's new in C? David Brown <david@westcontrol.removethisbit.com> - 2012-11-29 13:50 +0100
  Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-11-29 05:55 -0800
    Re: What's new in C? Les Cargill <lcargill99@comcast.com> - 2012-11-29 12:48 -0600
      Re: What's new in C? Rui Maciel <rui.maciel@gmail.com> - 2012-11-30 10:46 +0000
        Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-11-30 05:31 -0800
          Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-11-30 14:22 +0000
          Re: What's new in C? Rui Maciel <rui.maciel@gmail.com> - 2012-11-30 15:04 +0000
            Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-30 16:41 +0000
              Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-11-30 11:49 -0500
              Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-11-30 10:12 -0800
              Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-11-30 11:12 -0800
                Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-30 22:26 +0000
                Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-11-30 22:36 +0000
                Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-30 23:27 +0000
                Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-04 01:39 -0700
                Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-04 00:43 -0800
                Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-04 03:14 -0700
                Re: What's new in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-04 12:50 +0000
                Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-12-04 05:54 -0800
                Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-04 20:51 +0000
                Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-04 16:09 -0500
                Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-04 16:19 -0500
                Re: What's new in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-04 21:45 +0000
                Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-05 01:13 +0000
                Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-04 20:36 -0500
                Re: What's new in C? Robert Wessel <robertwessel2@yahoo.com> - 2012-12-04 20:26 -0600
                Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-04 18:48 -0800
                Re: What's new in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-05 02:57 +0000
                Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-05 01:42 +0000
                Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-06 02:06 -0700
                Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-06 07:50 -0500
                Re: What's new in C? JohnF <john@please.see.sig.for.email.com> - 2012-12-06 13:46 +0000
                Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-06 09:39 -0500
              Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-11-30 15:52 -0500
                Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-04 01:53 -0700
                Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-04 08:16 -0500
                Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-04 21:18 +0000
                Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-04 18:04 -0500
                Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-05 01:19 +0000
                Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-04 21:05 -0500
                Re: What's new in C? Noob <root@127.0.0.1> - 2012-12-05 16:45 +0100
                Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-06 01:45 -0700
                Re: What's new in C? James Kuyper <jameskuyper@verizon.net> - 2012-12-06 08:12 -0500
                Re: What's new in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-06 21:13 +0000
                Re: What's new in C? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-06 09:15 -0500
                Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-12 18:28 -0700
        Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-11-30 11:22 -0800
          Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-04 03:04 -0700
  Re: What's new in C? Rui Maciel <rui.maciel@gmail.com> - 2012-11-29 18:19 +0000
    Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-11-29 11:41 -0800
  Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-11-30 20:50 +0000
    Re: What's new in C? Jens Gustedt <jens.gustedt@loria.fr> - 2012-11-30 23:35 +0100
      Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-05 03:26 +0000
        Re: What's new in C? gwowen <gwowen@gmail.com> - 2012-12-05 23:49 -0800
          Re: What's new in C? Cal Dershowitz <cal@example.invalid> - 2012-12-06 01:49 -0700
          Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-08 07:55 +0000
            Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 13:29 +0000
              Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-08 15:23 +0000
                Re: What's new in C? Martin Shobe <martin.shobe@yahoo.com> - 2012-12-08 17:46 +0000
                Re: What's new in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-12-08 10:45 -0800
                Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-08 19:32 +0000
                Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 18:55 +0000
                Re: What's new in C? Ian Collins <ian-news@hotmail.com> - 2012-12-09 08:26 +1300
                Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 20:12 +0000
                Re: What's new in C? Ian Collins <ian-news@hotmail.com> - 2012-12-09 09:18 +1300
                Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 21:21 +0000
                Re: What's new in C? Ian Collins <ian-news@hotmail.com> - 2012-12-09 10:27 +1300
                Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-08 13:36 -0800
                Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-08 12:59 -0800
                Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 22:10 +0000
                Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-08 16:15 -0800
                Re: What's new in C? Ian Collins <ian-news@hotmail.com> - 2012-12-09 14:23 +1300
                Re: What's new in C? Ian Collins <ian-news@hotmail.com> - 2012-12-09 14:20 +1300
                Re: What's new in C? Keith Thompson <kst-u@mib.org> - 2012-12-08 11:32 -0800
                Re: What's new in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-09 02:52 +0000
                Re: What's new in C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-09 02:53 +0000
                Re: What's new in C? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-08 20:08 +0000
                Re: What's new in C? "BartC" <bc@freeuk.com> - 2012-12-08 20:25 +0000

csiph-web