【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?

C++で書いているソース内において、
setjmp・longjmpを挿入し、
プログラムを実行してみましたところ、
「セグメンテーション違反」ということで止まってしまいました。

どうして、そうなってしまったのか?が、
自分では解決に至らず、
質問させていただきます。

挿入す前はエラーなど出ませんでしたが、
長々と確認してみたところ、
「セグメンテーション違反」となった部分は、
やはり、挿入したsetjmp・longjmpの部分でした。

「test.c」というファイル内にて、
以下のとおり、setjmpをしかけております。

========================================
jmp_buf env;
if (setjmp(env) == 0) {
std::string food = get_air(key);
} else {
printf("test OK \n");
}
========================================

そして、
「test.c」とは別のファイルなんですが、
以下のとおり、
「get_air.h」というファイルにて、
「longjmp」をしかけております。

========================================
if (iterator != _container_air.end()) {
printf("## [info]get_the_air \n");
}
printf("## [info]fail to get \n");
longjmp(env, 1);
   printf("Here is out of area.\n");
}
========================================

周囲に質問できる人が皆無なため、
「特に問題なさそうだよ」というのでも結構ですので、
何かアドバイスいただけると大変ありがたいです。
宜しくお願い致します。

A 回答 (3件)

環境が掛かれていないので、不正確な回答を書いているかもしれませんが、



・C++では、setjmp/longjmpは使わない事。
プログラム状態の保存、復元の仕組みが違うので、C++からはsetjmp/longjmpは
使用できないみたいです。
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます!
勉強になりましたです。
なるほどですねー。

ありがとうございました。

お礼日時:2009/04/21 09:50

try~catchがあるので不要です。

    • good
    • 0

#1さんがご回答なさっている通りです。



C++ Language Reference
Using setjmp/longjmp

Do not use setjmp and longjmp in C++ programs; these functions do not support C++ object semantics. Also, using these functions in C++ programs may degrade performance by preventing optimization on local variables. Use the C++ exception handling try/catch constructs instead.

If you must use setjmp/longjmp in a C++ program, the interaction between these functions and C++ exception handling requires that you include SETJMP.H or SETJMPEX.H. Destructors for local objects will be called during the stack unwind if you compile with /EH. If you compile with /EHs and one of your functions call a function that uses nothrow, and if the function that uses nothrow calls longjmp, the destructor unwind may not occur, depending on the optimizer.

Also, if you intend your code to be portable, do not rely on correct destruction of frame-based objects when executing a nonlocal goto using a call to longjmp.
」(from msdn)
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!


おすすめ情報