Namespaces
Variants
Views
Actions

Talk:c/io/clearerr

From cppreference.com
< Talk:c‎ | io

I am pretty sure the second example -- accessing a FILE that has been fclose()d before -- is invoking undefined behaviour. I don't have a better example, in actual fact I came to this page looking for a way to provoke a positive return from ferror(), but the example given is not a good one. A simple implementation of fopen() / fclose() would malloc() memory for the FILE struct in fopen() and free() it again in fclose(), resulting in the subsequent fgetc() / ferror() to access unallocated memory. --DevSolar (talk) 09:12, 17 March 2016 (PDT)

I think you are right, C says in (C11's) 7.21.3/4 "The value of a pointer to a FILE object is indeterminate after the associated file is closed", and of course J.2/1 says it's undefined behavior if "the value of an object with automatic storage duration is used while it is indeterminate". --Cubbi (talk) 11:43, 17 March 2016 (PDT)
as for guaranteed ferror, I got one for you, conversion error (well, guaranteed if you have an appropriate locale). Going to put it in ferror. --Cubbi (talk) 12:22, 17 March 2016 (PDT)