請問如何用try catch異常檢測定位C 程式執行中的越界陣列(vector)?

時間 2021-06-01 08:45:29

1樓:粉蒸排骨

vector::at是物件級的方法,又不是類級(static)的,怎麼會「找不到越界的陣列具體是哪個」?

在try catch結構的catch塊內把這個vector的size和你傳入的下標打出來不就完了?

不知道這是不是你想要的:

#include

#include

#include

#include

using StrVector = std::vector;

// 這是使用、操縱vector的函式

std::string getElemnt( int idx, const StrVector& v ) catch( const std::exception& estd::

cerr << std::hexlt;< "在vector(0x" << static_cast( &vlt;< ")上發生異常:\n" << e.

whatlt;< ",\n所用下標是:[" << std::dec << idxlt;< "],當前只有元素數:

" << v.size() << "." << std::

endl;

result = "沒取到"return result;

}int main() ;

StrVector birds ;

std::cout << std::hexlt;< "animals的位址是(0x" << static_cast( &animalslt;< "),元素數:

" << std::dec << animals.size() << ".

" << std::endl;

std::cout << std::hexlt;< "birds的位址是(0x" << static_cast( &birdslt;< "),元素數:

" << std::dec << birds.size() << ".

" << std::endl;

std::cout << "開始下標越界試驗..." << std::endl;

getElemnt( 5, animals );

std::cout << "第二次試驗..." << std::endl;

getElemnt( 5, birds );

return EXIT_SUCCESS;};

如何用C語言實現異常 狀況處理機制?

藥罐子千里冰封 用強型別的union 俗稱Algebraic Data Type 加Functor和Monad,完全代替辣雞Exception 請參考C語言介面與實現第四章異常與斷言一章有利用setjmp longjmp實現的簡單的try catch finally raise 的介面封裝 還可以參...

請問python如何用docker API和paramiko做webssh?

37丫37 或許可以看看這個 Django實現WebSSH操作Kubernetes Podpython 呼叫kubernetes api,實現對pod的ssh連線,從而進行命令執行等操作 還有一系列的WebSSH相關的文章可以參考 https blog.ops coffee.cn webssh 布道...

請問如何用ppt講解文獻?

Ryan 你們這些大佬能不能展示一下ppt做示範啊,一股腦地說應該怎麼怎麼做,到頭來萌新還是看的一臉懵逼,既然搜這個問題就說明不會做,既然不會做說明沒有這個功力,需要乙份模板模仿啊!你們能不能別這麼言簡意賅!get不到哇! apple pie 首先提出這篇文獻回答了什麼問題,簡單介紹用了什麼方法 主...