Berkeley CSUA MOTD:Entry 54056
Berkeley CSUA MOTD
2021/09/20 [General] UID:1000 Activity:popular

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;
                  char * const * p3;
                  p1 = malloc(sizeof *p1);
                  p2 = malloc(sizeof *p2);
                  p3 = malloc(sizeof *p3);
                  free(p1); /* warning in C and C++.  Expected. */
                  free(p2); /* warning in C only.  Why warning in C? */
                  free(p3); /* warning in C++ only.  Why no warning in C? */
                  return 0;
        When I compile t.cpp, "free(p2)" generates a warning and "free(p3)"
        generates no warning, as expected.  However, when I compile t.c, the
        opposite happens.  Why?  Thx.
        \_ gcc 4.3 complains about free(p1) and free(p3) in both C and C++.
           What compiler are you using?
           \_ MS Visual Studio 2008 command-line for x86 (cl.exe).  -- OP
        \_ Some input.  add typedef char * cstr, then replace char * with cstr
           you will get warnings on all frees.  Which means to me that the
           issue might be that binding of ** is stronger than const.
        \_ Some input.  It may be clearer to add typedef char * cstr; then
           replace char * with cstr and you will get warnings on all frees.
           Which means to me that the issue might be that binding of ** is
           stronger than const.
           const char * * p2; // const charPtrPtr p2 not array of const char *
           note, a line const char * string = p2 will not throw any warnings
           note, a line const char * cstr = p2 will not throw any warnings
           (if you put it after the malloc(for init)).  You can also go:
           p2 = (const char **)0xdeadbeef since you can modify p2.
           Funny note: both:
           *p2 is not const since both:
                *p2 = (const char *)0xdeadbeef; // or
                *p2 = (char *) 0xdeadbeef;      // will work.
           no warnings of "assign from incompat ptr type".  (put this before
           the p2 = assignment if you test them both otherwise SEGV)
           with no warnings of "assign from incompat ptr type".  (put this
           before the p2 = assignment if you test them both otherwise SEGV)
           This is on gcc 4.4.4.
                I am assuming here tho that you didn't specifically mean to
           create a constant charPtrPtr with p2.
                My question to you is can you explain what you are declaring
           with p1, p2, p3; that would be a good point to start debugging.
                Also if you can get a disasm of the code look for push's of
           absolute values. that should tell you where the const is.
                RE: cpp vs c.  If you put the code as is in g++ you will get
           errors not warnings.  If you cast properly in the malloc and free
           you won't get any errors.  My guess is that g++ has stricter
           regulations than gcc.
                I'll answer some more when soda has been rebooted because it's
           fucking non-interactive right now.
2021/09/20 [General] UID:1000 Activity:popular

You may also be interested in these entries...
2014/1/14-2/5 [Computer/SW/Languages/C_Cplusplus] UID:54763 Activity:nil
1/14    Why is NULL defined to be "0" in C++ instead of "((void *) 0)" like in
        C?  I have some overloaded functtions where one takes an integer
        parameter and the other a pointer parameter.  When I call it with
        "NULL", the compiler matches it with the integer version instead of
        the pointer version which is a problem.  Other funny effect is that
        sizeof(NULL) is different from sizeof(myPtr).  Thanks.
2009/7/21-24 [Computer/SW/Languages/Java] UID:53168 Activity:moderate
7/20    For those who care btw, it looks like eclipse is now A Standard Tool
        at UCB ugrad cs, probably replaced emacs.  Furthermore, people get
        angry at seeing Makefiles, (since eclispe takes care of that).  I
        guess it's just a sign of the times.
        \_ The more people at my work use eclipse the less the code is
           managable in emacs.  I'm not sure which application's fault
2005/2/15-16 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Compilers] UID:36174 Activity:moderate
2/15    Technical question:  we have a memory leak in our C code, we think,
        but it's not the sort of memory leak where the memory's unreferenced.
        What we'd like to do is sort of a poor-man's profile, we want
        to know who calls our memory allocator "New"...  Sorta like a stack
        trace.  Using an actual profiler is sort of difficult 'cause it's
        a parallel application.  Thanks,  --peterm
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 ......
2004/9/23 [Computer/SW/Languages/C_Cplusplus] UID:33716 Activity:high
9/23    Is the a C++ equivelent to the C realloc function?
        \_ realloc(). But seriously, maybe you want an STL container
           that automatically grows.
           \_ Not in this case.  I just need to grow a char array
              automatically myself.   (For speed reasons) Really, It's a
              very isolated place in the code, and I'll probably just end
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
2004/4/13-14 [Computer/SW/Languages/C_Cplusplus] UID:13175 Activity:high
4/13    How much C++/C knowledge do recent Berkeley CS/EECS grad have?
        \_ Class CSGrad inherits FromDaddy and does not implement C++Knowledge
           very well.
           \_ funny.  just the rich and poor as always.  the middle class can't
              afford education.
        \_ They know how to deal with pointers and addresses, malloc and free.
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?
2003/12/4-5 [Computer/SW/Languages/C_Cplusplus] UID:11317 Activity:nil
12/4    Is there any reason to worry about mixing the usage of malloc and new?
        Obviously, you can't delete() malloc'd memory (and vv), but aside from
        that, will bad things happen?  Oh, I should note that this is for a C
        library that is used by C++ -- a coworker suggested wrapping my memory
        allocations (and deletions) with
        #ifdef __cplusplus
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