-2

플로트 배열 (distances)을 동적으로 할당하려하지만 디버거를 단계별로 실행하면 어떤 이유로 든 하나만 할당된다는 것을 알 수 있습니다. 나는 std :: vector를 이미 시험해 보았지만 잘 작동하지만 다음은 Vec2 * points = Util::BCurvePerpPoint(i, p1, p2, p3, this->distances, this->m_NumGeoms * 2)에서 실패한다. 거리는 객체의 수명에 대한 크기를 변경하지 않으므로 std :: vector를 사용하는 것은 과도한 종류의 일입니다. 내가 std :: vector를 사용하고있는 유일한 것은 Draw 함수이다. (나는 크기가 고정되어 있거나 고정되어 있다고 생각한다. 그러나 step이 변경되면 배열의 크기가 너무 바뀔 것이다.) 그래 ..).동적 할당에 실패 했습니까? C++

참고 : 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(); 
    } 
} 

Debugging Image

+1

기본적인 질문 - 왜 모든 "동적 배열"에'std :: vector'를 사용하지 않으시겠습니까? 왜 어떤 장소에서는 벡터를 사용하고 다른 곳에서는 사용하지 않는 것이 좋습니다. – PaulMcKenzie

+0

'그래서 코드에 다른 결함 (예 : tmp)을 고려하여 std :: vector' 고전적 조기 최적화를 사용하는 것은 무리입니다. – strangeqargo

+3

* 문제는 거리가 객체의 수명에 따라 크기가 변경되지 않는다는 것입니다. std :: vector *를 사용하는 것은 과도한 일입니다. - 제 제안은'vector'를 사용하고 프로그램을 먼저 작동시키는 것입니다. 작동하지 않는 코드를 최적화하려고 시도하는 것은 의미가 없습니다. 또한 :'this-> line = new std :: vector [numGeoms];'- 정말로 벡터 배열을 원했습니까? – PaulMcKenzie

답변

0

이 은 분명히 m_Geoms는 비록 데이터를 못하고 있었다 ... 그것을 고정 생성자에서 나는 그것을로 지정했다..

HoldTail::HoldTail(HoldTailGeom * geoms, const unsigned int numGeoms) : m_NumGeoms(numGeoms), distances(nullptr), line(nullptr) { 
     this->line = new std::vector<SVF_P3F_C4B_T2F>[numGeoms]; 
     this->distances = new float[numGeoms * 2](); 
     this->m_Geoms = new HoldTailGeom[numGeoms]; 

     //distances = new std::vector<float>(); 

     for (int i = 0; i < numGeoms; i++) { //for each geometry 
      //copy data... 
      this->m_Geoms[i].d1 = geoms[i].d1; 
      this->m_Geoms[i].d2 = geoms[i].d2; 
      this->m_Geoms[i].color1 = geoms[i].color1; 
      this->m_Geoms[i].color2 = geoms[i].color2; 
      //convert the colors to uint32 for quicker assignment to vector data. 
      this->m_Geoms[i].packedCol1 = geoms[i].color1.pack_argb8888(); 
      this->m_Geoms[i].packedCol2 = 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; 
     delete[] this->m_Geoms; 
     //this->distances.clear(); 
    } 

문제를 해결 : 그것은을 통해 배열을 복사하지 않았다 ... 그래서 나는 각각 생성자와 deconstructor을 수정합니다. 자바 프로그래밍이 너무 오래되었습니다 ...>. <

관련 문제