C++

コンストラクタを持つクラスの配列

C++

コンストラクタに引数が必要なクラスを配列で宣言する時、どうやってその引数を渡すのかわからなかったんだけど、こうすればできた。 class Test{ public: const int value; Test (int i) : value(i) {}; }; int main(void){ Test c[3] = {1,2,3}; printf("%…

'Hoge::v' is not a variable in clause 'shared'

C++でOpemMP並列をする際、 error: 'Hoge::v' is not a variable in clause 'shared'というエラーが出る事がある。これは、クラスのメンバ変数をshared指定しようとした時のエラー。例えば以下がサンプル。 #include <iostream> #include <omp.h> #include <vector> class Hoge{ priva</vector></omp.h></iostream>…

stlのvectorで重複チェック

C++

vectorとかで要素が含まれるかどうかをいつも忘れるので覚書。以下は0から9までの乱数をpush_backしてからそれぞれの値が含まれるか表示するコード。 #include <algorithm> #include <vector> #include <iostream> #include <stdlib.h> double myrand(void){ return static_cast<double>(rand())/static_cast<double></double></double></stdlib.h></iostream></vector></algorithm>…

-fbounds-checkとassert

C++

gccには配列の実行時添字チェックオプション、-fbounds-checkがある。しかし、manによると fbounds-check For front-ends that support it, generate additional code to check that indices used to access arrays are within the declared range. This is …

GNU makeとinclude

C++

ずっとmakefileでわからなかったことがあったのだが、今日まじめに調べてようやく分かった。 makefileでソースの依存関係をいちいち手書きするのが面倒なので、 SRC=$(shell ls *.cc) dep: g++ -MM -MG $(SRC) >makefile.depend というルールを作っておき、m…

メモリ不足でコンストラクタが呼ばれない

AIXで、datasizeが不足するとコンストラクタが呼ばれないという現象にあってハマったので、覚書。こんなコードを書いた。 const int SIZE = 50000000; class Class{ int buf[SIZE]; public: Class(void){ printf("Hello\n"); }; }; int main(void) { printf(…

stringのc_str()の指す先が破壊される

stringで作った文字列をc_str()でconst char*に受けたとき、後でそれを参照しようとすると値が破壊されている。ソースはこんな感じ。 #include <iostream> #include <mpi.h> #include <string> #include <sstream> #include <fstream> #include <iomanip> using namespace std; int main(int argc,char **argv){ i</iomanip></fstream></sstream></string></mpi.h></iostream>…