Problem with unique ID

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Zeus
Posts: 24
Joined: Sun Dec 13, 2009 1:03 pm
Location: Germany :P

Problem with unique ID

Post by Zeus »

Hi ...

whene analyzing my project cause of an error i had found an peculiar gab in the id series of my programm ...

the ids should by like [0,1,2,3,4,5,6 etc.]

but i found out that it is like [... 81,82,61820 ... etc] (see screenshot)

http://img525.imageshack.us/i/jumph.jpg/

the code of the id class is

_id.h

Code: Select all

#pragma once

class _id
{
private: static unsigned int _nextId;
private: unsigned int id;

private: _id(_id&);
private: _id operator =(_id&);

public: _id(void);
public: ~_id(void);
public: unsigned int get(void)const{return this->id;}
public: unsigned int next(void)const{return this->_nextId;}
};
_id.cpp

Code: Select all

#include "_id.h"

unsigned int _id::_nextId = 0;

_id::_id(void)
{
	this->id = this->_nextId;
	this->_nextId++;
}

_id::~_id(void)
{
}
its not realy an error ... everything works fine but i'm a bit puzzled ...
does anyone of you know because this occours

regards zeus
Is there any posible answer ... i'm sick of it ...

http://irrlicht.sourceforge.net/phpBB2/ ... 919#215919

thanks ...
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, constructors are also called for temporary objects, copy constructors, etc. So you have many intermediate calls there, which are deleted afterwards. You only see those of the long-living objects. You can be sure that the numbers will be strictly increasing, but almost never consecutive. Although this huge gap looks like a little too many constructors are called.
Zeus
Posts: 24
Joined: Sun Dec 13, 2009 1:03 pm
Location: Germany :P

Post by Zeus »

at first i thought the same ... but the class _id is only used one time in the GameBaseEntity Class ... which has also no (privat) copy ctr or operator = ...

GameBaseEntity.h

Code: Select all

#pragma once

#include "_irr.h"
#include "_id.h"

class MessageDispatcher;

class GameBaseEntity
{
private: _id id;
private: entity_type m_type;
private: GameBaseEntity(GameBaseEntity&);
private: GameBaseEntity operator = (GameBaseEntity&);

public: GameBaseEntity(	entity_type type,
					
						core::vector3df position,
						double collisionSize
						):	m_type(type),
							m_position(position),
							m_collisionSize(collisionSize)
							{}

public: virtual ~GameBaseEntity(void){};
public: virtual void Update( float elapsed) = 0;

public: unsigned int getID()const{return this->id.get();}
public: entity_type getType()const{return this->m_type;}
//---------- code omitted----------------//
};
thats only an abstract base class ... can't be copied, instantiate or what ever ... only used for handling the entitys by polymorphism ...
Is there any posible answer ... i'm sick of it ...

http://irrlicht.sourceforge.net/phpBB2/ ... 919#215919

thanks ...
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Also derived classes do call the constructor of the base class.
Zeus
Posts: 24
Joined: Sun Dec 13, 2009 1:03 pm
Location: Germany :P

Post by Zeus »

i know ,sorry. entitys are all stored as pointers and they are also called so. there should not be a new creation for functions etc ... in all derived classes the copy ctr ant op = is privat .there is only one function that can create derived classes, and these don't do that ... espacialy not 60k in only 10 seconds! thats the thing i'm wondering about ... is there no other possibility of this gab ? than i have to check my code again.
thanks for your help

EDIT:

i've founded the gab ... its within the expand function of my List class. there are X new elements created ...

Code: Select all

template<class TDerived>
void TList<TDerived>::expand()
{
	int newDimentions = this->m_capacity + this->m_capacity;
	TDerived* tmp = new TDerived[newDimentions];
	for(int x = 0; x < this->capacity(); x++)
	{
		tmp[x] = this->_member[x];
	}
	delete [] this->_member;
	this->_member = tmp;
	tmp = NULL;
	this->m_capacity = newDimentions;
}
in this case TDerived is GameBaseEntity. the problem is i don't know how to do this on an other way ... did you know an alternativ way ?
Is there any posible answer ... i'm sick of it ...

http://irrlicht.sourceforge.net/phpBB2/ ... 919#215919

thanks ...
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Depending on the usecase you could try to do a memcpy instead of assignment. Moreover, if it's really a list and not a vector/array, you can just add another field of elements somewhere else. Lists do not need to be consecutive in memory. Also, think about allocating enough elements in the beginning, that would save all those assignments as long as the list is empty.
Post Reply