I've just started learning C and I decided to create a snake games using characters.
So I started building blocks for the games (functions and all). And I try to test each block individually.
So after some time I created the movement block and tested it. the program returned 0xC0000005 which appears to be illegal memory access error code.
After some tinkering I found that the problem is with the system("cls") function. I experimented with putting it elsewhere in the program and this behavior emerged:
If I use dynamic allocation the system("cls") no longer works.
This code works fine because the system("cls") is used before dynamic allocation:
#include <stdio.h>
main()
{
char ** grid;
int i;
system( "cls" );
grid = (char**)malloc( 16 * sizeof( char * ) );
for ( i = 0; i <= 16; ++i )
{
*(grid + i) = (char*)malloc( 75 * sizeof( char ) );
}
}
Whereas this code returns an error because it is called after the dynamic allocation:
#include <stdio.h>
main()
{
char ** grid;
int i;
grid = (char**)malloc( 16 * sizeof( char * ) );
for ( i = 0; i <= 16; ++i )
{
*(grid + i) = (char*)malloc( 75 * sizeof( char ) );
}
system( "cls" );
}
EDIT: after a bit of tinkering I found that reducing the size of each pointer allocated memory solves the problem which makes no sense