The structue data returned in C
来源:互联网 发布:visual studio支持php 编辑:程序博客网 时间:2024/05/17 07:10
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.
- The structue data returned in C
- (NOT CLEAR)Question 45: Which of the following options are returned by the typeid operator in C++?
- the Data Form in Silverlight
- meta-data in the Android
- Here are a few guidelines to keep in mind regarding C data types on the C6000?
- svn the post request returned invalid xml in the responese: XML parse | Commit - 500 Internal Server
- 【SVN】关于 The OPTIONS request returned invalid XML in the response: XML parse error 的 解决方案
- Sesing the Data Structures & Algorithms in Java
- View statistics in the data dictionary
- Data Structures in the Andrew Text Editor
- The Top Ten Algorithms in Data Mining
- Using Renewable Energy in the Data Center
- RDF data in Neo4J - the Tinkerpop story
- the data type of tuple in python
- the data type of list in python
- II. Data types in the Win32 API
- Islands in the Data Stream UVALive
- on the web appear:"No data returned for this view.This might be because the applied filter excludes
- JDO规范相关术语的定义
- 动网论坛上传文件漏洞的原理以及攻击的代码实现
- 创建表,创建行,创建列(VB.NET)
- 习惯
- 客户机与服务器的数据交互
- The structue data returned in C
- JDO makePersistent 的一些补充
- 获取列表框名柄
- 2.13.4 - [lex.string] - 【词法.字符串】
- PB接触初印象
- 中断执行顺序及其时间段的划分。
- 一款免费blog阅读器 - BlogNavigator
- 著名杀毒网站 江民再次被黑
- SUSE LINUX Professional 9.2 功能和优点