Berkeley CSUA MOTD:Entry 18506
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/25 [General] UID:1000 Activity:popular
5/25    

2000/6/21-22 [Computer/SW/Languages/C_Cplusplus] UID:18506 Activity:very high
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.
2025/05/25 [General] UID:1000 Activity:popular
5/25    

You may also be interested in these entries...
2011/3/7-4/20 [Computer/SW/Languages/C_Cplusplus] UID:54056 Activity:nil
3/7     I have a C question.  I have the following source code in two identical
        files t.c and t.cpp:
                #include <stdlib.h>
                int main(int argc, char *argv[]) {
                  const char * const * p1;
                  const char * * p2;
	...
2004/12/14-15 [Computer/SW/Compilers] UID:35291 Activity:moderate
12/14   If I have a C function like this
        void foo(void) {
          static const char unused1[] = "one";
          const char * const unused2 = "two";
          ...... some code ......
          return;
	...
2004/8/10-11 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Compilers] UID:32805 Activity:high
8/10    C question.  Is there anything wrong with the following?
        const my_struct_t **pp;
        pp = malloc(sizeof(my_struct_t *));
        pp = realloc(pp, sizeof (my_struct_t *) * 2);
        "gcc -Wall" doesn't complain.  But the M$ compiler (cl.exe) complains
        about the realloc line:
	...
2004/5/28-29 [Computer/SW/Compilers] UID:30481 Activity:nil
5/28    I just found out that bison inserts these lines of code
                #ifndef __cplusplus
                #ifndef __STDC__
                #define const
                #endif
                #endif
	...
2003/12/8-9 [Computer/SW/Languages/C_Cplusplus] UID:11356 Activity:nil
12/8    c++ question, how do I overload << in my class so it will handle
        endl? ie: myclass << "some string" << endl
        I know how to do the "some string" part:
                myclass & operator << (const char * s);
        what about endl?
        Thanks!!
	...
2003/9/10 [Computer/SW/Languages/C_Cplusplus] UID:29529 Activity:nil
9/10    Stupid question: how do I reference a C callback?
        int mycompare(void const *a, void const *b) { ... }
        qsort(msg, sizeof(Msg), numMsgs, mycompare);
        I get
        "Type error in argument 4 to `qsort'; calling convention mismatch."
        \_ There's nothing wrong in the you reference it.  However, you should
	...
2003/4/22-23 [Computer/SW/Languages/C_Cplusplus] UID:28189 Activity:insanely high
4/22    Anyone know a good link that explains all of C++'s use of the
        keyword mconst?
        \_ http://www.parashift.com/c++-faq-lite
           Search for const in the text box.
           Search for mconst in the text box.
           \- perfection
	...
2002/7/13-15 [Computer/SW/Languages/C_Cplusplus] UID:25351 Activity:moderate
7/13    how do i pass variables to the system in C ?  e.g.
         system("echo input is %s", argv[1]);
        results in "input is %s" but I want the system to see argv[1]
        (I know i can just use printf, but not for what i really want to do).
        \_ sprintf into array, pass array to system?
        \_ so write your own function that takes a variable number of
	...
2001/9/23-24 [Computer/SW/Languages/C_Cplusplus] UID:22600 Activity:moderate
9/23    let's say there's a C library you want to use called libmdn.so.
        I think to load it you'd go something like:
        static { System.loadLibrary("mdn"); }
        However, let's say that a function call looked like this:
        mdn_result_t mdn_encodename(int actions, const char
        *from, char *to, size_t tolen)
	...
2001/3/17-18 [Computer/SW/Languages/C_Cplusplus] UID:20827 Activity:high
3/16    Why does so much C sample code use #define instead of const?
        \_ because any good C code will use a bunch of preprocessor
           anyways. you can't be a good C programmer and eschew the
           preprocessor. For that, you need a language which fills those
           gaps with other constructs (c++ templates go a long way to
           obviate the need for preprocessor for example). you
	...
2000/4/26-28 [Computer/SW/Languages/C_Cplusplus] UID:18123 Activity:high
4/26    Any recommendations for good questions to ask for live interviews
        for a C/C++ coder position?  We're having some tomorrow and I
        wanted to see what people thought.  We don't want geniuses; just
        bright people who have good habits.  E.g., questions one or two
        difficulty levels below asking them to compare timings for
        common sort alogorithms.
	...