3DS 用の自作の Minecraft のようなクローンの場合、位置を含むチャンクと呼ばれるデータ構造体と、配列内の int がブロック ID を表す int の 3D 配列を作成しようとしています。
次のように定義されています。
typedef struct{
Vector3 position; //Vector3 is a struct that simply contains xyz as floats, similar to Unity
int blocks[16][128][16]; //Array of ints, each int represents a block ID
} Chunk;
チャンクを埋めるために、変数へのポインタを取る関数があります。'blocks' 配列に含まれるブロック ID を入力するためのものです。
ただし、これは発生せず、プログラムは実行時にハングします。機能は次のとおりです。
void generate_chunk(Chunk *chunk)
{
int newBlocks[16][128][16]; //Create temp 3D array
int x,y,z; //For loop coordinate values
for(x=0; x<16; x++) { //Loop X
for(z=0; z<16; z++) { // Loop Z
for(y=0; y<128; y++) { // Loop Y
//<enum> BLOCK_GOLD_ORE = 6, as a test
newBlocks[x][y][z]=(int)BLOCK_GOLD_ORE; //Set the value in the position xyz of the array to 6
}
}
}
/* The runtime then freezes/crashes whenever the array "newBlocks" is referenced. */
printf("\x1b[14;2Hgenerate_chunk :: %i", newBlocks[0][0][0]); //Debug print //!Crashes
//! vvv Uncomment when I've solved the problem above
//! memcpy(chunk->blocks, newBlocks, sizeof(chunk->blocks)); //Copy the temp array to the struct
}
そして、次のように呼び出されます:
Chunk newChunk;
generate_chunk(&chunk);
これ以降、配列またはその値のいずれかが参照されるたびに、プログラムがハングします。
奇妙なことに、関数呼び出しを if ステートメントの後ろに置くと、その時点で呼び出されていないにもかかわらず、プログラムは最初のフレームでフリーズします。
さらに興味深いことに、次のように for ループを使用せずに値を代入すると、次のようになります。
void generate_chunk(Chunk *chunk)
{
int newBlocks[16][128][16]; //Create temp 3D array
newBlocks[0][0][0]=(int)BLOCK_GOLD_ORE; //Set its first value to 6 (enum)
printf("\x1b[14;2Hgenerate_chunk :: %i", newBlocks[0][0][0]); //Debug print, doesnt crash anymore
}
プログラムがハングしなくなりました。for ループを使用して値を割り当てようとすると、失敗するようです。明らかな何かが欠けている可能性がありますが、これはコンパイラの問題でさえある可能性があると考えるようになりました(おそらくそうではありません)
コンパイラは DEVKITPRO の GCC です。
ありがとう!