'新'在相对不大的分配上导致std :: bad_alloc

时间:2015-11-28 04:00:48

标签: c++ windows memory-management visual-studio-2015 allocation

我正在开发一个用于处理接近1:1微米:像素比的电路图片的程序,所以我在动态分配的各种矢量中有很多元素(通过{ {3}})。除此之外,我只分配了几个Qt小部件。

<script>
  function dragstartHandler(e) {
    // add `input` element's name to the data transfer object
    e.dataTransfer.setData("text/plain", e.target.firstElementChild.name);
  }

  function dropHandler(e) {
    e.preventDefault();
    // get the `name` of the target and add the moved element to the target's DOM
    var data = e.dataTransfer.getData("Text");
    e.target.appendChild(
      document.querySelector("input[name=" + data + "]").parentElement
    );
  }

  document.ondragover = function(event) {
    // prevent default to allow drop
    // TODO: drop `input` parent `div` element between `contentEditable` text
    event.preventDefault();
    console.log(event)
  };
</script>

<div contentEditable="true" droppable="true" ondrop="dropHandler(event);">
  This text can be edited
  <div draggable="true" ondragstart="dragstartHandler(event);">
    <input disabled name="fixed" type="text" 
           minlength="14" maxlength="14" value="BUT THIS CAN'T" />
  </div>and so can this one here

</div>

这就是它的结果。

public class Main
{
    public static void main(String[] args) throws Exception
    {
        String text = "one,two,123,123.45";

        Object[] row = text.split(",");

        for (int i = 0; i < row.length; i++)
        {
            try
            {
                row[i] = new Double( row[i].toString() );
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }

        System.out.println();

        for(Object o: row)
            System.out.println(o.getClass());

        //  This works, I don't see the difference?

        Object[] data = new Object[2];
        data[0] = "one";
        data[1] = "111.22";
        data[1] = new Double( data[1].toString() );

        System.out.println();

        for(Object o: data)
            System.out.println(o.getClass());
    }
}
构造函数调用中的

CImg<unsigned char> image(this->image.width(), this->image.height(), 1, 3, 0); CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c, const T& value) : _is_shared(false) { const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; if (siz) { _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; try { _data = new T[siz]; /*thrown here*/ } catch (...) { _width = _height = _depth = _spectrum = 0; _data = 0; throw CImgInstanceException(_cimg_instance "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", cimg_instance, cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), size_x, size_y, size_z, size_c); } fill(value); } else { _width = _height = _depth = _spectrum = 0; _data = 0; } } 分别约为25000和900。这使得image.width()在6600万附近的某个地方。这样就分配了大约66MB的无符号字符。

谷歌搜索给了我一堆暗示内存碎片的结果。在高峰使用时,该程序使用了2GB。当然Windows可以在剩余的> 6GB内存中找到66MB的位置,这不会是内存碎片,对吧?那说,还有什么呢?

我补充一点,这只发生在调试模式下编译后,而不是在发布模式下编译后。

1 个答案:

答案 0 :(得分:2)

固定。需要/ LARGEADDRESSAWARE来对抗32位内存限制。

谢谢@jdigital。