Use memory on stack

What are the possible behaviors of the program below? I have tried to allocate and use memory on stack, and print the memory block pointed by p, output are characters of ''. I known technically it is not available when the function returns. However, why not the program crash, or print some random garbage? #include <cstring> #include <cstdio> #include <cstdlib> //

在堆栈上使用内存

以下程序可能的行为是什么? 我试图在堆栈上分配和使用内存,并打印由p指向的内存块,输出是' 0'的字符。 我知道它在函数返回时不可用。 但是,为什么不是程序崩溃,或打印一些随机垃圾? #include <cstring> #include <cstdio> #include <cstdlib> //malloc char* getStackMemory(){ char mem[10]; char* p = mem; return p; } int main(){ char* p = getStackMemory();

C function memory allocation

In C, at what time is a function allocated and where in memory does it go? Is the memory for a function allocated when the program is compiled first or is it allocated when the function call is seen first? Is it allocated on the stack or in a code segment? The memory for C functions is always allocated from the code segment at the time the functions are loaded into memory. If a function bel

C函数内存分配

在C中,什么时候分配了一个函数,以及内存中的哪个地方去了? 当程序首先编译时分配函数的内存,还是先分配函数调用时分配的内存? 它是在堆栈上还是在代码段中分配的? C函数的内存始终在函数加载到内存时从代码段中分配。 如果函数属于动态链接库,程序可能会在任意时间加载和卸载它。 你的问题是一个很好的问题,但是要做好一些额外的复杂性准备工作,因为这些东西中的一部分触及运行代码的CPU中的裸晶体管。 我选

In which segment is memory for library functions allocated?

The way automatic variables/local variables go on to stack,dynamically allocated objects/data type go on to heap; where is the memory for library function calls (say printf()) allocated. In which segment? Static linking For a statically linked program, library code is merged in with the application and almost all of the distinction between program and library is lost, ie, each object ends u

哪个段是分配库函数的内存?

自动变量/局部变量进入堆栈的方式,动态分配的对象/数据类型进入堆栈; 哪里是分配库函数调用(比如printf())的内存。 在哪个部分? 静态链接 对于静态链接的程序,库代码与应用程序合并在一起,程序和库之间几乎所有的区别都会丢失,也就是说,每个对象最终都位于主程序中类似对象所在的同一节中。 动态链接 对于动态链接的程序,如果一个对象是可写的而不是自动的,那么将在每个使用该库的进程中分配内存页面,并

Extend Kendo HtmlHelpers for TextBoxFor

我正在寻找如何扩展Kendo HtmlHelpers来做类似的事情@Html.Kendo().TextBoxFor(model => model.field) 这是我的建议using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq.Expressions; using System.Web.Mvc; using System.Web.Mvc.Html; using Kendo.Mvc.UI.Fluent; namespace Kendo.Mvc.UI { public static class KendoExtensions { [SuppressMessag

为TextBoxFor扩展Kendo HtmlHelpers

我正在寻找如何扩展Kendo HtmlHelpers来做类似的事情@Html.Kendo().TextBoxFor(model => model.field) 这是我的建议using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq.Expressions; using System.Web.Mvc; using System.Web.Mvc.Html; using Kendo.Mvc.UI.Fluent; namespace Kendo.Mvc.UI { public static class KendoExtensions { [SuppressMessag

Why Does The Compiler Add An Unnecessary Local Variable

This C# code: private void LoadAssignments(AssignmentType assignmentType, Collection<Assignment> assignments) { bool flag; DataTable lessons = this.GetResults(assignmentType); try { IEnumerator enumerator = lessons.Rows.GetEnumerator(); try { while (true) { flag = enu

为什么编译器添加一个不必要的局部变量

这个C#代码: private void LoadAssignments(AssignmentType assignmentType, Collection<Assignment> assignments) { bool flag; DataTable lessons = this.GetResults(assignmentType); try { IEnumerator enumerator = lessons.Rows.GetEnumerator(); try { while (true) { flag = enu

What is the purpose of the vshost.exe file?

When I create and compile a "hello world" application in C#, I get three files in the Debug folder apart from the main exe (eg HelloWorld.exe) HelloWorld.vshost.exe HelloWorld.pdb HelloWorld.vshost.exe.manifest What purpose do these files serve? The vshost.exe feature was introduced with VS2005 (to answer your comment). The purpose of it is mostly to make debugging launch qu

vshost.exe文件的用途是什么?

当我在C#中创建和编译“hello world”应用程序时,除了主exe文件(例如HelloWorld.exe)外,我还在Debug文件夹中获得了三个文件。 HelloWorld.vshost.exe HelloWorld.pdb HelloWorld.vshost.exe.manifest 这些文件的用途是什么? VS2005引入了vshost.exe功能(回答您的评论)。 它的目的主要是使调试更快启动 - 基本上已经有一个框架运行的过程,只要你愿意,就准备好加载你的应用程序。 有关更多信息,请参阅此MSD

The difference between re

Suppose I have the following two classes in two different assemblies: //in assembly A public class TypeA { // Constructor omitted public void MethodA { try { //do something } catch { throw; } } } //in assembly B public class TypeB { public void MethodB { try { TypeA a = new TypeA(); a.MethodA(); } catch (Exception

重新区别

假设我在两个不同的程序集中有以下两个类: //in assembly A public class TypeA { // Constructor omitted public void MethodA { try { //do something } catch { throw; } } } //in assembly B public class TypeB { public void MethodB { try { TypeA a = new TypeA(); a.MethodA(); } catch (Exception e) //Handle exception

Stack and heap memory of a C program

I'm trying to understand the CPU's role in tracking a programs stack/heap allocation. Reading some material, I've come across this: The stack area traditionally adjoined the heap area and grew the opposite direction; when the stack pointer met the heap pointer, free memory was exhausted. Are the stack and heap pointers stored in program specific registers? If the stack pointe

C程序的堆栈和堆内存

我试图理解CPU在跟踪程序堆栈/堆分配中的作用。 读一些材料,我遇到了这个: 堆栈区域传统上与堆区相邻并朝相反的方向增长; 当堆栈指针遇到堆指针时,空闲内存耗尽。 堆栈和堆指针是否存储在程序特定的寄存器中? 如果堆栈指针指向栈顶,并且(我假设)堆栈指针指向堆栈的末尾,那么这些指针在不覆盖内存(溢出)的情况​​下会如何满足? 这在现代系统中如何工作? 堆栈和堆指针是否存储在程序特定的寄存器中? 基

Is this a memory leak since memory was allocated on stack?

I have a device that can either be a "Router" or a "Switch". I use the below function, passing it an enum that returns me the string. My question is that the memory for whoami is allocated on the stack. When this function devicetype_string finishes, the stack is destroyed. Would this not cause an issue when i use a pointer thats pointing to a memory allocated on the stack?

这是内存泄漏,因为内存分配在堆栈上?

我有一个设备可以是“路由器”或“开关”。 我使用下面的函数,传递给它一个返回字符串的枚举。 我的问题是,whoami的内存分配在堆栈上。 当这个函数devicetype_string完成时,堆栈被销毁。 当我使用指向堆栈上分配的内存的指针时,这不会引起问题吗? 当前的代码有效。 我只是想明白它为什么起作用。 我会假设一个更清洁和便携的解决方案将是malloc内存来保存whoami(以便它进入堆),并且调用函数应释放该内存。 这是目

Is it a bad manner to use the stack to avoid dynamic allocation?

I understand the answer given by this very similar question: When is it best to use the stack instead of the heap and vice versa? But I would like to confirm: Here is a very simple program: void update_p(double * p, int size){ //do something to the values refered to by p } void test_alloc(int size){ double p[size]; unsigned int i; for(i=0;i<size;i++)p[i]=(double)i; up

使用堆栈来避免动态分配是不好的方式吗?

我明白这个非常类似的问题给出的答案:什么时候最好使用栈而不是堆,反之亦然? 但我想确认一下: 这是一个非常简单的程序: void update_p(double * p, int size){ //do something to the values refered to by p } void test_alloc(int size){ double p[size]; unsigned int i; for(i=0;i<size;i++)p[i]=(double)i; update(&p,size); for(i=0;i<size;i++)printf("%fn",p[i]); } int m