C++ C++;围绕变量'';他被腐蚀了。(D3D9)
我知道有很多类似的问题,但我看不出有任何关于这个具体问题的。代码在发布模式下运行良好,但在调试模式下编译时给出了“变量'index'周围的堆栈已损坏”。我怎样才能修好它?提前谢谢 以下是问题代码:C++ C++;围绕变量'';他被腐蚀了。(D3D9),c++,directx,C++,Directx,我知道有很多类似的问题,但我看不出有任何关于这个具体问题的。代码在发布模式下运行良好,但在调试模式下编译时给出了“变量'index'周围的堆栈已损坏”。我怎样才能修好它?提前谢谢 以下是问题代码: // Create Saturn rings // create the vertices using the CUSTOMVERTEX struct #define num_vertices 1000 CUSTOMVERTEX vertices[num_vertices]; nSections =
// Create Saturn rings
// create the vertices using the CUSTOMVERTEX struct
#define num_vertices 1000
CUSTOMVERTEX vertices[num_vertices];
nSections = 150;
float outerRadius = (80000000 + escalaa[6]) / escalaa[6];
float innerRadius = (7000000 + escalaa[6]) / escalaa[6];
float endAngle = ToRadians(360);
float beginAngle = ToRadians(0);
float angle = endAngle - beginAngle;
for (unsigned int i = 0; i <= nSections; i+=2)
{
float t = (float)i / (float)nSections;
float theta = beginAngle + t * angle;
float s = (float)sin(theta);
float c = (float)cos(theta);
vertices[i] = { c * innerRadius, 0, -1 * (s * innerRadius), { 0.0f, 1.0f, 0.0f }, 4, 2 };
vertices[i + 1] = { c * outerRadius, 0, -1 * (s * outerRadius), { 0.0f, 1.0f, 0.0f }, 2, 4 };
}
// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(num_vertices* sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid; // a void pointer
// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();
// create the indices using an int array
short indices[num_vertices];
int aa=0;
for (int n = 0; n < num_vertices; n += 6)
{
indices[n] = aa;
indices[n + 1] = aa + 1;
indices[n + 2] = aa + 2;
indices[n + 3] = aa + 2;
indices[n + 4] = aa + 1;
indices[n + 5] = aa + 3;
aa += 2;
}
// create an index buffer interface called i_buffer
d3ddev->CreateIndexBuffer(3 * num_vertices* sizeof(short),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&i_buffer,
NULL);
// lock i_buffer and load the indices into it
i_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(indices));
i_buffer->Unlock();
//创建土星环
//使用CUSTOMVERTEX结构创建顶点
#定义num_顶点1000
自定义顶点[num_顶点];
n部分=150;
浮动外表面=(80000000+escalaa[6])/escalaa[6];
浮动内半径=(7000000+escalaa[6])/escalaa[6];
浮动端角=半径(360);
浮球起始角=ToRadians(0);
浮动角度=结束角度-开始角度;
对于(unsigned int i=0;i CreateVertexBuffer)(num_顶点*sizeof(CUSTOMVERTEX),
0,
自定义FVF,
D3DPOOL_管理,
&v_缓冲区,
无效);
VOID*pVoid;//一个VOID指针
//锁定v_缓冲区并将顶点加载到其中
v_buffer->Lock(0,0,(void**)和pVoid,0);
memcpy(pVoid,顶点,sizeof(顶点));
v_buffer->Unlock();
//使用int数组创建索引
短索引[num_顶点];
int aa=0;
对于(int n=0;nCreateIndexBuffer(3*num_顶点*sizeof(短),
0,
D3DFMT_指数16,
D3DPOOL_管理,
&我需要一个缓冲区,
无效);
//锁定i_缓冲区并将索引加载到其中
i_buffer->Lock(0,0,(void**)和pVoid,0);
memcpy(pVoid,index,sizeof(index));
i_buffer->Unlock();
此循环已中断:
for (int n = 0; n < num_vertices; n += 6)
{
indices[n] = aa;
indices[n + 1] = aa + 1;
indices[n + 2] = aa + 2;
indices[n + 3] = aa + 2;
indices[n + 4] = aa + 1;
indices[n + 5] = aa + 3;
aa += 2;
}
for(int n=0;n
您需要测试(n+5)
。最终n
将是996,小于num\u顶点
(1000),因此循环将继续运行,但996+4是1000,超出数组边界。读取或写入索引[n+4]
(及以上)因此,当n
达到996时,这是一种未定义的行为,写入它似乎会损坏堆栈
注意,以这种方式更改循环条件将导致索引[996]
到索引[999]
未初始化!这意味着您对d3ddev->CreateIndexBuffer()的调用可能会触发对未初始化对象的读取,这也是未定义的行为。您可以考虑重新定义<代码> NothVistes < /C> > 6的倍数以避免此问题。
请注意,未定义的行为可能会导致任何行为,包括程序“运行良好”或至少看起来运行良好。此循环被中断:
for (int n = 0; n < num_vertices; n += 6)
{
indices[n] = aa;
indices[n + 1] = aa + 1;
indices[n + 2] = aa + 2;
indices[n + 3] = aa + 2;
indices[n + 4] = aa + 1;
indices[n + 5] = aa + 3;
aa += 2;
}
for(int n=0;n
您需要测试(n+5)
。最终n
将是996,小于num\u顶点
(1000),因此循环将继续运行,但996+4是1000,超出数组边界。读取或写入索引[n+4]
(及以上)因此,当n
达到996时,这是一种未定义的行为,写入它似乎会损坏堆栈
注意,以这种方式更改循环条件将导致索引[996]
到索引[999]
未初始化!这意味着您对d3ddev->CreateIndexBuffer()的调用可能会触发对未初始化对象的读取,这也是未定义的行为。您可以考虑重新定义<代码> NothVistes < /C> > 6的倍数以避免此问题。
请注意,未定义的行为可能会导致任何行为,包括程序“运行良好”或至少看起来运行良好。此循环被中断:
for (int n = 0; n < num_vertices; n += 6)
{
indices[n] = aa;
indices[n + 1] = aa + 1;
indices[n + 2] = aa + 2;
indices[n + 3] = aa + 2;
indices[n + 4] = aa + 1;
indices[n + 5] = aa + 3;
aa += 2;
}
for(int n=0;n
您需要测试(n+5)
。最终n
将是996,小于num\u顶点
(1000),因此循环将继续运行,但996+4是1000,超出数组边界。读取或写入索引[n+4]
(及以上)因此,当n
达到996时,这是一种未定义的行为,写入它似乎会损坏堆栈
注意,以这种方式更改循环条件将导致索引[996]
到索引[999]
未初始化!这意味着您对d3ddev->CreateIndexBuffer()的调用可能会触发对未初始化对象的读取,这也是未定义的行为。您可以考虑重新定义<代码> NothVistes < /C> > 6的倍数以避免此问题。
请注意,未定义的行为可能会导致任何行为,包括程序“运行良好”或至少看起来运行良好。此循环被中断:
for (int n = 0; n < num_vertices; n += 6)
{
indices[n] = aa;
indices[n + 1] = aa + 1;
indices[n + 2] = aa + 2;
indices[n + 3] = aa + 2;
indices[n + 4] = aa + 1;
indices[n + 5] = aa + 3;
aa += 2;
}
for(int n=0;n
您需要测试(n+5)
。最终n
将是996,小于num\u顶点
(1000),因此循环将继续