Is it safe to return a struct in C or C++?

后端 未结 12 1913
北海茫月
北海茫月 2020-12-04 07:55

What I understand is that this shouldn\'t be done, but I believe I\'ve seen examples that do something like this (note code is not necessarily syntactically correct but the

12条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 08:36

    It's perfectly safe.

    You're returning by value. What would lead to undefined behavior is if you were returning by reference.

    //safe
    mystruct func(int c, int d){
        mystruct retval;
        retval.a = c;
        retval.b = d;
        return retval;
    }
    
    //undefined behavior
    mystruct& func(int c, int d){
        mystruct retval;
        retval.a = c;
        retval.b = d;
        return retval;
    }
    

    The behavior of your snippet is perfectly valid and defined. It doesn't vary by compiler. It's ok!

    Personally I always either return a pointer to a malloc'ed struct

    You shouldn't. You should avoid dynamically allocated memory when possible.

    or just do a pass by reference to the function and modify the values there.

    This option is perfectly valid. It's a matter of choice. In general, you do this if you want to return something else from the function, while modifying the original struct.

    Because my understanding is that once the scope of the function is over, whatever stack was used to allocate the structure can be overwritten

    This is wrong. I meant, it's sort of correct, but you return a copy of the structure you create inside the function. Theoretically. In practice, RVO can and probably will occur. Read up on return value optimization. This means that although retval appears to go out of scope when the function ends, it might actually be built in the calling context, to prevent the extra copy. This is an optimization the compiler is free to implement.

提交回复
热议问题