Liste des Groupes | Revenir à cl c |
Julio Di Egidio <julio@diegidio.name> writes:
You piece of fraudulent, incompetent, then always nazi-retarded shit, now you get it? No, since you incompetent piece of shit indeed don't even know half of it. I just hope you aren't teaching, you piece of ungodly shit.Overall, I am surmising this and only this might go write-protected:Yes, though you should extend your concern beyond what might be
>
MyStruct_t const T = {...};
write-protected. Modifying an object whose type is const qualified is > undefined, even if the object is in writable storage. A compiler may
assume that such an object has not changed because in a program that has
undefined behaviour, all bets are off. For example, under gcc with
almost any optimisation this program prints 42:
#include <stdio.h>
void f(const int *ip)
{
*(int *)ip = 0;
}
int main(void)
{
const int a = 42;
f(&a);
printf("%d\n", a);
}
While this one allocates a "byte-array", i.e. irrespective of how theTechnically you get an object with no effective type. David's reply
pointer we are assigning it is declared:
>
MyStruct_t const *pT = malloc(...);
>
Is my understanding (to that point) correct?
included some references to find out more about the effective type of an
object, but it is safe to say that these only come into play if you are
messing about with the way you access the allocated storage (for example
accessing it as a MyStruct but then later as a floating point object).
More relevant to a discussion of const is to ask what you plan to do
with pT since you can't (without a cast) assign any useful value to the
allocated object.
It is generally better to use a non const-qualified pointer for the
allocation but, when using the pointer, to pass it to functions that use
the right type depending on whether they modify the pointed-to object or
not. For example:
MyStack *sp = malloc(*sp);
...
stack_push(sp, 99);
...
if (stack_empty(sp)) ...
...
stack_free(sp);
we would have
void stack_push(MyStack *sp, int v) { ... }
bool stack_empty(MyStack const *sp) { ... }
void stack_free(MyStack *sp) { ... }
Les messages affichés proviennent d'usenet.