分割错误,数组地狱

Segmentation fault, array hell?

本文关键字:地狱 数组 错误 分割      更新时间:2023-10-16

似乎有很多人有分割故障问题,但我似乎找不到任何与我的程序相关的问题,如果有一个线程,我很抱歉,我通过多个虽然找不到它。

到目前为止几乎所有内容:

#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <time.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int maxLandMass = SCREEN_WIDTH * SCREEN_HEIGHT;
const int landPercentage = 30;
const int maxHeight = 200;
const int numAnts = 10;
int main(int argc, char **argv)
{
    int currentLandMass = 0;
    int ants[numAnts][3]={{0}}; //x,y,pDir (0123,NESW)
    std::string rules[numAnts]={0};
    std::string rule;
    int states = 2;
    std::cout << "Which rule would you like to use? ";
    std::cin >> rule;
    if(rule == "")
    {
        rule = "RL";
    }
    else if(rule == "RND" || rule == "rnd")
    {
        std::cout << "How many states? ";
        std::cin >> states;
    }
    srand(time(NULL));
    for(int i = 0;i < numAnts;i++)
    {
        ants[i][0] = rand() % SCREEN_WIDTH;
        ants[i][1] = rand() % SCREEN_HEIGHT;
        ants[i][2] = rand() % 4;
        if(rule != "RND" && rule != "rnd")
        {
            rules[i] = rule;
        }
        else
        {
            std::string tempRule;
            for(int s = 0; s < states; s++)
            {
                int r = rand() % 2;
                if(r == 0){ tempRule += "L"; }
                if(r == 1){ tempRule += "R"; }
            }
            rules[i] = tempRule;
        }
        std::cout << rules[i] << "n";
    }
    SDL_Window* window = NULL;
    SDL_Surface* surface = NULL;
    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %sn", SDL_GetError());
    }
    else
    {
        window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if( window == NULL )
        {
            printf("Window could not be created! SDL_Error: %sn", SDL_GetError());
        }
        else
        {
            surface = SDL_GetWindowSurface(window);
            SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0x00, 0x00, 0x00));
            SDL_UpdateWindowSurface(window);
        }
    }
    //Uint16 *pixels = (Uint16 *) surface->pixels;
    /////////////////////////////
    int grid[SCREEN_HEIGHT][SCREEN_WIDTH]={{0}};
    int heights[SCREEN_HEIGHT][SCREEN_WIDTH]={{0}};
    int prevState = 0;
    for(int a = 0; a < numAnts; a++)
    {
        //TODO add stuff here
    }
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

当它运行时,它应该从一个控制台窗口开始,从用户那里获得一些输入,然后切换到SDL窗口并做它的事情,但是控制台窗口告诉我:抛出'std::logic_error'实例后终止调用what():basic_string::_S_construct null not valid

我有一种感觉,这与我如何初始化(或至少试图)数组有关。

我最后添加的部分是我所做的//////线下面的位。

让我知道如果你需要更多的信息,我会尽快得到它,我为我的程序目前的状态道歉,它的混乱也让我有点疯狂,但是(直到我打破它)它工作了:p

您正在用零初始化字符串数组。这是无效的初始化。

有趣的是,如果您选择任何其他数字,您将得到一个更清晰的错误消息,告诉您正在进行从int到const char*的无效转换(这是c风格的字符串,最接近的有效输入类型std::string有一个构造函数)。不幸的是,零初始化会混淆编译器,使其认为您实际上是在分配一个指针,它可能会转换该指针,但幸运的是,它会检查并看到指针为NULL,因此会跳出您看到的错误消息:basic_string::_S_construct null not valid .

我拿走了你的代码并删除了所有的SDL部分,它编译并复制了错误。

然后我把

std::string rules[numAnts]={0};

std::string rules[numAnts];

使用默认构造函数为所有元素创建空字符串,现在似乎可以工作了