繁体   English   中英

qemu:未捕获的目标信号 11(分段错误)-核心转储分段错误-EXC_BAD_ACCESS(代码=2,地址=0x16f603ff0)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM