D
Douglas A. Gwyn
Jordan said:And how is the input controlled? Blind luck?
No. How is an automobile's direction controlled? By blind luck?
Jordan said:And how is the input controlled? Blind luck?
Keith Thompson said:Well, obviously malloc() does, and calloc() and realloc() effectively
do. Other than that, fopen() and other functions *could* malloc
storage behind the scenes, but not in any manner that's visible to a
calling program.
Hardly. Gets() has been used in a zillion contexts where it has
not overflowed the allotted buffer, ...
... so it must not require all
of what you describe. You probably have in mind a usage model
that does *not* include controlling the input to stdin, which is
a different context.
John Devereux said:I was thinking in particular about "big" functions like printf, and
embedded applications (where malloc / free use is often discouraged).
If it was guaranteed that standard functions did *not* use malloc
"behind the scenes", then this could be useful to know.
Keith said:Well, obviously malloc() does, and calloc() and realloc() effectively
do. Other than that, fopen() and other functions *could* malloc
storage behind the scenes, but not in any manner that's visible to a
calling program.
John Devereux said:I was thinking in particular about "big" functions like printf, and
embedded applications (where malloc / free use is often discouraged).
If it was guaranteed that standard functions did *not* use malloc
"behind the scenes", then this could be useful to know.
No. How is an automobile's direction controlled? By blind luck?
Many of the standard library functions contain wording that says that
other library functions must behave as-if they never call that
particular function. I can find no such wording for the malloc()
family.
Even if there were such wording, an implementation could easily get
around it by providing a lower-level __malloc() function. malloc()
itself could just call __malloc(); other library functions couldn't
call malloc(), but they could call __malloc(). One could argue either
way on the question of whether this meets the "as-if" requirement.
Well, obviously malloc() does,
Richard Heathfield said:Keith Thompson said:
You mean malloc calls malloc? ;-)
"Steering an automobile into a concrete wall will cause a crash" is
acceptable and widely understood by drivers. I'd dispute both that
"entering more than _x_ characters at _y_ prompt will cause a crash (or
other undefined behavior)" is acceptable and that it's typically
documented.
Nowhere in my /automobile/ documentation does it say "do not steer
this vehicle into a wall". I was taught this when I learned to drive.
Nowhere in my /compiler/ documentation does it say "do not overflow
arrays". I was taught this when I learned to programme.
Is it really true that no standard function malloc(s) storage?
Douglas A. Gwyn said:No. How is an automobile's direction controlled? By blind luck?
Douglas A. Gwyn said:It's not evident how useful it is in general, because details
of the buffering vary among platforms. For example, on some
systems multiple input or output lines might be buffered while
on others just one line at a time might be, depending on file
type etc. There are also multiple levels of buffering,
including kernel character queues, etc. and one needs to
specify whether or not unread/unwritten buffered data is
flushed at that level also. For a specific platform one can
handle all the details, but for general use it would be not
only a problem to specify adequately but also to use in a
portable manner.
Douglas A. Gwyn said:Some of us tried to get strdup() adopted for the standard,
but there was resistance primarily on the grounds that no other
standard function malloc()ed storage and the opposition didn't
want to start that as a design trend.
Douglas A. Gwyn said:Pedagogically, gets() is a good example to have around.
Mark McIntyre said:Nowhere in my /automobile/ documentation does it say "do not steer
this vehicle into a wall". I was taught this when I learned to drive.
Nowhere in my /compiler/ documentation does it say "do not overflow
arrays". I was taught this when I learned to programme.
But the former is common sense
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.