Berkeley CSUA MOTD:2011:March:07 Monday <Sunday>
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
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.
Berkeley CSUA MOTD:2011:March:07 Monday <Sunday>