[英]qemu: uncaught target signal 11 (Segmentation fault) - core dumped Segmentation fault - EXC_BAD_ACCESS (code=2, address=0x16f603ff0)
我需要用 5 個不同的項目隨機填充這個二維數組。 在運行時,每個項目的數量與 2d 數組的維度一起作為百分比傳遞。
這是我嘗試過的,但我在終端中遇到了分段錯誤錯誤。 當我嘗試運行調試器時,出現此錯誤:
EXC_BAD_ACCESS (code=2, address=0x16f603fe8)
.h 文件
#ifndef LEVEL_H
#define LEVEL_H
#include <cmath>
#include <iostream>
#include <time.h>
#include <stdio.h>
#include "Mario.h"
using namespace std;
//Class for level
class Level{
public:
//Constructor for level, which takes in the array dimensions
//Also takes in the percentages chosen by the file, for how many coins, nothing spaces, goomba and koopa's
//are present in the array (world)
Level(int dimension, int coinPrct, int nothingPrct, int goombaPcrt, int koopaPrct, int mushPcrt);
//Default destructor
~Level();
void populate();
char nextItem();
//Private member variables
private:
char *m_levelAry;
int m_coins;
int m_nothing;
int m_goombas;
int m_koopas;
int m_mushrooms;
int m_dimension;
};
#endif
.cpp 文件
#include "Level.h"
//Constructor handles dimensions of the array and odds of how frequent coins, enemies and nothing spawn
Level::Level(int dimension, int coinPrct, int nothingPrct, int goombaPcrt, int koopaPrct, int mushPcrt){
m_dimension = dimension;
m_levelAry = new char [dimension * dimension];
m_coins = round((coinPrct/100.0) * (dimension * dimension));
m_nothing = round((nothingPrct/100.0) * (dimension * dimension));
m_goombas = round((goombaPcrt/100.0) * (dimension * dimension));
m_koopas = round((koopaPrct/100.0) * (dimension * dimension));
m_mushrooms = round((mushPcrt/100.0) * (dimension * dimension));
/*
srand(time(NULL));
for (int i = 0; i < dimension; i++){
for (int j = 0; j < dimension; j++){
m_levelAry[i * dimension + j] = nextItem();
}
}
for (int i = 0; i < dimension; i++){
for (int j = 0; j < dimension; i++){
cout << m_levelAry[i * dimension + j] << " ";
}
cout << endl;
}
*/
}
Level::~Level(){
delete[] m_levelAry;
}
void Level::populate(){
srand(time(NULL));
for (int i = 0; i < m_dimension; i++){
for (int j = 0; j < m_dimension; j++){
m_levelAry[i * m_dimension + j] = nextItem();
}
}
for (int i = 0; i < m_dimension; i++){
for (int j = 0; j < m_dimension; i++){
cout << m_levelAry[i * m_dimension + j] << " ";
}
cout << endl;
}
}
char Level::nextItem(){
int randItemNum = (rand() % 4) + 1;
switch (randItemNum){
case 1:
if (m_coins != 0){
m_coins -= 1;
return 'c';
} else {
return nextItem();
}
break;
case 2:
if (m_nothing != 0){
m_nothing -= 1;
return 'x';
} else {
return nextItem();
}
break;
case 3:
if (m_goombas != 0){
m_goombas -= 1;
return 'g';
} else {
return nextItem();
}
break;
case 4:
if (m_koopas != 0){
m_koopas -= 1;
return 'k';
} else {
return nextItem();
}
break;
case 5:
if (m_mushrooms != 0){
m_mushrooms -= 1;
return 'm';
} else {
return nextItem();
}
break;
default:
return NULL;
break;
}
}
int main(int argc, char const *argv[])
{
Level level1(5, 25, 47, 8, 8, 12);
level1.populate();
return 0;
}
Level::nextItem()
中的 switch case 永遠不會 go 進入 case 5。
int randItemNum = (rand() % 4) + 1;
為 1~4。
當 m_coins、m_nothing、m_goobas、m_koopas 全部消耗完后, Level::nextItem()
將無限循環,最終導致分段錯誤。
注意: gdb有助於處理此類問題。
更簡單、更安全的人群:
void Level::populate(){
char *locp = m_levelAry;
memset(locp, m_coins, 'c');
locp += m_coins;
memset(locp, m_goombas, 'g');
locp += m_goombas;
memset(locp, m_koopas, 'k');
locp += m_coins;
memset(locp, m_mushrooms, 'm');
locp += m_coins;
memset(locp, m_nothing, 'x');
locp += m_nothing;
std::random_device rd;
std::mt19937 engine(rd());
std::shuffle(m_levelAry, locp, engine);
}
代碼用每個圖塊的所需數量填充數組,然后打亂數組以獲得圖塊的良好分布。
旁注:我會計算硬幣、koopas、goombas 和蘑菇的數量。 數組中沒有任何剩余空間。 這可以防止數學無法精確填充數組的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.