Liste des Groupes | Revenir à cl c |
On 1/9/2025 11:40 PM, Keith Thompson wrote:No, because the value of a has become indeterminate, and operating on it, even to just look at its value, can trap.Andrey Tarasevich <andreytarasevich@hotmail.com> writes:I must be missing something here. Humm... I thought is was okay to do something like this:On 01/09/25 12:12 AM, Julio Di Egidio wrote:>I do not understand that: `free` is changing the pointed data, so>
how can `const void *` even be "correct"?
`free` is destroying the pointed data.
Right. In other words, it causes the pointed-to data to reach the end
of its lifetime. "Changing" the data generally means modifying its
value (that's what "const" forbids).
>
Given:
>
int *ptr = malloc(sizeof *ptr);
*ptr = 42;
printf("*ptr = %d\n", *ptr);
free(ptr);
>
After the call to free(), the int object logically no longer exists.
Also, the value of the pointer object ptr becomes indeterminate.
Attempting to refer to the value of either ptr or *ptr has undefined
behavior.
_____________________________
#include <stdio.h>
#include <stdlib.h>
int main() {
int* a = malloc(sizeof(*a));
if (a)
{
*a = 42;
printf("a = %p\n", (void*)a);
printf("*a = %d\n", *a);
free(a);
printf("a = %p was just freed! do not deref\n", (void*)a);
}
return 0;
}
_____________________________
Is that okay?
[...]
Les messages affichés proviennent d'usenet.