Re: When to use automatic variables and when to use malloc



In article <87y7ms1m63.fsf@xxxxxxxxxxxxxxxx>,
Ben Pfaff <blp@xxxxxxxxxxxxxxx> wrote:

What "dangers" of malloc are alleviated by using variable length
arrays? I suspect that forgetting to deallocate the memory block
is the only one.

And unlike malloc(), variable length arrays don't let you recover when
you run out of memory. Of course, nor do any other kind of auto
variable.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
.



Relevant Pages

  • Re: Variable-Length Arrays (was Re: Something wrong in my program)
    ... malloc() can return a non-NULL pointer ... >which relies on a lazy allocator non-conforming. ... in those arrays to fail in the same way that the non-NULL malloc ... return value fails. ...
    (comp.lang.c)
  • Re: data exchange between two windows
    ... There is a do-nothing dtor associated with basic types, ... Concerning using newvs. malloc to allocate arrays of basic types, ...
    (microsoft.public.vc.mfc)
  • Re: initialising array of unknown size (newbie)
    ... Perhaps the malloc function comes in somewhere. ... define these in a manner that can be used to define the arrays. ... This makes main and int rerturning function but in C99 this ... can be know at compile time. ...
    (comp.lang.c)
  • Re: Extra bytes
    ... "In case that ptr is NULL, the [realloc] function behaves exactly as ... the size of an allocated memory block. ... If memblock is NULL, realloc behaves ... the same way as malloc and allocates a new block of size bytes. ...
    (comp.unix.programmer)
  • Re: When to use automatic variables and when to use malloc
    ... The problems and dangers of malloc are widely known. ... A compile time variable length array (such as through ... C99's variable length arrays, or the function allocathat some C ... appropriate to declare everything as automatic variables, ...
    (comp.lang.c)

Loading