【发布时间】:2016-10-04 23:40:04
【问题描述】:
我正在尝试动态分配一个浮点数组 (distances),但单步调试调试器显示它出于某种原因只分配了一个。
我已经尝试过 std::vector 并且它工作正常,但随后在Vec2 * points = Util::BCurvePerpPoint(i, p1, p2, p3, this->distances, this->m_NumGeoms * 2) 失败。问题是距离不会在对象的生命周期内改变大小,所以使用 std::vector 有点矫枉过正。我唯一使用 std::vector 的是 Draw 函数(我想我可以将其修复为静态大小或其他东西,但如果 step 发生变化,那么数组的大小将改变所以......是的......)。
注意:Util::BCurvePerpPoint 返回一个 Vec2 数组,其中第一个是贝塞尔曲线点 i。它所做的只是在贝塞尔曲线的切线的垂直线上提供点。函数原型:BCurvePerpPoint(float time, Vec2 p1, Vec2 p2, Vec2 p3, float * distance, const int numDists);
标题:
#ifndef _H_HOLDTAIL_
#define _H_HOLDTAIL_
#pragma once
#include "../../OD_Draw2d.h"
namespace Game {
struct HoldTailGeom {
float d1,d2;
ColorF color1, color2;
uint32 packedCol1, packedCol2;
HoldTailGeom() :
d1(0.0f),
d2(0.0f),
color1(0.0f, 0.0f, 0.0f, 0.0f),
color2(0.0f, 0.0f, 0.0f, 0.0f),
packedCol1(0),
packedCol2(0)
{};
};
class HoldTail {
public:
HoldTail(HoldTailGeom * geoms, const unsigned int numGeoms);
~HoldTail();
void Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, float step = 0.05f, bool isolatedDraw = false);
void Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, int numPoints, bool isolatedDraw = false);
private:
HoldTailGeom * m_Geoms;
unsigned int m_NumGeoms;
float * distances;
//std::vector<float> distances;
std::shared_ptr<OD_Draw2d> OD_Draw2dPtr;
std::vector<SVF_P3F_C4B_T2F> *line;
};
}
#endif //_H_HOLDTAIL_
代码:
#include <StdAfx.h>
#include "HoldTail.h"
namespace Game {
HoldTail::HoldTail(HoldTailGeom * geoms, const unsigned int numGeoms) : m_Geoms(geoms), m_NumGeoms(numGeoms) {
this->line = new std::vector<SVF_P3F_C4B_T2F>[numGeoms];
this->distances = new float[numGeoms * 2]();
//distances = new std::vector<float>();
for (int i = 0; i < numGeoms; i++) { //for each geometry
//convert the colors to uint32 for quicker assignment to vector data.
this->m_Geoms[i].packedCol1 = this->m_Geoms[i].color1.pack_argb8888();
this->m_Geoms[i].packedCol2 = this->m_Geoms[i].color2.pack_argb8888();
//convert the distances to an array that we can use.
//int i1 = i * 2;
//int i2 = (i * 2) + 1;
this->distances[i * 2] = m_Geoms[i].d1;
this->distances[(i * 2) + 1] = m_Geoms[i].d2;
//this->distances.push_back(m_Geoms[i].d1);
//this->distances.push_back(m_Geoms[i].d2);
}
//this->distances.shrink_to_fit();
}
HoldTail::~HoldTail() {
for (int i = 0; i < this->m_NumGeoms; i++) this->line[i].clear(); //clear the data just to be sure.
delete[] this->line;
delete[] this->distances;
//this->distances.clear();
}
void HoldTail::Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, int numPoints, bool isolatedDraw) {
if (tStart >= tEnd) return;
this->Draw(p1, p2, p3, tStart, tEnd, (float)((float)(tEnd - tStart) / (float)(numPoints)), isolatedDraw);
}
void HoldTail::Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, float step, bool isolatedDraw) {
if (tStart >= tEnd) return;
for (float i = tStart; i < tEnd+step; i += step) { //from start time to end time with a step between each
Vec2 * points = Util::BCurvePerpPoint(i, p1, p2, p3, this->distances, this->m_NumGeoms * 2); //calculate the distances at time i.
for (int i2 = 0; i2 < this->m_NumGeoms; i2++) { //for each geometry
SVF_P3F_C4B_T2F tmp;
//push back the vectors
tmp.xyz = Vec3(points[(i2 * 2)+1].x, points[(i2 * 2)+1].y, 1);
tmp.color.dcolor = this->m_Geoms[i2].packedCol1;
tmp.st = Vec2(0, 0);
this->line[i2].push_back(tmp);
tmp.xyz = Vec3(points[(i2 * 2)+2].x, points[(i2 * 2)+2].y, 1);
tmp.color.dcolor = this->m_Geoms[i2].packedCol2;
this->line[i2].push_back(tmp);
}
}
if (isolatedDraw) this->OD_Draw2dPtr->BeginDraw2d(1280, 720);
for (int i = 0; i < this->m_NumGeoms; i++) { //for each geometry
this->OD_Draw2dPtr->DrawTriangleStrip(&this->line[i][0], this->line[i].size()); //draw the line
this->line[i].clear(); //done using the line, clear it for next pass.
}
if (isolatedDraw) this->OD_Draw2dPtr->EndDraw2d();
}
}
【问题讨论】:
-
一个基本问题——为什么不对所有“动态数组”使用
std::vector?为什么在某些地方使用矢量,而在其他地方不使用它(可能是明智的)? -
so it's kind of overkill to use std::vector经典的过早优化,考虑到代码中的其他缺陷(例如 tmp) -
问题是,距离不会在对象的生命周期内改变大小,所以使用 std::vector 有点矫枉过正 - 我的建议是使用
vector并首先让您的程序运行。试图优化不起作用的代码没有多大意义。另外:this->line = new std::vector<SVF_P3F_C4B_T2F>[numGeoms];——你真的想要一个向量数组吗? -
另外,如果
HoldTail用作值对象(按值传递、按值返回、复制等),您的程序将完全损坏,因为HoldTail不遵守“规则” 3"。使用向量或类似容器代替指针可以缓解这个问题。 -
嗨,我同意@PaulMcKenzie 不要在 std::vector 上使用指针来创建具有适当大小的指针。对普通对象使用保留功能。其次在代码中使用智能指针而不是原始指针。
标签: c++ arrays visual-studio-2013 dynamic-arrays dynamic-allocation