<small id='Vq5WL'></small><noframes id='Vq5WL'>

    • <bdo id='Vq5WL'></bdo><ul id='Vq5WL'></ul>
    <tfoot id='Vq5WL'></tfoot>
    <i id='Vq5WL'><tr id='Vq5WL'><dt id='Vq5WL'><q id='Vq5WL'><span id='Vq5WL'><b id='Vq5WL'><form id='Vq5WL'><ins id='Vq5WL'></ins><ul id='Vq5WL'></ul><sub id='Vq5WL'></sub></form><legend id='Vq5WL'></legend><bdo id='Vq5WL'><pre id='Vq5WL'><center id='Vq5WL'></center></pre></bdo></b><th id='Vq5WL'></th></span></q></dt></tr></i><div id='Vq5WL'><tfoot id='Vq5WL'></tfoot><dl id='Vq5WL'><fieldset id='Vq5WL'></fieldset></dl></div>
    <legend id='Vq5WL'><style id='Vq5WL'><dir id='Vq5WL'><q id='Vq5WL'></q></dir></style></legend>

      C++ 如何在堆栈上动态分配内存?

      时间:2023-09-15
      <tfoot id='evpXf'></tfoot>

        • <legend id='evpXf'><style id='evpXf'><dir id='evpXf'><q id='evpXf'></q></dir></style></legend>
        • <small id='evpXf'></small><noframes id='evpXf'>

            <i id='evpXf'><tr id='evpXf'><dt id='evpXf'><q id='evpXf'><span id='evpXf'><b id='evpXf'><form id='evpXf'><ins id='evpXf'></ins><ul id='evpXf'></ul><sub id='evpXf'></sub></form><legend id='evpXf'></legend><bdo id='evpXf'><pre id='evpXf'><center id='evpXf'></center></pre></bdo></b><th id='evpXf'></th></span></q></dt></tr></i><div id='evpXf'><tfoot id='evpXf'></tfoot><dl id='evpXf'><fieldset id='evpXf'></fieldset></dl></div>
            • <bdo id='evpXf'></bdo><ul id='evpXf'></ul>
                <tbody id='evpXf'></tbody>
                本文介绍了C++ 如何在堆栈上动态分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                有没有办法在栈而不是堆上分配内存?我找不到这方面的好书,这里有人有想法吗?

                Is there a way to allocate memory on stack instead of heap? I can't find a good book on this, anyone here got an idea?

                推荐答案

                使用 alloca()(有时称为 _alloca()_malloca() ),但是 要非常小心 —它会在您离开函数时释放内存,而不是在超出范围时释放内存,因此如果在循环中使用它,您很快就会崩溃.

                Use alloca() (sometimes called _alloca() or _malloca() ), but be very careful about it — it frees its memory when you leave a function, not when you go out of scope, so you'll quickly blow up if you use it inside a loop.

                例如,如果你有一个像

                int foo( int nDataSize, int iterations ) 
                {
                   for ( int i = 0; i < iterations ; ++i )
                   {
                      char *bytes = alloca( nDataSize );
                      // the memory above IS NOT FREED when we pass the brace below!
                   } 
                   return 0;
                }  // alloca() memory only gets freed here
                

                然后 alloca() 将在每次循环中分配 额外的 nDataSize 个字节.在您从函数返回之前,所有 alloca() 字节都不会被释放.因此,如果您的 nDataSize 为 1024,iterations 为 8,您将在返回前分配 8 KB.如果您有 nDataSize= 65536 和 iterations = 32768,您将总共分配 65536×32768=2,147,483,648 字节,几乎肯定会炸毁您的堆栈并导致崩溃.

                Then the alloca() will allocate an additional nDataSize bytes every time through the loop. None of the alloca() bytes get freed until you return from the function. So, if you have an nDataSize of 1024 and an iterations of 8, you'll allocate 8 kilobytes before returning. If you have an nDataSize= 65536 and iterations = 32768, you'll allocate a total 65536×32768=2,147,483,648 bytes, almost certainly blowing your stack and causing a crash.

                轶事:如果你越过缓冲区的末尾写入,你很容易遇到麻烦,特别是如果你将缓冲区传递给另一个函数,并且该子函数对缓冲区的长度有错误的想法.我曾经在使用 alloca() 时修复了一个相当有趣的错误创建临时存储以在将 TrueType 字体字形发送到 GPU 内存之前渲染它.我们的字体库没有考虑瑞典语 Å 中的变音符号.在计算字形大小时,它告诉我们在渲染前分配 n 个字节来存储字形,然后实际渲染 n+128 个字节.额外的 128 字节写入调用堆栈,覆盖返回地址并导致非常痛苦的非确定性崩溃!

                anecdote: You can easily get into trouble if you write past the end of the buffer, especially if you pass the buffer into another function, and that subfunction has the wrong idea about the buffer's length. I once fixed a rather amusing bug where we were using alloca() to create temporary storage for rendering a TrueType font glyph before sending it over to GPU memory. Our font library didn't account for the diacritic in the Swedish Å character when calculating glyph sizes, so it told us to allocate n bytes to store the glyph before rendering, and then actually rendered n+128 bytes. The extra 128 bytes wrote into the call stack, overwriting the return address and inducing a really painful nondeterministic crash!

                这篇关于C++ 如何在堆栈上动态分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:C++11:用 std::shared_ptr() 替换所有非拥有的原始指针 下一篇:你能在 c 或 c++ 中分配一个非常大的单个内存块

                相关文章

                最新文章

                <legend id='bPBfb'><style id='bPBfb'><dir id='bPBfb'><q id='bPBfb'></q></dir></style></legend>
              1. <i id='bPBfb'><tr id='bPBfb'><dt id='bPBfb'><q id='bPBfb'><span id='bPBfb'><b id='bPBfb'><form id='bPBfb'><ins id='bPBfb'></ins><ul id='bPBfb'></ul><sub id='bPBfb'></sub></form><legend id='bPBfb'></legend><bdo id='bPBfb'><pre id='bPBfb'><center id='bPBfb'></center></pre></bdo></b><th id='bPBfb'></th></span></q></dt></tr></i><div id='bPBfb'><tfoot id='bPBfb'></tfoot><dl id='bPBfb'><fieldset id='bPBfb'></fieldset></dl></div>

                <small id='bPBfb'></small><noframes id='bPBfb'>

                  1. <tfoot id='bPBfb'></tfoot>
                    • <bdo id='bPBfb'></bdo><ul id='bPBfb'></ul>