Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualmcgrane66

Posted 10 May 2012 - 07:04 PM

Hey, i am having problems with converting a list into an array in c++,
Any ideas whats gone wrong? Its causing segment faults.


#ifndef _CARRAY_H
#define _CARRAY_H
#include <fstream>

using namespace std;

template <class ALLTYPE> class cDataType {  
	 public:	  
	 ALLTYPE  Data;
	 cDataType *p_Next;
	 cDataType(): p_Next( NULL ) {
	 }
};
template <class ALLTYPE> class cArray {
	 int m_Size;
	 cDataType<ALLTYPE> *p_Head;
	 cDataType<ALLTYPE> *p_Tail;
	 public:
	 cArray() : m_Size( 0 ), p_Head( NULL ), p_Tail( NULL ) {
	 }
	 void Drop( ) {

		  cDataType<ALLTYPE> *ptr = p_Head;
		  while( ptr->p_Next != p_Tail ) {
				 ptr = ptr->p_Next;	
		  }
		  cDataType<ALLTYPE> *deleteNode = ptr->p_Next;
		  p_Tail = ptr;
		  m_Size--;
		  delete deleteNode;		
	 }
	 void Add( ALLTYPE l_firstPoint ) {
		  if( p_Head == NULL ) {
			   cDataType<ALLTYPE> *newNode = new cDataType<ALLTYPE>;
			   newNode->Data   = l_firstPoint;
			   newNode->p_Next = NULL;
			   p_Head = newNode;
			   p_Tail = newNode;
			   m_Size++;
		  }
		  else {
			   cDataType<ALLTYPE> *newNode = new cDataType<ALLTYPE>;
			   newNode->Data   = l_firstPoint;
			   newNode->p_Next = NULL;
			   p_Tail->p_Next = newNode;
			   p_Tail = newNode;		  
			   m_Size++;
		  }
	 }	
	 ALLTYPE *getArray() {
		   int index = 0;
		   ALLTYPE *array = new ALLTYPE[m_Size];
		   cDataType<ALLTYPE> *ptr = NULL;
		   ptr = p_Head;
		   while( ptr->p_Next != NULL ) {  // !#### Causes error here, or if set to ptr == NULL
				array[index] = ptr->Data;     // !#### Causes error here
				ptr = ptr->p_Next;
				index++;
		   }
		   return array;
	 }
	 int getSize() { return m_Size; }
};
#endif
It is being called here

glDrawElements( GL_TRIANGLE_STRIP, (indexArray->getSize()) -256, GL_UNSIGNED_INT, indexArray->getArray() );
and initialized here

indexArray   = new cArray<unsigned int>();


indexArray->Add( 0 );

	 int ySize = TERRAIN_DEPTH - 1;	   // Height of array
	 int xSize = TERRAIN_DEPTH * 2;	   // Width of array
	 unsigned int index  = TERRAIN_DEPTH;		  // First, Third, Fifth ... etc row
	 unsigned index2 = (TERRAIN_DEPTH*3)-1;	// Secound, Fourth Sixth ... etc row
	 // Fill the array
	 for( int y = 0; y < ySize; y+=2 ) {
		  for( int x = 0; x < xSize; x+=2 ) {							  
			   indexArray->Add( index++ );
			   indexArray->Add( index-TERRAIN_DEPTH );
		  }
		  indexArray->Drop();
		  for( int x = 0; x < xSize; x+=2 ) {			  
			   indexArray->Add( index2-- );
			   indexArray->Add( index2-TERRAIN_DEPTH );		  
		  }
		  indexArray->Drop();

		  index  = index  + TERRAIN_DEPTH;
		  index2 = index2 + (TERRAIN_DEPTH*3);
	 }

Thanks for any help !

#1mcgrane66

Posted 10 May 2012 - 07:00 PM

Hey, i am having problems with converting a list into an array in c++,
Any ideas whats gone wrong? Its causing segment faults.


#ifndef _CARRAY_H
#define _CARRAY_H
#include <fstream>

using namespace std;

template <class ALLTYPE> class cDataType {   
     public:       
     ALLTYPE  Data;
     cDataType *p_Next;
     cDataType(): p_Next( NULL ) {
     }
};
template <class ALLTYPE> class cArray { 
     int m_Size;
     cDataType<ALLTYPE> *p_Head;
     cDataType<ALLTYPE> *p_Tail;
     public:
     cArray() : m_Size( 0 ), p_Head( NULL ), p_Tail( NULL ) {
     }
     void Drop( ) {

          cDataType<ALLTYPE> *ptr = p_Head;
          while( ptr->p_Next != p_Tail ) {
                 ptr = ptr->p_Next;    
          }
          cDataType<ALLTYPE> *deleteNode = ptr->p_Next;
          p_Tail = ptr;
          m_Size--;
          delete deleteNode;        
     }
     void Add( ALLTYPE l_firstPoint ) {
          if( p_Head == NULL ) {
               cDataType<ALLTYPE> *newNode = new cDataType<ALLTYPE>;
               newNode->Data   = l_firstPoint;
               newNode->p_Next = NULL;
               p_Head = newNode;
               p_Tail = newNode;
               m_Size++;
          }
          else {
               cDataType<ALLTYPE> *newNode = new cDataType<ALLTYPE>;
               newNode->Data   = l_firstPoint;
               newNode->p_Next = NULL;
               p_Tail->p_Next = newNode;
               p_Tail = newNode;          
               m_Size++;
          }
     }    
     ALLTYPE *getArray() {
           int index = 0;
           ALLTYPE *array = new ALLTYPE[m_Size];
           cDataType<ALLTYPE> *ptr = NULL;
           ptr = p_Head;
           while( ptr->p_Next != NULL ) {
                array[index] = ptr->Data;
                ptr = ptr->p_Next;
                index++;
           }
           return array;
     }
     int getSize() { return m_Size; } 
};
#endif
It is being called here

glDrawElements( GL_TRIANGLE_STRIP, (indexArray->getSize()) -256, GL_UNSIGNED_INT, indexArray->getArray() );
and initialized here

indexArray   = new cArray<unsigned int>();


indexArray->Add( 0 );

     int ySize = TERRAIN_DEPTH - 1;       // Height of array
     int xSize = TERRAIN_DEPTH * 2;       // Width of array
     unsigned int index  = TERRAIN_DEPTH;          // First, Third, Fifth ... etc row
     unsigned index2 = (TERRAIN_DEPTH*3)-1;    // Secound, Fourth Sixth ... etc row
     // Fill the array
     for( int y = 0; y < ySize; y+=2 ) {
          for( int x = 0; x < xSize; x+=2 ) {                              
               indexArray->Add( index++ );
               indexArray->Add( index-TERRAIN_DEPTH );
          }
          indexArray->Drop();
          for( int x = 0; x < xSize; x+=2 ) {               
               indexArray->Add( index2-- );
               indexArray->Add( index2-TERRAIN_DEPTH );          
          }
          indexArray->Drop();

          index  = index  + TERRAIN_DEPTH;
          index2 = index2 + (TERRAIN_DEPTH*3); 
     } 

Thanks for any help !

PARTNERS