hello!

i have problem with program i made:
the program should define a pointer to pointer which points to dynamic array of Grade (class i made).
i increase the size of the array in function. so i made a new pointer to pointer which point to array of Grade (the class i made). i transferred the old array to new one, and finaly, before i going out of the function, i shoud transfer it's address to the first pointer i made:

{
int students=1;
	
	Grade ** numGrades=new Grade * [students];
	numGrades[0]=new Grade;
	assert(numGrades!=0);
	AddGrade(numGrades,students);	
}
void AddGrade(Grade ** _numGrades,int& _students)
{
	_students++;
	Grade ** gradeTemp=new Grade * [_students];
	for(int i=0;i<_students-1;i++)
		gradeTemp[i]=_numGrades[i];
	gradeTemp[i]=new Grade;

}

how can i transfer the address of "gradeTemp" to "numGrades"?

thank you,
Moshiko

>how can i transfer the address of "gradeTemp" to "numGrades"?
Return and assign:

Grade **AddGrade(Grade ** _numGrades,int& _students)
{
  // ...

  return _numGrades;
}

//...

numGrades = AddGrade ( _numGrades, students );

However, that still doesn't change the fact that as soon as you overwrite numGrades, you've created a memory leak as you no long have a reference to the memory that you originally allocated to numGrades.

yeah, this is reasonable... how didn't i think about it...
but memory leak, how can i avoid it happen? is there any option to use reference? my lecturer told me, if i understand her well, that it not possible to use reference to pointer, is it right?

Grade ** AddGrade(Grade ** _numGrades,int& _students)
{
	_students++;
	Grade ** gradeTemp=new Grade * [_students];
	for(int i=0;i<_students-1;i++)
		gradeTemp[i]=_numGrades[i];
        delete[] _numGrades;
	return gradeTemp;


}

or use a reference

void AddGrade(Grade **& _numGrades,int& _students)
{
	_students++;
	Grade ** gradeTemp=new Grade * [_students];
	for(int i=0;i<_students-1;i++)
		gradeTemp[i]=_numGrades[i];
        delete[] _numGrades;
	_numGrades =  gradeTemp;


}

I think your lecturer needs a boot up the pants. A reference and a pointer are very similar. You can have a reference to a pointer. consider the following:

void AddGrade(Grade ** &_numGrades, int& _students)
{
	_students++;
	Grade ** gradeTemp = new Grade * [_students];
	for(int i=0;i<_students-1;i++)
		gradeTemp[i]=_numGrades[i];

	gradeTemp[_students-1]=new Grade;
	
	delete[] _numGrades;
	
	_numGrades = gradeTemp;
   }

We passed a reference-of-a-(pointer-to-a-pointer), alternately we could have used a pointer-to-a-(pointer-to-a-pointer), but you need more ***, which becomes confusing; i.e. instead e.g. delete [] _numgrades we'd need delete[] *_numgrades.

The old array of pointers is freed, so no memory leak, and all the old pointers are copied across.

That said, can't you use a vector<Grade*> instead?? Much less prone to errors!

OK. I really am a slow typer

delete[] _numGrades;

you freed the memory in the function, and then you return it a new value to be pointed!!! it's great. i'll save this idea, it would be useful, even though i this the reference is more elegant. do i right or the 2 ways are useful the same?

or use a reference

delete[] _numGrades;
       _numGrades =  gradeTemp;

i saw you wrote it both: why should i delete _numGrades? it reference so just other name...

_numGrades is deleted because it's a reference to a pointer to the _old_ array of grades. You've copied this array into your new location (gradeTemp); it no longer has any use to you. So you delete the old array of pointers to stop a memory leak, and you the new array of pointers instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.