Function variables stored on heap or stack in address space?

I encountered the following question in an exam: When a program calls a function, in which type of data structure is the memory allocated for the variable in that function? HEAP QUEUE LIFO STACK According to the test, HEAP is the correct answer, although I selected STACK. Can someone fantastic person out there please explain why? Thanks in advance. Well, local variables and para

函数变量存储在堆或堆栈中的地址空间中?

我在考试中遇到以下问题: 当一个程序调用一个函数时,哪种类型的数据结构是为该函数中的变量分配的内存? 堆 队列 LIFO STACK 根据测试,HEAP是正确答案,尽管我选择了STACK。 有没有人可以为你解释原因? 提前致谢。 那么,局部变量和参数不会堆积在堆栈上。 对于本地值类型,这意味着该值本身存储在堆栈中。 对于本地引用类型,只有引用会在堆栈上。 然而,为了得到更深入的解释,我建议阅读Erik Lippe

StatusBar record x of y

I have an ObservableCollection which is the DataContext for a Grid. The Grid is a User Control inserted into the main Window. I would like to display 'Record x of y' in the StatusBar so, as a first step, I am attempting to display it in the Grid using this XAML: <TextBlock Grid.Row="2" Grid.Column="3"> <TextBlock Text="{Binding CurrentPosition}" /> <TextBlock Text="

y的StatusBar记录x

我有一个ObservableCollection,它是Grid的DataContext。 网格是插入到主窗口中的用户控件。 我想在StatusBar中显示'记录x的y',作为第一步,我试图使用这个XAML在网格中显示它: <TextBlock Grid.Row="2" Grid.Column="3"> <TextBlock Text="{Binding CurrentPosition}" /> <TextBlock Text="{Binding Count}" /> </TextBlock> 计数工作没有问题,并自动更新添加新项目。 在我的代码

Garbage Collector no more references

I've been reading about GC in C# and I find some statements quite amusing to be real. Say I have the following method: public void Foo() { Animal eAnimal = new Animal(); eAnimal.Name = "Matthew"; GC.Collect(); ...more code not using eAnimal... return; } Supposedly in the GC.Collect() statement, the GC figures out that the memory address corresponding the the new Animal()

垃圾收集器没有更多的参考

我一直在阅读关于C#中的GC,我发现一些陈述很有趣,是真实的。 假设我有以下方法: public void Foo() { Animal eAnimal = new Animal(); eAnimal.Name = "Matthew"; GC.Collect(); ...more code not using eAnimal... return; } 假设在GC.Collect()语句中,GC指出代码中不再引用与新的Animal()对象相对应的内存地址,因此适用于处理。 据我所知,即使在GC.Collect()语句中,也有对堆栈中新的Ani

How does the GC update references after compaction occurs

The .NET Garbage Collector collects objects (reclaims their memory) and also performs memory compaction (to keep memory fragmentation to minimum). I am wondering, since an application may have many references to objects, how does the GC (or the CLR) manage these references to objects, when the object's address changes due to compaction being made by the GC. The concept is simple enough, t

压缩发生后GC如何更新引用

.NET垃圾收集器收集对象(回收它们的内存),并执行内存压缩(将内存碎片降到最低)。 我想知道,由于应用程序可能有很多对象的引用,当GC(或CLR)由于GC进行压缩而导致对象的地址更改时,GC(或CLR)如何管理这些对象引用。 这个概念很简单,垃圾收集器只是更新任何对象引用,并将它们重新指向移动的对象。 实现有点棘手,原生代码和托管代码之间没有真正的区别,它们都是机器代码。 对象引用没有什么特别之处,它只是

Structs, heaps, and stacks

I've been reading a book about C# and came across the topic of storing values in memory. An instance of a reference type is always created on the heap, however a variable's value lives wherever it's declared. Only local variables (variables declared within methods [not anonymous]) and method parameters live on the stack. So my question is - if I declare those structs as such local

结构,堆和堆栈

我一直在阅读一本关于C#的书,并且遇到了在内存中存储值的主题。 一个引用类型的实例总是在堆上创建,但是一个变量的值将在其声明的任何地方生存。 只有局部变量(在方法[非匿名]中声明的变量)和方法参数存在于堆栈中。 所以我的问题是 - 如果我将这些结构声明为这样的局部变量 - 它们会全部放在栈上吗? struct A<T> where T : struct { } struct B<T> where T : class { } struct C { } 我只是想知道一

Expression tree for child collection List<string>.Any

I am building generic linq query using expression tree. I am stuck when creating expression on child collection. Method call blows up because of incompatible types. Normally I know what to put there, but the Any() method call has me confused. I've tried every type I can think of and no luck. Any help would be appreciated. Here is my entity class: public class Story : Entity { publ

表达式树的子集合List <string> .Any

我正在建立使用表达式树的通用linq查询。 我在创建子集合表达式时遇到困难。 方法调用由于不兼容的类型而爆炸。 通常我知道该放什么,但Any()方法调用让我感到困惑。 我尝试了所有我能想到的类型,但没有运气。 任何帮助,将不胜感激。 这是我的实体类: public class Story : Entity { public string Author { get; set; } public IList<string> Contributors { get; set; } } 查询我想要生成表达式

In C#, do objects which have a local scope use the stack?

Say we have void Foo() { var bar = new Bar(); bar.Woo(); } Will the CLR use the stack for the local variable? If not, why? Is it not more efficient to use the stack for variables which have a limited scope instead of using a more expensive garbage collector? Not only the CLR would need to know that the scope is local, but also that the object wont be referenced from anywhere else.

在C#中,具有本地作用域的对象是否使用堆栈?

说我们有 void Foo() { var bar = new Bar(); bar.Woo(); } CLR会使用堆栈作为局部变量吗? 如果不是,为什么? 将堆栈用于具有有限范围的变量而不是使用更昂贵的垃圾收集器是否更有效? 不仅CLR需要知道范围是本地的,而且该对象也不会从其他地方引用。 除了最微不足道的情况之外,这需要进行深入的代码分析,就像您发布的那样。 理论上,CLR可以执行某种类型的逃逸分析。 如果这导致只能从本地上下文访问该

Performance: should method return a class or struct?

Recently moved from python to C#. Developing math app. Looked through many questions at SO about class vs struct, so I would like an advice from experienced people regarding performance. DETAILS I have a method, and during its execution 6 double variables are calculated and about 6 double[] arrays of same lenght. I want my method to return all them "packed" into one variable. No

性能:方法应该返回一个类或结构?

最近从Python跳转到C#。 开发数学应用程序。 在关于类和结构的SO上看过很多问题,所以我希望有经验的人提供关于性能的建议。 细节 我有一个方法,并在执行期间计算6个双变量和大约6个相同长度的double []数组。 我希望我的方法将所有“包装”成一个变量。 不打算改变它们,我只需要一个存储空间并访问它们。 在应用程序执行过程中,方法会被调用很多次,这样的存储也会被多次创建(最多40次)。 再生示例 public (???)

Array memory Allocation clarification

Today i was reading a SO thread about array and its memory allocation. And i found an answer which was neatly explained and i must say its excellent. But after reading that answer, i got few more questions which i kept asking myself about what i just read. So far still i am unable to answer it myself nor able to google it up. Hence i require your kind help in explaining those questions to me

数组内存分配说明

今天,我正在阅读关于数组和其内存分配的SO线程。 我找到了一个整齐解释的答案,我必须说它的优秀。 但在读完这个答案之后,我又多了几个问题,我一直在问自己我读的是什么。 到目前为止,我仍然无法自己回答,也无法将其通过Google进行回答。 因此,我需要你的善意帮助向我解释这些问题。 为什么当数组的每个内容都被存储在堆栈上时,数组会在堆上创建? 只是2在值类型的情况下避免拳击? 如果上面是真的,那么为什么

Is a static value type field boxed in the heap in C#?

Just out of curiosity - consider the following example: public class A { public static int Foo; } public class Program { static void Main() { // The following variable will be allocated on the // stack and will directly hold 42 because it is a // value type. int foo = 42; // The following field resides on the (high frequency) // heap,

在C#堆中是否装有静态值类型的字段?

出于好奇 - 考虑下面的例子: public class A { public static int Foo; } public class Program { static void Main() { // The following variable will be allocated on the // stack and will directly hold 42 because it is a // value type. int foo = 42; // The following field resides on the (high frequency) // heap, but is it boxed because of