使用指向我的数组的指针可以让我C++"Unhandled exception"?

Using a pointer to my arrays gives me an "Unhandled exception" in C++?

本文关键字:C++ Unhandled exception 我的 数组 指针      更新时间:2023-10-16

以下是供参考的完整代码:

#include <SDL.h>            /* SDL stuff */
#include <SDL_video.h>      /* surfaces,screen,renderer */
#include <SDL_keyboard.h>   /* to handle kbd input */
#include <cstdlib>          /*srand,rand*/
#include <iostream>         /* cout,cin */
#include <time.h>           /* time */
#define NUMFLAKES 128
#define NUMLOOPS (NUMFLAKES >> 1)
//setup the putpixel function
void putpixel( SDL_Renderer* renderer,int &x,int &y,SDL_Color &pxl )
{
    SDL_SetRenderDrawColor( renderer, pxl.r,pxl.g,pxl.b,pxl.a );
    SDL_RenderDrawPoint( renderer,x,y );
}
int quitFunc( SDL_Surface *screen,SDL_Texture *texdisp,SDL_Renderer *renderer,SDL_Window *window );
int main( int argc,char *args[] )
{
    std::ios::sync_with_stdio( false ); //make iostream a bit snappier by stopping sync with stdio
        //Start SDL
    int initret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_EVENTS );
    switch( initret ) {
    case true:
    std::cout << "SDL_Init: Couldn't start SDL...n" << SDL_GetError() << std::endl; break;
    default:    break;
    }
    SDL_DisplayMode fulldisp;
    SDL_GetCurrentDisplayMode( 0,&fulldisp );
    //The attributes of the screen
    const int PX_WIDTH      = (int)(fulldisp.w / 1.5);
    const int PX_HEIGHT     = (int)(fulldisp.h / 1.5);
    int xo[NUMFLAKES] = {};
    int yo[NUMFLAKES] = {};
    int *x = &xo[NUMFLAKES];        //fix this bullshit
    int *y = &yo[NUMFLAKES];

    //To define the current window
    SDL_Window* window = SDL_CreateWindow( "SDL Snowflakes",
                                            SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,
                                            PX_WIDTH,PX_HEIGHT,SDL_WINDOW_SHOWN );
    //The surface using the window defined above
    SDL_Surface* screen = SDL_GetWindowSurface( window );
    SDL_ShowCursor(1);
    /* Set up renderer */
    SDL_Renderer* renderer = SDL_CreateRenderer( window,-1,
                             SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE  | SDL_RENDERER_PRESENTVSYNC );
    /* render to texture */
    SDL_Texture* texdisp = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGB888,
                                             SDL_TEXTUREACCESS_TARGET,PX_WIDTH,PX_HEIGHT);
    SDL_SetRenderTarget( renderer,texdisp );
    SDL_UpdateWindowSurfaceRects( window,NULL,NUMFLAKES );
    SDL_SetRenderDrawColor( renderer,0,0,0,0 );
    SDL_RenderClear( renderer );
    //colours for putpixel
    SDL_Color white;
    SDL_Color rndcol;
    white = { 0xFF,0xFF,0xFF,0xFF };
    rndcol = { rand() % 255,rand() % 255,rand() % 255,0xFF };
    //If there was an error in setting up the screen
    switch((bool)(screen)) {
    case 0:
        SDL_Quit(); return 1; break;
    default:
        std::cout << "Created window of size: " << PX_WIDTH << "x" << PX_HEIGHT << std::endl;
        break;
    }
    SDL_Event sdlevent;
    SDL_PollEvent( &sdlevent );
    //Setup Snow Flakes
    SDL_Rect        putfr;      //Rect for updating the screen
    int flake = NUMFLAKES;
    int *pFlake = &flake;
    srand( ( unsigned )time( NULL ) );
    for( *pFlake = 0; *pFlake < NUMFLAKES; (*pFlake)++ )
    {
        (*x)[ pFlake ] = rand() % PX_WIDTH;
        (*y)[ pFlake ] = rand() % ( int )( PX_HEIGHT / 1.5 );
        xo[ *pFlake ] = (*x)[ pFlake ];
        yo[ *pFlake ] = (*y)[ pFlake ];
        std::cout << "Set origin..." << (*x)[ pFlake ] << "," << (*y)[ pFlake ]
                  << "nAt address->" << y[ *pFlake ] << "," << y[ *pFlake ] << std::endl;
        putpixel( renderer,(*x)[ pFlake ],(*y)[ pFlake ],white );
        /* Intended to only update what changed on screen, still working on it, ignore this */
        putfr.x = (*x)[ pFlake ];
        putfr.y = (*y)[ pFlake ];
        SDL_SetRenderTarget( renderer,NULL );
        SDL_RenderCopy( renderer,texdisp,NULL,&putfr );
        SDL_RenderPresent( renderer );
    }
    int move,movex,movey;
    for( int loop = 0; loop <= NUMLOOPS && (bool)(sdlevent.type != SDL_QUIT); loop++ )
    {
        for( (*pFlake) = 0; (*pFlake) < NUMFLAKES && sdlevent.type != SDL_QUIT; (*pFlake)++,SDL_PollEvent( &sdlevent ) )
        {
            //resetting rndcol
            rndcol = { rand() % 255,rand() % 255,rand() % 255 };
            // choose whether to move left, right or straight down and by how many pixels
            move = rand() % 3;
            movex = ( rand() % 2 ) + 1;
            movey = ( rand() % 5 ) + 1;
            //backup the existing positions
            xo[ *pFlake ] = (*x)[ pFlake ];
            yo[ *pFlake ] = (*y)[ pFlake ];
            // now draw again
            putpixel( renderer,(*x)[ pFlake ],(*y)[ pFlake ],rndcol );
            // move down the screen by the no of movey
            if( (*y)[ pFlake ] + ( movey + 1 ) < PX_HEIGHT )
            {
                (*y)[ pFlake ] = (*y)[ pFlake ] + ( movey + 1 );
            } else {
                putpixel( renderer,(*x)[ pFlake ],(*y)[ pFlake ],rndcol );
                (*y)[ pFlake ] = rand() % ( PX_HEIGHT / 2 );
            }

            if( (*x)[ pFlake ] < 0 ) {
                (*x)[ pFlake ] = movex;
            }
            if( (*x)[ pFlake ] > PX_WIDTH ) {
                (*x)[ pFlake ] = ( PX_WIDTH - movex );
            }
            // if 0 then left, 1 for right, else straight down
            switch( move ) {
            case 0:
                (*x)[ &pFlake ] = (*x)[ &pFlake ] - movex; break;
            case 1:
                (*x)[ &pFlake ] = (*x)[ &pFlake ] + movex; break;
            }
            putpixel( renderer,(*x)[ pFlake ],(*y)[ pFlake ],rndcol );
        }
        //Renderer needs only to update what changes
        putfr.x = xo[ *pFlake ];
        putfr.y = yo[ *pFlake ];
        SDL_SetRenderTarget( renderer,NULL );
        SDL_RenderCopy( renderer,texdisp,&putfr,&putfr );
        SDL_RenderPresent( renderer );
        SDL_Delay(128);
        //std::cout << "Completed " << loop << " loops" << std::endl;
    }
    quitFunc( screen,texdisp,renderer,window );
    return 0;   //just to stop compiler warning
}
int quitFunc( SDL_Surface *screen,SDL_Texture *texdisp,SDL_Renderer *renderer,SDL_Window *window )
{
    SDL_FreeSurface( screen );      // Destroy things before SDL_Quit
    SDL_DestroyTexture( texdisp );
    SDL_DestroyRenderer( renderer );
    SDL_DestroyWindow( window );
    std::cout << "nDestroyed SDL objects, calling SDL_Quit()..." << std::endl;
    SDL_Quit();
    std::cout << "nAccomplished SDL_Quitn" << std::endl;
    return 0;
}

我正在尝试使用指向 x[] 和 y[](不是指针数组,而是指向数组的指针)的指针来使程序只使用这些对象,而不是制作副本并更改它们。

这几乎只是为了提高处理效率/最小化程序占用空间,因为数组在某些点可能会变得太大,并且复制这些点可能需要太多时间。

我也尝试过:

int *x = { &xo[NUMFLAKES] };
int *y = { &yo[NUMFLAKES] };

这给了我同样的错误。我知道我可能错误地使用了指针(我认为它试图访问错误的内存?),但我不确定如何以正确的方式做到这一点。

我怀疑问题是

int *x = &xo[NUMFLAKES];

这是初始化指针 x 以指向数组NUMFLAKES元素的地址 - 一个超过数组末尾的地址。这不太可能是你的意思。也许

int *x = &xo[0]

这会将指针设置为数组第一个元素的地址,从而允许您使用 x 作为 xo 的别名。

错误在这里:int *x = &xo[NUMFLAKES];
您正在尝试访问大小为 128 的数组的索引 128(NUMFLAKES)。此指数 (128) 超出界限,因为索引从 0 开始。

您可以通过以下方式实现所需的目标:

int *p;
int xo[NUMFLAKES];
p = xo; // <-- Now p is pointing to the first element of xO