push_back class problem

hi guys! i've got problem when i'm trying to push class element into the vector. problem begins in copy constructor at line free(p_chrom);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Individual::Individual(const Individual& ind)
{
	//IND_PRNT("Individual copy constructor  called\n");
	if(this != &ind)
	{
		if(p_chrom != NULL){
			free(p_chrom);
			p_chrom = NULL;
		}

		if(&ind.p_chrom != NULL){
			p_chrom = static_cast<chrom_type*>( malloc(Individual::chroms.siz * Individual::chroms.count) );
			memcpy(p_chrom,ind.p_chrom,Individual::chroms.siz * Individual::chroms.count);
			fitness = ind.fitness;
			//probability = ind.probability;
		}
	}
};

push_back doesn't call class constructor before copy constructor so p_chrom contains some stuff that doesn't equal to NULL.
please, help me to get rid of this. thanks

here is destructor and constructor. I may have done something wrong there
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Individual::~Individual()
{
	//IND_PRNT("Individual destructor\n");
	if(p_chrom != NULL)
	{
		free(p_chrom);
		p_chrom = NULL;
	}
}

Individual::Individual(): fitness(0)//, probability(0)
{
	IND_PRNT("Individual constructor\n");
	IND_PRNT("chrom_size:%u; chrome_count:%i;\n",Individual::chroms.siz, Individual::chroms.count);
	p_chrom = static_cast<chrom_type*>( malloc(Individual::chroms.siz * Individual::chroms.count) );

	void * p = p_chrom;

	for(int  i = 0; i< Individual::chroms.count; i++){
		*p_chrom = GetChromosome();
		IND_PRNT("%p: chromosome[%i]: %lf\n", p_chrom, i , *p_chrom);//chromosome[i]);
		p_chrom++;
		}

	p_chrom = static_cast<chrom_type *>(p);
}



Topic archived. No new replies allowed.