使用指向我的数组的指针给我一个"未处理的异常"在C ++中?

时间:2015-03-12 01:07:08

标签: c++ arrays pointers visual-c++

以下是完整的参考代码:

#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_Quit\n" << std::endl;
    return 0;
}

我正在尝试使用指向x []和y []的指针(不是指针数组,而是指向数组的指针),以使程序只使用这些对象,而不是复制和更改它们。 / p>

这几乎只是为了降低处理效率/最小化程序占用空间,因为阵列在某些点可能会变得太大而复制这些可能需要花费太多时间。

我也尝试过:

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

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

2 个答案:

答案 0 :(得分:2)

我怀疑问题是

int *x = &xo[NUMFLAKES];

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

int *x = &xo[0]

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

答案 1 :(得分:2)

错误在于: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