![](/img/trans.png)
[英]Program received signal SIGSEGV, Segmentation fault while debugging in codeblocks
[英]program received signal SIGSEGV while using scanf
我正在研究C ++中的算法,並嘗試通過將cin更改為scanf來加快其執行速度。 但是,當我這樣做時,我開始出現SIGSEGV錯誤。
int main(){
int nominal;
int check;
string b;
int x;
int a;
int i = 0;
//cin >> a;
scanf ("%d",&a);
for (int i = 0; i<a; i++){
//cin >> b;
scanf("%s",&b);
std::priority_queue < int > Q;
std::priority_queue < int, std::vector < int >, std::greater < int > > moneyBox;
while (b!="0"){
輸入第一個while時,代碼停止工作。 有關如何修復的任何提示?
scanf()
使用與預定類型相對應的格式化字符。 "%s"
用於在char
數組中存儲以null結尾的c字符串。 因此, scanf()
會將c ++字符串&b
的地址錯誤地解釋為有效的char*
指向足夠大的char的預分配數組。 這將損壞內存並可能導致UB。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.