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

Posted

技术标签:

【中文标题】在 C++ 中使用指向我的数组的指针会给我一个“未处理的异常”吗?【英文标题】:Using a pointer to my arrays gives me an "Unhandled exception" in C++? 【发布时间】:2015-03-12 01:07:08 【问题描述】:

这是完整的代码供参考:

#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] ;

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

【问题讨论】:

你能试着把它归结为一个更简单的例子吗?仅此一项就可以为您澄清问题。 @sfjac 我已经尝试过这样做(在一个单独的项目中),但我无法使用作为此最小表示的代码在此之外重新创建错误:/ 也许我应该放弃指针并尝试在其他地方使其高效,直到我弄清楚发生了什么? 如果你使用向量和一个好的调试编译器,它会在越界访问时触发一个断点。 【参考方案1】:

我怀疑问题是

int *x = &amp;xo[NUMFLAKES];

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

int *x = &amp;xo[0]?

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

【讨论】:

【参考方案2】:

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

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

int *p;
int xo[NUMFLAKES];

p = xo; // <-- Now p is pointing to the first element of xO

【讨论】:

以上是关于在 C++ 中使用指向我的数组的指针会给我一个“未处理的异常”吗?的主要内容,如果未能解决你的问题,请参考以下文章

为啥在将唯一指针插入无序映射时 C++ 会给我一个错误?

c ++指针将结构新数组指向delphi到DLL函数

在 C++ 中使用指向数组中对象的指针会更好吗?

C++ 2DArray 对象;指针和数组问题

指向未指定大小数组“(*p)[]”的指针在 C++ 中是非法的,但在 C 中是合法的

如何在 C++ 中初始化指向动态二维数组的指针 [关闭]