Berkeley CSUA MOTD:Entry 53849
Berkeley CSUA MOTD
2022/01/25 [General] UID:1000 Activity:popular

2010/6/4-30 [Computer/SW/Languages/C_Cplusplus] UID:53849 Activity:nil
6/4     Is this valid C++ code?

        std::string getStr(void) {
            std::string str("foo");
            return str;
        void foo(char *s);
        void bar(void) {
            foo(getStr().c_str());      // valid?

        Will foo() receive a valid pointer?  Is there any dangling reference or
        memory leak?  Thanks.   --- old timer
        \_ Yes, this is fine (except for the const problem noted below).
           The temporary std::string object returned by getStr is destroyed
           at the end of the statement in which you called getStr.
           \_ It can be a pointer to inside the implementation--it doesn't have
              to alloc anything.
        \_ c_str() returns a const char*, not a char*.  foo needs to take a
           const char*, and does not need to deallocate anything (nor is it
           allowed to modify the contents of the char*). You can cast away the
           const for legacy functions that don't deal with const well as long
           as you know the function won't try to change the string.
           \_ So after I change foo() to "void foo(const char *s);", everything
              is good, right?  Thanks.  -- OP
              \_ Yep. -pp
              \_ for bonus points think about how many times the copy ctor
                 is used.
                 \_ One?  -- OP
2022/01/25 [General] UID:1000 Activity:popular

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 ......
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
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/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?
           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
        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/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.