简体   繁体   English

Memset分段故障核心已转储

[英]Memset Segmentation Fault Core Dumped

#include <cstdlib>
#include <stdlib.h>
#include <iostream>
#include <SDL2/SDL.h>

int main ( int argc, char** argv )
{
const int height = 700;
const int width = 800;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0){
    std::cout << "SDL Failed to initalize" << std::endl;
    return 1;
}
SDL_Window *window = SDL_CreateWindow("Particle Fire Explosion", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);

if (window == NULL){
    SDL_Quit();
    return 1;
}

SDL_Renderer *renderer = SDL_CreateRenderer(window, -1,     SDL_RENDERER_PRESENTVSYNC);
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, width, height);
if(renderer == NULL){
    std::cout << "COULD NOT CREATE RENDERER" << std::endl;
    SDL_DestroyTexture(texture);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 1;

}
if(texture == NULL){
    std::cout << "COULD NOT CREATE TEXTURE" << std::endl;
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 1;
}

Uint32 *buffer = new Uint32(width * height);
Uint32 *memsett = new Uint32(width * height * sizeof(Uint32));
memset(buffer, 0xFF, width*height*sizeof(Uint32));

SDL_UpdateTexture(texture, NULL, buffer, width*sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture, NULL, NULL);
SDL_RenderPresent(renderer);

bool quit = false;
SDL_Event event;

while (quit == false){


    while(SDL_PollEvent(&event)){
        if(event.type == SDL_QUIT){
            quit = true;
        }
    }

}
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(texture);
delete [] buffer;
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

this Is my code, Im trying to make an all white window in SDL an dim trying to use memset to so this but it is not working. 这是我的代码,我试图使SDL中的全白窗口变成暗淡的,试图使用memset来这样做,但这是行不通的。 The bug says: Segmentation fault core dumped. 该错误说:细分故障核心已转储。 It goes away when I remove the memset function, so I know that the memory meaning that memset is using is using memory not free so how do I change this? 当我删除memset函数时,它消失了,所以我知道memset正在使用的内存正在使用的内存不是空闲的,该如何更改呢?

Uin32 *buffer = new Uint32(width * height) allocates a single Uint32 with the value width * height , not an array of width * height Uint32 s. Uin32 *buffer = new Uint32(width * height)分配单个Uint32 ,其值为width * height ,而不是width * height Uint32的数组。 Your call to memset then writes beyond the end of that single Uin32 and off into unowned memory. 然后,您对memset调用将超出该单个Uin32并写入未拥有的内存。

You want [] instead of () : 您想要[]而不是()

Uint32* buffer = new Uint32[width * height];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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