Update 3
My answer to Where exactly does C++ standard say dereferencing an uninitialized pointer is undefined behavior? gives a much better answer to why using an uninitialized pointer is undefined behavior. The basic logic from the C++ draft standard, section 24.2
Iterator requirements, specifically section 24.2.1
In general paragraph 5 and 10 which respectively say (emphasis mine):
[...][ Example: After the declaration of an uninitialized pointer x (as with int* x;), x must always be assumed to have a singular value of a pointer. —end example ] [...] Dereferenceable values are always non-singular.
Update 2
This was originally an answer to a C question with nearly identical circumstances but the original question I answered was merged with this one. I am updating my answer to include an answer specific to the new question and to the C++ draft standard.
b
has not be initialized and therefore it's value is indeterminate but you used indirection on b
which is undefined behavior.
One possible simple fix would be to assign b
to the address of an existing variable, for example:
int a ;
int* b = &a;
Another option would have been to use dynamic allocation via new.
For completeness sake we can see this is undefined behavior by going to the draft C++ standard section 5.3.1
Unary operators paragraph 1 which says(emphasis mine):
The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points.[...]
and if we then go to section 3.10
Lvalues and rvalues paragraph 1 says(emphasis mine):
An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment expression) designates a function or an object. [...]
but b
does not point to a valid object.
Original Answer
You did not allocate any memory to f
nor b
but you used indirection on both which is undefined behavior.
Update
It is worth noting that cranking up the warning levels should have indicated this was a problem, for example using gcc -Wall
gives me the following warning for this code:
warning: 'f' is used uninitialized in this function [-Wuninitialized]
The simplest fix would be to assign f
to point to a valid object like so:
char a ;
char *f = &a ;
Another options would be to use dynamic allocation, if you don't have a handy reference the C FAQ is not a bad place to start.
for completeness sake, if we look at the C99 draft standard Annex J.2
Undefined behavior paragraph 1 says:
The behavior is undefined in the following circumstances:
and includes the following bullet:
The value of an object with automatic storage duration is used while it is
indeterminate (6.2.4, 6.7.8, 6.8).
The value of f
and b
are both automatic variables and are indeterminate since they are not initialized.
It is not clear from reading the referenced sections which statement makes it undefined but section 6.5.2.5
Compound literals paragraph 17 which is part of normative text has an example with the following text which uses the same language and says:
[...]next time around p would have an indeterminate value, which would result in undefined behavior.
In the C11 draft standard the paragraph is 16.