关于C / C ++堆栈分配 [英] About C/C++ stack allocation

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

问题描述

在学习C ++(和C)我有关于堆栈分配的工作,我不能找到解决某些特定的疑惑:

While studying C++ (and C) I had some particular doubts regarding the working of stack allocation, that I can't find a solution to:


  1. 隐是否堆栈分配调用malloc / free函数?如果没有;它是如何保证有堆栈分配和堆分配之间没有冲突?

  1. Does stack allocation call malloc/free functions implicitly? If not; how does it assure there is no conflict between stack allocation and heap allocation?

如果是的;并在C堆栈分配++调用隐含新/删除吗?如是;不超载新的运营商一类影响其堆栈分配?

If yes; does stack allocation in C++ implicitly call new/delete too? If yes; does overloading the new operator for a class affect its stack allocation?

这产生了混乱的结果在VC ++;但由于VC ++是不完全符合标准的(或让我听到),我决定我最好问这里...

It yielded confusing results in VC++; but as VC++ isn't entirely standard-compliant (or so I heard) I decided I better ask here...

推荐答案

堆栈分配不喜欢使用的malloc /免费的东西。它采用了一块内存调用程序栈是内存只是一个连续的片段。

Stack allocation doesn't use anything like malloc/free. It uses a piece of memory called program stack which is just a contiguous segment of memory.

有是存储栈的顶部的特殊寄存器。当在堆栈创建新对象的顶部被升高从而增加了堆,当一个对象被释放(超出范围)的顶部被降低由此降低堆栈

There's a special register that stores the top of the stack. When a new object is created on stack the top is raised thus increasing the stack, when an object is deallocated (goes out of scope) the top is lowered thus decreasing the stack.

如果您尝试分配过大的物体上堆栈或太深进入递归顶部将会超过堆栈的最大允许大小,这被称为堆栈溢出。

If you try to allocate a too large object on stack or go too deep into recursion the top will outgrow the maximum allowed size of the stack and this is called stack overflow.

请注意:堆栈增长的实际方向(增加或减少的地址)<一个href=\"http://stackoverflow.com/questions/664744/what-is-the-direction-of-stack-growth-in-most-modern-systems\">will通过系统有所不同,但总的想法是一样的,无论实际的方向。

Note: actual direction of stack growth (increasing or decreasing addresses) will vary by system, but general idea is the same regardless of actual direction.

这篇关于关于C / C ++堆栈分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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