The structue data returned in C
2004-10-31 21:25
429 查看
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 (1)
getA() really return the address of a, the address in the stack which is destroyed, but now no one is using 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 design if the large object returned from 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 used 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 object 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.
Case 1:
struct A { ...}
void Caller()
{
struct A b=getA(); (1)
...
}
struct A getA()
{
struct A a;
a.xxx=xxx
....
return a;
}
The (1)
getA() really return the address of a, the address in the stack which is destroyed, but now no one is using 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 design if the large object returned from 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 used 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 object 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.
相关文章推荐
- Data Flow in the Filter Graph
- Security and Privacy for Cloud-Based Data Management in the Health Network Service Chain: A Microser
- 75.The following data exists in the PRODUCTS table:
- SharePoint At Work----Hyperlinks in the Data View Web Part
- Could not store transport type data for Receive Location 'Recv.Loc' to config store. Primary SSO Server 'Sql-server' failed. The external credentials in the SSO database are more recent.
- 116.Examine the data in the ORD_ITEMS table:
- The computed property "qianmingurl" is already defined in data.
- CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System.Data'
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- Error: The version of SQL Server in use does not support datatype 'datetime2
- The version of SQL Server in use does not support datatype 'datetime2'.
- REST web services with Python, MongoDB, and Spatial data in the Cloud - Part 2
- Authentication and Authorization in the Google Data Protocol
- CRS-2800: Cannot start resource 'ora.DATADG.dg' as it is already in the INTERMEDIATE state on se
- ADO.NET Entity Framework: The version of SQL Server in use does not support datatype 'datetime2'
- ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA
- Sesing the Data Structures & Algorithms in Java
- An Analysis of Data Corruption in the Storage Stack
- iOS多种语言切换须知: error: read failed: The data couldn’t be read because it isn’t in the correct format.
- robot framework连接Oracle错误:ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA