-1

I am new to C++, and I am trying to program snake as a small project. I'm sitting on the problem of increasing the array-size for the coordinates of the snake. The way I am doing it currently is as following:

Defining arrays in start:

int *snakeX = new int[4];
    int *snakeY = new int[4];
    int snakeLength = 4;

Updating length upon picking up apple:

int *incSizeX = new int[snakeLength+1];
int *incSizeY = new int[snakeLength+1];
for(int i = 0; i<snakeLength; i++){
    incSizeX[i] = snakeX[i];
    incSizeY[i] = snakeY[i];
}
delete []snakeX;
delete []snakeY;
snakeX = incSizeX;
snakeY = incSizeY;
incSizeX = NULL;
incSizeY = NULL;
snakeLength +=1;

This was the method most people suggested when I was researching it. It gives me the following warning when running:

Thread 1 received signal SIGTRAP, Trace/breakpoint trap.
0x00007ff88b8aa313 in ntdll!RtlRegisterSecureMemoryCacheCallback () from C:\WINDOWS\SYSTEM32\ntdll.dll

It works fine when compiling and running from VSCode (as far as I know), but seems to crash when ran from the executable. So is there any better way to redefine the length of these arrays? (And secondary question, is there a good simple way to store coordinates in an array?). Appreciate any help:)

0 Answers0