关于结构和堆栈的一般问题 [英] General Question About Structs and Stack

查看:78
本文介绍了关于结构和堆栈的一般问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般信息:

结构存储在堆栈上,堆上的类。

结构是值类型,而类是引用类型。


问题:

如果struct包含字符串属性(变量),该怎么办?字符串将

是包含在值类型中的引用类型。这个过滤器是否会上升并导致堆栈现在成为放在堆上的引用类型

或者它是否仍然在堆栈中,内部使用指针指向

到堆上预先分配的字符串对象?


问题的原因是因为几乎所有的例子都与

堆栈和讨论速度优势从来没有显示除了值类型(内部结构本身就像int)之外的任何内容。


我正在创建一个大的二进制文件树和当前使用类来实现它。

代码没有问题,但是如果我能看到一个主要的性能通过将类转换为结构来获得
增益你知道吗?


-Greg

解决方案




结构将始终保留在堆栈中,这就是为什么你不能将

null分配给结构ne尽管如此,你还有一个引用变量。

如果你想将结构移动到堆来测试性能,你可以使用weakReference来存储值类型。 br />

c#

规范的第11.3.1,4.1和4.2节有一些细节。


希望这有助于理解。

Salva


" gmccallum"写道:

一般信息:
结构存储在堆栈上,堆上的类。
结构是值类型,而类是引用类型。

问题:
如果struct包含字符串属性(变量),该怎么办?该字符串将是包含在值类型中的引用类型。这个过滤器是否会上升并导致堆栈现在是放在堆上的引用类型
还是它仍然在堆栈上,内部使用指针指向预分配的字符串对象在堆上?

问题的原因是因为几乎所有与
堆栈和讨论速度优势相关的示例都不会在
堆栈中显示除值类型之外的任何内容(其中一个内部结构本身就像int)。

我正在创建一个大的二叉树,并且当前正在使用类来实现它。
代码没有问题,但如果我能看到一个主要的通过将类转换为结构来获得性能我会这样做。

你怎么看?

-Greg



所以如果结构将保留在堆栈上(我想要的地方),那么它是怎么回事

仍然

被视为一个值当它包含引用类型时键入。这是否严重

会影响性能。


示例

struct {

int x; //值类型

string s; //参考类型

}


-Greg McCallum(MCSD)


" Salvador"写道:



结构将始终保留在堆栈中,这就是为什么你不能为结构分配
null尽管如此,你还有一个引用变量。
如果你想将结构移动到堆来测试性能,你可以使用weakReference来存储值类型。

那里关于c#
规范的第11.3.1,4.1和4.2节的一些细节。

希望这有助于理解。
Salva

" ; gmccallum"写道:

一般信息:
结构存储在堆栈上,堆上的类。
结构是值类型,而类是引用类型。

问题:
如果struct包含字符串属性(变量),该怎么办?该字符串将是包含在值类型中的引用类型。这个过滤器是否会上升并导致堆栈现在是放在堆上的引用类型
还是它仍然在堆栈上,内部使用指针指向预分配的字符串对象在堆上?

问题的原因是因为几乎所有与
堆栈和讨论速度优势相关的示例都不会在
堆栈中显示除值类型之外的任何内容(其中一个内部结构本身就像int)。

我正在创建一个大的二叉树,并且当前正在使用类来实现它。
代码没有问题,但如果我能看到一个主要的通过将类转换为结构来获得性能我会这样做。

你怎么看?

-Greg



所以如果结构将保留在堆栈上(我想要的地方),那么它是怎么回事

仍然是

在包含引用类型时将其视为值类型。这是否严重

会影响性能。


示例

struct {

int x; //值类型

string s; //参考类型

}


-Greg McCallum(MCSD)

" Salvador"写道:



结构将始终保留在堆栈中,这就是为什么你不能为结构分配
null尽管如此,你还有一个引用变量。
如果你想将结构移动到堆来测试性能,你可以使用weakReference来存储值类型。

那里关于c#
规范的第11.3.1,4.1和4.2节的一些细节。

希望这有助于理解。
Salva

" ; gmccallum"写道:

一般信息:
结构存储在堆栈上,堆上的类。
结构是值类型,而类是引用类型。

问题:
如果struct包含字符串属性(变量),该怎么办?该字符串将是包含在值类型中的引用类型。这个过滤器是否会上升并导致堆栈现在是放在堆上的引用类型
还是它仍然在堆栈上,内部使用指针指向预分配的字符串对象在堆上?

问题的原因是因为几乎所有与
堆栈和讨论速度优势相关的示例都不会在
堆栈中显示除值类型之外的任何内容(其中一个内部结构本身就像int)。

我正在创建一个大的二叉树,并且当前正在使用类来实现它。
代码没有问题,但如果我能看到一个主要的通过将类转换为结构来获得性能我会这样做。

你怎么看?

-Greg



General Info:
A struct is stored on the stack and a class on the heap.
A struct is a value type while a class is a reference type.

Question:
What if a struct contains a string property(variable). The string would
be a reference type being contained in a value type. Would this filter
up and cause the stack to now be a reference type placed on the heap
or would it still be on the stack with a pointer used internally to point
to a preallocated string object on the heap?

The reason for the question is because almost all examples related to
stacks and discussing the speed benefits never show anything in the
stack other than value types (which a internally structs themselves like int).

I am creating a large binary tree and currently use classes to implement it.
There are no problems with the code, but if I could see a major performance
gain by switching the classes to structs I would do it.

What do you think?

-Greg

解决方案

Hi,

The struct will always remain on the stack, that''s why you can not assign
null to the structure nevertheless you have a reference variable in it.
If you want to move the structure to the heap to test performance you can
use a weakReference to store the value type.

There are some details on the secions 11.3.1, 4.1 and 4.2 of the c#
specification.

Hope this helps to understand.
Salva

"gmccallum" wrote:

General Info:
A struct is stored on the stack and a class on the heap.
A struct is a value type while a class is a reference type.

Question:
What if a struct contains a string property(variable). The string would
be a reference type being contained in a value type. Would this filter
up and cause the stack to now be a reference type placed on the heap
or would it still be on the stack with a pointer used internally to point
to a preallocated string object on the heap?

The reason for the question is because almost all examples related to
stacks and discussing the speed benefits never show anything in the
stack other than value types (which a internally structs themselves like int).

I am creating a large binary tree and currently use classes to implement it.
There are no problems with the code, but if I could see a major performance
gain by switching the classes to structs I would do it.

What do you think?

-Greg



So if the struct will remain on the stack (where I want it), then how is it
still
treated as a value type when it contains a reference type. Does this
seriously
affect performance.

example
struct {
int x; // value type
string s; // ref type
}

-Greg McCallum (MCSD)

"Salvador" wrote:

Hi,

The struct will always remain on the stack, that''s why you can not assign
null to the structure nevertheless you have a reference variable in it.
If you want to move the structure to the heap to test performance you can
use a weakReference to store the value type.

There are some details on the secions 11.3.1, 4.1 and 4.2 of the c#
specification.

Hope this helps to understand.
Salva

"gmccallum" wrote:

General Info:
A struct is stored on the stack and a class on the heap.
A struct is a value type while a class is a reference type.

Question:
What if a struct contains a string property(variable). The string would
be a reference type being contained in a value type. Would this filter
up and cause the stack to now be a reference type placed on the heap
or would it still be on the stack with a pointer used internally to point
to a preallocated string object on the heap?

The reason for the question is because almost all examples related to
stacks and discussing the speed benefits never show anything in the
stack other than value types (which a internally structs themselves like int).

I am creating a large binary tree and currently use classes to implement it.
There are no problems with the code, but if I could see a major performance
gain by switching the classes to structs I would do it.

What do you think?

-Greg



So if the struct will remain on the stack (where I want it), then how is it
still
treated as a value type when it contains a reference type. Does this
seriously
affect performance.

example
struct {
int x; // value type
string s; // ref type
}

-Greg McCallum (MCSD)
"Salvador" wrote:

Hi,

The struct will always remain on the stack, that''s why you can not assign
null to the structure nevertheless you have a reference variable in it.
If you want to move the structure to the heap to test performance you can
use a weakReference to store the value type.

There are some details on the secions 11.3.1, 4.1 and 4.2 of the c#
specification.

Hope this helps to understand.
Salva

"gmccallum" wrote:

General Info:
A struct is stored on the stack and a class on the heap.
A struct is a value type while a class is a reference type.

Question:
What if a struct contains a string property(variable). The string would
be a reference type being contained in a value type. Would this filter
up and cause the stack to now be a reference type placed on the heap
or would it still be on the stack with a pointer used internally to point
to a preallocated string object on the heap?

The reason for the question is because almost all examples related to
stacks and discussing the speed benefits never show anything in the
stack other than value types (which a internally structs themselves like int).

I am creating a large binary tree and currently use classes to implement it.
There are no problems with the code, but if I could see a major performance
gain by switching the classes to structs I would do it.

What do you think?

-Greg



这篇关于关于结构和堆栈的一般问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆