6/20 I have a variable inside a struct. I want to be able to initialize
that variable ONCE and not write to it again. Any subsequent writes
should not be permitted. Is there a way to do that in C? I know
about "const int foo = 5;" but the value I need to pass in is dynamic
and happens at runtime. Declaring a variable as const doesn't let
me assign anything to it at all. Thanks.
\_ No. -pld
\_ There is but it involves changing the variable to a pointer. You
make the pointer point to a big array of these variables that are
all alocated from a section of memory that you have mprotect()ed
to disallow writes. Then you have to worry about the exception
that happens when you get the write...Who are you trying to stop,
anyway? Plugins? Your program has a lot of power over its own
address space. Are you trying to prevent mistakes or what? I hate
these questions where people pose some narrow problem instead of
their goal. --aaron
\_ Uh... since you're coding it why not just not write to the var
again?
\_ you are a fucking moron. "gee, if you're writing the code,
why bother with abstraction and documentation?" -ali
\_ Still waiting for your answer, asshole. "you are a fucking
moron but I don't have a working solution any better than
yours!" I'd rather be a fucking moron than an asshole
*and* a fucking moron like you.
\_ i'd rather be an asshole than say things like "uh...
i don't know what i'm talking about, but i'll tell
you that your question is irrelevant anyways." -ali
\_ Yup. You're an asshole *and* a moron. Thanks for
clarifying this.
\_ ali is the asshole supreme my friend. the egomanic
of all egomaniacs. and, oh yeah, a moron. if you're
going to post on the motd you have to expect some
flames like that. don't let it bother you.
-- bitter alum who loathes ali and wishes him ill
\_ dude. you are pathetic. -ali
\_ uhm. ali has clue, and enough spine to give technical
advice on the motd. you haven't proven shit -- so
siddown and shaddup.
\_ uhm. ali does not have clue. why? because I
said so! -logician extraordinaire
\_ Still waiting to see ali's fantastic technical
advice on this matter. "you are a moron" isn't
technical advice, even on the motd.
\_ please see below. i'm offering two solutions.
one of which seems to be under dispute, though
the antagonist's position isn't clear. -ali
\_ My position is clear and I think it doubly
funny that *you* would call anyone else an
antagonist after starting off by calling
someone else a moron. Seek help.
\_ Use ML or prolog. All variables behave like that by default in
these languages.
\_ cast it to mutable for the initialization. cast it to const
for the routines that shouldn't change it. as close as you
can get to "not permitted" in C for anything...
\_ just declare it const and when you initialize it, cast the
constness away. i can't think of any way a machine could
magically put ONE field in a struct in read-only memory. -ali
\_ it could optimize the const and eliminate the field.
\_ you are a fucking moron. "im too dumb to know about compilier
optimization so ill just ignore it!" -!ali
\_ look dumbshit, i forgive you this one time but please don't
make adhominem comments unless you know what you're talking
about. to the fellow who replied first, try writing:
struct S { const int field = 45; };
in C. you will notice that this is not valid C. in fact
until a few years ago, it wasn't even legal C++. now, if
i declare
struct S { const int field; };
there is NO WAY for the compiler to know at compile time
what the value of "field" is. hence the field cannot be
optimized away.
to the dipshit who replied second: i can teach you C if
you like. i'm very patient. -ali
\_ coulda fooled me!
\_ i'm just saying.
\_ Duh, you're wrong *again*. Just give it up, ali.
\_ what's wrong with what i'm saying? is
struct S {const int field; } not valid C? -ali
\_ Depends.
\_ another disgusting thing you can do:
struct S {
/* int X */
# define X x_setter*1
/* private */
int x_setter;
} s;
func(s.X);
s.X = 3 /* doesn't work because s.X => s.x_setter*1 */ is an
rvalue. there are of coruse problems with the precedence of
*. -ali
\_ This falls miles outside the bounds of "good coding
practice". I hope you don't write code like this for real
world use.
\_ All this argument is grating. C/C++ can get disgusting,
fast. Why bother with the horrid details? If you're not writing
something that is very dependent on super optimiziation,
just make a bool and switch it once you write to the value,
and check the bool after all writes.
\_ The idea is to catch this at compile-time. Otherwise, you can
just as well say "well don't write to it more than once"
\_ And there's nothing wrong with that.
\_ man, i wish more poeple who are wrong would at least display
a certain level of uncertainty in their argumentation so that
the rest of the world doesn't feel like they have to retort
with all their might. start your arguments with something like
\_ summary: suggestion from aaron, an anonymous person, ali, and pld..
moron motd dumbshit say "it won't work" without explainign why.
idiot flames grammar.
\_ Shutup ali. Nobody cares about you or your hairy scrotum,
if you even had one to begin with.
"i don't know much about good programming, but..." and you
WILL get a good answer from someone who does, and it will be
polite and pleasant one. Aver shit like "THIS IS USELESS JUST
FORGET IT" and you'll get the kinds of answer you deserve and
you encourage harshness on yourself.
\_ Huh? English? Some speaking Engrish goodly on motd!
\_ what part of the above don't you understand? i'm sure
people can teach you english as well as C.
\_ me think you not know article part of engrish and
need help much on using pROPER cAPS aND use,,, of,,
a c,om,ma,, or two and me wonder whats word phrase
"Aver shit like" mean and general bad grammar from
coming is. hypocrite bad speaking you telling
other people me needing engrish resson. |