物理の駅 Physics station by 現役研究者

テクノロジーは共有されてこそ栄える

Visual Studio 2013でstd::min std::maxが使えなくなった時の対処

std::max - cppreference.com

Visual Studio 2013以降、Visual Studio 2015も完全にalgorithmに移動したらしいので以下の記述が必要。

#include <algorithm> //必要
int main()
{
    int i = std::max(1,2);
    int j = std::min(3,4);
}

ちなみにVisual Studio 2012以下は以下のコードで動く。

#include <xutility> //非推奨
int main()
{
    int i = std::max(1, 2);
    int j = std::min(3, 4);
}

VC10, VC11ではalgorithm 経由で xutilityに記述、VC12, VC14ではalgorithm に直接記述されるようになったようだ。VC12, VC14では、別のincludeファイルからxutilityが参照されている場合は、明示的にalgorithmを追加してやる必要がある。

メモリリークしているソースコードの行を特定する

#include <cstdlib>
#include <new>
#include <memory>

#include <crtdbg.h>

#define _CRTDBG_MAP_ALLOC

#define new ::new(_NORMAL_BLOCK, __FILE__, __LINE__)

void main()
{
    int *i = new int;
    _CrtDumpMemoryLeaks();
    return;
}

これをデバックモードで実行すると

Detected memory leaks!
Dumping objects ->
c:\users\documents\source.cpp(13) : {170} normal block at 0x00000200AD5747B0, 4 bytes long.
 Data: <    > CD CD CD CD 
Object dump complete.

ちゃんとint *i = new int; である13行目でエラーが出ていることが分かる。

参照

プログラマーの友 第八報:メモリリークと crtdbg.h

catchしなかった例外が発生した際の ***は動作を停止しました「WerFault」を表示させない・表示させる

f:id:onsanai:20160912070612p:plain

問題が発生したため、プログラムが正しく動作しなくなりました。プログラムは閉じられ、解決策がある場合はWindowsから通知されます。

を表示させないプログラムは以下のとおり。

#include <exception>
#include <Windows.h>
void main()
{
    SetErrorMode(SEM_NOGPFAULTERRORBOX);
    throw std::exception(); //例外を投げる
}

表示させたい場合は以下の通り。

#include <exception>
#include <Windows.h>
void main()
{
    SetErrorMode(0);//なくてもよいが
    throw std::exception(); //例外を投げる
}

ただし、レジストリで以下の設定をしておかないと SetErrorMode を変えても表示されないので注意

HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Windows Error Reporting
Disabled DWORD 00000000
DontShowUI DOWRD 00000000

Windows Error Reporting がない場合は自分でキーを作成すべし。変更後は要再起動。

参照

SetErrorMode function (Windows)

逆引きWIN32API: 一般保護例外ダイアログを出さなくする方法 - seclan のほえほえルーム

[VC]子プロセスが落ちたのを検出する。(6) うずまき の なんとなくでいいのかも?

xxxEXEは動作を停止しました メッセージを表示させない | きりんの雑記

マルチスレッドにおける例外処理の受け渡し (VC++)

別スレッド中の例外を、本スレッドに渡す方法のメモ。動作確認はVisual Studio 2013 C++で行っている。

詳しい説明は スレッド間の例外転送 を参照してね。

#include <thread>
#include <iostream>

//マルチスレッド用の関数
void f1(std::exception_ptr &eptr) {
    try {
        throw std::exception("test");
    }
    catch (...) { //すべての例外を受ける
        eptr = std::current_exception();
    }
}

int main() {
    std::exception_ptr eptr;
    std::thread t1(f1, std::ref(eptr)); //スレッドを作成し参照でeptrを渡す
    t1.join(); //終了待ち

    try {
        if (eptr) {
            std::rethrow_exception(eptr); //例外を投げなおす
        }
    }
    catch (std::exception &ex) {
        std::cout << ex.what() << std::endl;
    }
    ::system("pause"); //デバッグ用のpause
}

WiMAX 2+は下り速度4.5Mbpsしか出ない

タイトルで何もかも終わったんだが、下り速度を測定してみた。まずは一番重要な速度の一つ、アニメのHD画質が見れるかどうか。

f:id:onsanai:20160904144227p:plain

確かにアニメのHD画質は見れるようだ。4.5Mbpsしか使っていないので、そもそも大したことはないんだが。

Re:ゼロから始める異世界生活のアニメ見放題 | dアニメストア

 

真面目に下り速度を測定してみる。

f:id:onsanai:20160904144535p:plain

6Mbpsか。これ、測定の時だけブーストしてないか?って疑いがあるので、信用しない方がいいと思う。

 

とにかく、速度制限に入ると本当に下り速度は4.5Mbpsになるから、WiMAX 2+を家庭用固定回線代わりに使うのはやめよう。遅すぎる。

 

次回、応答速度を測定予定