首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >程序在数组中设置值时冻结使用for循环,C Devkitpro 3DS

程序在数组中设置值时冻结使用for循环,C Devkitpro 3DS
EN

Stack Overflow用户
提问于 2021-08-09 13:04:52
回答 1查看 76关注 0票数 1

对于3DS的类似于家酿的克隆,我试图创建一个数据结构,称为包含一个位置的块,以及一个int的三维数组,其中数组中的int表示一个块ID。

其定义如下:

代码语言:javascript
复制
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;

为了填充一个块,我有一个函数,它接受一个指向变量的指针。它的意思是用它应该包含的块ID填充‘块’数组。

但是,这种情况不会发生,程序将挂起执行。其职能如下:

代码语言:javascript
复制
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
}

被称为:

代码语言:javascript
复制
Chunk newChunk;
generate_chunk(&chunk);

所发生的情况是,每当以后引用数组或其任何值时,程序将挂起。

奇怪的是,如果我把调用函数放在if语句后面,程序仍然会冻结在第一个帧上,尽管它当时没有被调用。

更奇怪的是,如果我赋值时没有像这样的for循环:

代码语言:javascript
复制
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。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-12 12:05:19

好的,3DS似乎不允许3D阵列在1919年左右,当所有的面都是相等的时候,尽管如果你使尺寸不相等的话,这可能是灵活的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68712567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档