<bdo id='GLnie'></bdo><ul id='GLnie'></ul>
    1. <legend id='GLnie'><style id='GLnie'><dir id='GLnie'><q id='GLnie'></q></dir></style></legend>

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

        <i id='GLnie'><tr id='GLnie'><dt id='GLnie'><q id='GLnie'><span id='GLnie'><b id='GLnie'><form id='GLnie'><ins id='GLnie'></ins><ul id='GLnie'></ul><sub id='GLnie'></sub></form><legend id='GLnie'></legend><bdo id='GLnie'><pre id='GLnie'><center id='GLnie'></center></pre></bdo></b><th id='GLnie'></th></span></q></dt></tr></i><div id='GLnie'><tfoot id='GLnie'></tfoot><dl id='GLnie'><fieldset id='GLnie'></fieldset></dl></div>
        <tfoot id='GLnie'></tfoot>
      1. 我不明白为什么这会导致我的程序崩溃?

        时间:2023-07-01
          <i id='nSV1u'><tr id='nSV1u'><dt id='nSV1u'><q id='nSV1u'><span id='nSV1u'><b id='nSV1u'><form id='nSV1u'><ins id='nSV1u'></ins><ul id='nSV1u'></ul><sub id='nSV1u'></sub></form><legend id='nSV1u'></legend><bdo id='nSV1u'><pre id='nSV1u'><center id='nSV1u'></center></pre></bdo></b><th id='nSV1u'></th></span></q></dt></tr></i><div id='nSV1u'><tfoot id='nSV1u'></tfoot><dl id='nSV1u'><fieldset id='nSV1u'></fieldset></dl></div>

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

            <tbody id='nSV1u'></tbody>
            <legend id='nSV1u'><style id='nSV1u'><dir id='nSV1u'><q id='nSV1u'></q></dir></style></legend>

              <tfoot id='nSV1u'></tfoot>
              • <bdo id='nSV1u'></bdo><ul id='nSV1u'></ul>

                  本文介绍了我不明白为什么这会导致我的程序崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我不明白为什么这会导致我的程序崩溃!?当我编译它时,它会结束程序然后停止响应.

                  I don't understand why this is causing my program to crash!? when i compile it makes it to then end of the program then stops responding.

                  void rotate90(Image& image)
                  {
                      Pixel * tempPixel = new Pixel[(image.infoHeader.biWidth * image.infoHeader.biHeight)];
                      for(int r = 0; r < image.infoHeader.biHeight; r ++)
                      {
                          for(int c = 0; c < image.infoHeader.biWidth; c++)
                          {
                  
                              int f = c+(r*image.infoHeader.biWidth);
                              int t = (image.infoHeader.biHeight - r - 1) + (image.infoHeader.biWidth-c-1);
                              tempPixel[t] = image.pixels[f];
                          }
                      }
                      image.pixels =tempPixel ;
                      delete[] tempPixel;
                  }
                  

                  推荐答案

                  你必须在使用它之前声明该变量...

                  You have to declare that variable before using it...

                  Pixel * tempPixel = new Pixel[image.infoHeader.biWidth * image.infoHeader.biHeight];
                  

                  请注意,您必须使用 delete[] 在函数末尾释放临时数组(否则会出现内存泄漏).为了使这个自动化并避免异常安全问题,你应该使用一个智能指针,比如来自 Boost 的 scoped_array 或者(如果你有一个支持新 C++ 标准的编译器)unique_ptr.

                  Notice that you must deallocate the temporary array at the end of the function with delete[] (otherwise you have a memory leak). To make this automatic and avoid issues with exception safety, you should be using a smart pointer, like scoped_array<Pixel> from Boost or (if you have a compiler that supports the new C++ standard) unique_ptr<Pixel[]>.

                  更好的是:你可以只使用 std::vector

                  Even better: you could just use a std::vector<Pixel>

                  std::vector<Pixel> tempPixel(image.infoHeader.biWidth * image.infoHeader.biHeight);
                  

                  让它处理分配/解除分配.

                  and let it deal with allocation/deallocation.

                  抢先式答案更正(由于您的新问题):如果最后你要把 tempPixel 赋值给 image.pixels,那么你一定不能 delete[]> tempPixel,否则 image 将被一个指向释放内存的指针替换.

                  Preemptive answer correction (due to your new question): if in the end you are going to assign tempPixel to image.pixels, then you must not delete[] tempPixel, otherwise image will be replaced with a pointer to deallocated memory.

                  但是你有更大的问题:当你替换 image.pixels 时,你并没有释放它之前指向的内存.所以你应该释放那个内存,然后然后tempPixel分配给它.

                  But you have bigger problems: when you replace image.pixels you are not deallocating the memory it was pointing to previously. So you should deallocate that memory and then assign tempPixel to it.

                  所有这些都假设 image.pixels 是用 new 分配的,并且将用 delete[] 释放(否则你会得到一个分配函数/运算符不匹配).

                  All this assuming that image.pixels was allocated with new and is going to be deallocated with delete[] (otherwise you get a mismatch of allocation functions/operators).

                  顺便说一句,如果您的图像只是 Windows DIB (BMP) 的某种包装,就像从标题字段名称中看到的那样,您没有考虑到像素线是 4 字节对齐的这一事实(因此,如果您的图像不是 32bpp,则必须分配更多内存并相应地执行像素复制).

                  By the way, if your image is just some kind of wrapper for a Windows DIB (BMP) as it seems from the header fields names you are not taking into account the fact that pixel lines are 4-byte aligned (so, if your image is not 32bpp, you must allocate more memory and perform the pixel copy accordingly).

                  这篇关于我不明白为什么这会导致我的程序崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:班级在 Vim 中高亮显示函数名称 下一篇:在 Visual Studio 2017 中计算 AES/CCM 的时间加密

                  相关文章

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

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

                      • <bdo id='bJwk8'></bdo><ul id='bJwk8'></ul>
                      <tfoot id='bJwk8'></tfoot>