本文介绍了哪里?堆还是堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨 我刚开始使用c ++而且我有点困惑的地方 go ... 假设我们有一个班级: 班级考试{ 私人: int arr [100]; }; 也就是说,我认为''arr''是在堆栈上分配的,因为我不会用b $ b来使用new。 但是如果我在 堆中分配一个测试对象,那么这个数组在哪里分配? 即 test t =新测试; 对象将驻留在堆中,因此''arr''也将在堆中? 是否有任何赞成/堆积分配成员变量(非 琐碎类型)? 谢谢 JonasHiI just started with c++ and I''m a little bit confused where stuffgo...Assume we have a class:class test {private:int arr[100];};That is, I assume that ''arr'' is allocated on the stack, since I don''tuse new.But where is this array allocated if I allocate a test object in theheap?I.e.test t = new test;The object will reside in the heap so therefore the ''arr'' will be inthe heap too?Are there any pro/cons to heap/stack allocate member variables (of nontrivial type)?ThanksJonas推荐答案 编译时未知时间,就是这样。Unknown at compile time, that is. 首先,根据ANSI / ISO标准,堆或堆栈没有 的要求。 但我会参考 ;堆" as _dynamic_memory_。动态 内存是程序在运行时分配的内存,程序控制这些对象的生命周期。 这个内存区域由new运算符和* alloc 函数访问。 变量的生命周期:当程序创建它时, 直到程序销毁它。 示例: test *指针; //... 指针=新测试; 还有另一个内存区域,其中放置了自动, 和静态变量。在main之前,编译器在此存储器中为对象分配了。功能 被执行。全局变量,在文件范围内声明为 static的变量,在 函数或块中声明为static的变量是从该内存区域分配的。 变量的生命周期:程序开始程序结束。 示例: test global_test_variable; void Example1( void) { 静态测试static_function_variable; 返回; } 下一个常见的内存区域是存储本地函数 和块变量的位置。这些变量 在块或函数执行之前分配 并在退出函数或块时销毁。 生命周期变量:函数开始或块 直到函数结束 或块。 示例: void Example2(void) { test a_local_variable; for(unsigned int i = 0; i< 10; ++ i) { test a_block_variable; a_block_variable.a [i] = i; } 返回; } 在您的示例中,标识符arr命名为 字段或成员,类test。这个会员 将永远是班级的一部分。所有变量 类型为test。会有arr会员;无论 变量或实例所在的位置。 从 " heap"中分配两种常见的用法。 :巨大的变量和变量,其大小或需要在运行时确定。许多编译器都有固定大小的区域用于自动和堆栈 内存区域。它们通常会限制 堆的大小。到平台剩余的内存量。 因此堆通常比其他两个内存区域大。 通常,在运行期间需要创建对象。 数量或大小由程序外的因素决定。\\ b 。示例:数据库,消息, 图像处理,音频处理和数据记录。 通过音频处理,一个人不知道数量 $ b $数据b直到运行时。通过消息处理, 程序不知道它将收到哪个消息 以及何时。 - Thomas Matthews C ++新闻组欢迎辞: http://www.slack.net/~shiva/welcome.txt C ++常见问题: http://www.parashift.com/c++-faq-lite C常见问题: http://www.eskimo.com/~ scs / c-faq / top.html alt.comp.lang.learn.c-c ++ faq: http://www.raos.demon.uk/acllc-c++/faq.html 其他网站: http://www.josuttis.com - C ++ STL图书馆书籍 http://www.sgi.com/tech/stl - 标准模板库First off, according to the ANSI/ISO standard, there is norequirement for a heap or a stack.But I will refer to the "heap" as _dynamic_memory_. Dynamicmemory is memory that is allocated by a program duringrun-time and the program controls the life-span of these objects.This area of memory is accessed by the new operator and *allocfunctions.Lifetime of variables: When the program creates it,until the program destroys it.Example:test * pointer;//...pointer = new test;There is also another region of memory where automatic,and static variables are placed. Objects are allocatedin this memory by the compiler before the "main" functionis executed. Global variables, variables declared instatic with file scope, variables declared as static infunctions or blocks are allocated from this memory area.Lifetime of variables: Program start to program end.Example:test global_test_variable;void Example1(void){static test static_function_variable;return;}The next common area of memory is where local functionand block variables are stored. These variablesare allocated before the block or function is executedand destroyed upon exit of the function or block.Lifetime of variables: Start of function or blockuntil the end of the functionor block.Example:void Example2(void){test a_local_variable;for (unsigned int i = 0; i < 10; ++i){test a_block_variable;a_block_variable.a[i] = i;}return;}In your example, the identifier, "arr", names afield, or member, of the class "test". This memberwill always be a part of the class. All variablesof type "test" will have the "arr" member; regardlessof where the variable or instance resides.There are two common usages for allocation from the"heap": huge variables and variables whose size orneed are determined at run-time. Many compilers havea fixed sized area for the automatic and "stack"memory areas. They usually limit the size of the"heap" to the amount of memory left in the platform.Thus the heap is usually larger than the other twomemory areas.Often times, objects need to be created during run-time.The quantity or size is determined by factors outsideof your program. Examples: databases, messages,image processing, audio processing and data logging.With audio processing, one doesn''t know the quantityof data until runtime. With message processing, theprogram doesn''t know which message it will receiveand when.--Thomas MatthewsC++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txtC++ Faq: http://www.parashift.com/c++-faq-liteC Faq: http://www.eskimo.com/~scs/c-faq/top.htmlalt.comp.lang.learn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.htmlOther sites: http://www.josuttis.com -- C++ STL Library book http://www.sgi.com/tech/stl -- Standard Template Library 这篇关于哪里?堆还是堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 09:06