------- Comment #16 from bangerth at dealii dot org 2007-08-20 16:21 -------
(In reply to comment #15)
> Of course, the output is '5' and not '0'. So yes, atoi() seems perfectly able
> to initialize buf. (or perhaps, I am still confused).
I think you are. This program here segfaults:
---------------------
#include <cstdio>
const int i=0;
void use(const int *a)
{
int * b = (int *)a;
b[0] = 5;
}
int main(void)
{
use(&i);
printf("%d\n", i);
return 0;
}
----------------------------
Since use() (like atoi) can't know whether its argument is a local automatic
or a global variable, there is no way for it to reliably initialize its
argument. Casting away constness invokes undefined behavior if the static
type of the object is const.
W.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10138