C ++程序不依赖虚拟内存 [英] C++ Program Not Relying on Virtual Memory

查看:85
本文介绍了C ++程序不依赖虚拟内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做的一项家庭作业要求我们耗尽主内存,以便该程序使用虚拟内存,以便我们可以观察和衡量速度下降.但是,当我获得足够大的内存值时,就会发生段错误或崩溃.我需要耗尽主内存并同时使用虚拟内存,而我给人的印象是Windows(或其他操作系统)会照顾好它,至少这是我所描述的方式.我正在用来观察此程序的程序:

A homework assignment I am working on requires that we exhaust our main memory so that the program uses virtual memory so that we can observe and measure the slowdown. However, when I get to sufficiently large memory values, I segfault or crash. I need to exhaust main memory and use virtual memory simultaneously and I was under the impression that windows (or other operating systems) would just take care of this, at least that is how it has been portrayed to me. The program I am using to observe this:

#include <stdio.h>
#include <iostream>
#include <chrono>

int sizes[] = { 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 };

using namespace std::chrono;

int main(int c, char** args)
{
    int** A;
    int** B;
    int** C;
    for (int n : sizes)
    {
        A = new int*[n];
        B = new int*[n];
        C = new int*[n];
        for (int i = 0; i < n; i++) {
            A[i] = new int[n];
            B[i] = new int[n];
            C[i] = new int[n];
        }
        milliseconds pre_add1 = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                C[i][j] = A[i][j] + B[i][j];
            }
        }
        milliseconds post_add1 = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
        milliseconds pre_add2 = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
        for (int j = 0; j < n; j++)
        {
            for (int i = 0; i < n; i++)
            {
                C[i][j] = A[i][j] + B[i][j];
            }
        }
        milliseconds post_add2 = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
        for (int i = 0; i < n; i++) {
            delete A[i];
            delete B[i];
            delete C[i];
        }
        delete A;
        delete B;
        delete C;
        std::cout << "Size " << n << " took " << (post_add1 - pre_add1).count() << " ms for addition 1" << std::endl;
        std::cout << "Size " << n << " took " << (post_add2 - pre_add2).count() << "ms for addition 2" << std::endl;
    }
    return 0;
}

推荐答案

每次调用 new 应该具有对 delete 的相应调用并且每个对new[]的调用都应具有对delete[]的相应调用.

Every call to new should have a corresponding call to delete and every call to new[] should have a corresponding call to delete[].

通过在分配有new[]的内存块上调用delete而不是调用delete,会导致未定义的行为.这可能是您崩溃的原因.

By instead calling delete on a memory block that was allocated with new[], you are causing undefined behavior. This is likely the reason for your crash.

要解决此问题,必须更改行数

To fix the problem, you must change the lines

delete A[i];
delete B[i];
delete C[i];

delete[] A[i];
delete[] B[i];
delete[] C[i];

和线条

delete A;
delete B;
delete C;

delete[] A;
delete[] B;
delete[] C;

此外,您可能希望增加页面文件的大小,以使new不会很快失败.对于Windows 7,请参阅此链接有关如何增加页面文件的大小.

Also, you may want to increase the size of your page file, so that new doesn't fail so quickly. For Windows 7, see this link on how to increase the page file size.

这篇关于C ++程序不依赖虚拟内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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