c++ - 使用指向我的数组的指针在 C++ 中给我一个 "Unhandled exception"?

标签 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[] 的指针(不是指针数组,而是指向数组的指针)让程序只使用这些对象,而不是复制和更改它们。

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

我也试过:

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

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

最佳答案

我怀疑是这个问题

int *x = &xo[NUMFLAKES];

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

int *x = &xo[0]?

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

关于c++ - 使用指向我的数组的指针在 C++ 中给我一个 "Unhandled exception"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29000261/

相关文章:

c - 具有结构体和指针的算法

pointers - 返回类型与 struct golang 相同

c++ - std::shared_ptr 的用法

c++ - 如何在 C++ 中将 yuy2 转换为 BITMAP

Javascript - 原型(prototype)函数名称作为关联数组的键返回

c++ - 发布由指针管理的列表的添加和打印元素

c++ - Windows Mobile Hopper 测试工具,FocusApp

C++字节序会影响fstream的读写功能吗?

java - 无法将用户输入传递给方法调用中的 main

arrays - 如何在 PowerShell 中比较两个 DataRow 对象数组?