The structue data returned in C

xiaoxiao2021-03-06  112

Case study case 1: struct a {...} void caller () {struct a b = geta (); (1) ...} struct a geta () {struct a a; a.xxx = xxx ... RETURN A;} THE () Geta () Really Return The Address of A, The Address In The Stack Which IS destroyed, But now No one is useing this stack area. = is the key part in this problem. = Will Cause the value copy (bitwise copy for the struct, copy construct for the object) when (1) finished, the b get the value copy from a. So the object or struct value returned in the function will caused a copy. It is not a Good designiff function. Case 2: struct a {...} void caller () {struct a * b = geta (); (1) ...} struct a * geta () {struct a a; a.xxx = xxx .... return &} the destroyed stack address is buyed in the caller. Case 3: struct a {...} void caller () {struct a b = * (struct a *) Geta (); (1) ...} struct a * geta () {struct a a; a.xxx = xxx .... Return &} this may be the Same as the case 1, but only valid for the structure. IT WILL BE FAILED IT AN O bject is returned unless * operation is overloaded. The object returned in Java is more simple. All the object are allocate from heap, so do not worry about the object in the stack. The reference (object handle) is value copy. It is simple .

转载请注明原文地址:https://www.9cbs.com/read-98206.html

New Post(0)