Fetching the value of an invalid pointer is an implementation defined behavior in C++ according to this. Now consider the following C program:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int* p=(int*)malloc(sizeof(int));
*p=3;
printf("%d\n",*p);
printf("%p\n",(void*)p);
free(p);
printf("%p\n",(void*)p); // Is this undefined or implementation defined in behaviour C?
}
But is the behaviour same in C also? Is the behaviour of the above C program undefined or implementation defined? What does the C99/C11 standard say about this? Please tell me if the behaviour is different in C99 & C11.
Implementation-defined behavior is defined by the ISO C Standard in section 3.4.1 as: unspecified behavior where each implementation documents how the choice is made. EXAMPLE An example of implementation-defined behavior is the propagation of the high-order bit when a signed integer is shifted right.
So, in C/C++ programming, undefined behavior means when the program fails to compile, or it may execute incorrectly, either crashes or generates incorrect results, or when it may fortuitously do exactly what the programmer intended.
Undefined Behavior results in unpredicted behavior of the entire program. But in unspecified behavior, the program makes choice at a particular junction and continue as usual like originally function executes.
The Pointer in C, is a variable that stores address of another variable. A pointer can also be used to refer to another pointer function. A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location. The purpose of pointer is to save memory space and achieve faster execution time.
This means that the c_str variable points to the location that is not a dynamic memory region; thus, it is not allowed to be passed to the free function. As a result, when the next example is executed, and the program reaches the free function call, it is aborted, and free(): invalid pointer error is displayed.
Expanding on Andrew Henle's answer:
From the C99 Standard, 6.2.4:
An object has a storage duration that determines its lifetime. There are three storage durations: static, automatic, and allocated. Allocated storage is described in 7.20.3. […] The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.
Then in 7.20.3.2: the standard goes on describing malloc()
, calloc()
and free()
, mentioning that
The
free
function causes the space pointed to byptr
to be deallocated.
In 3.17.2:
indeterminate value
either an unspecified value or a trap representation
In 6.2.6.1.5:
Certain object representations need not represent a value of the object type. If the stored value of an object has such a representation and is read by an lvalue expression that does not have character type, the behavior is undefined. […] Such a representation is called a trap representation.
Since the pointer becomes indeterminate, and an indeterminate value can be a trap representation, and you have a variable which is an lvalue, and reading an lvalue trap representation is undefined, therefore yes, the behavior may be undefined.
Per the C standard, section 6.2.4:
The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime. If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With