Re: using const & in function prototypes
Doug Harrison [MVP] wrote:
The first thing that jumps out at me is that you're using #define instead
of typedefs. There is a big difference when you start using cv-qualifiers
(const and volatile) with them, not to mention pointer-declarators in some
cases. Macro-substituting TIndexFile::Read, we have:
void TIndexFile::Read(const DWORD &index, TIndexPage &page)
Then we have ReadPartPage, called from TIndexFile::Read:
ReadPartPage(index, &page.Header, 0, sizeof(page.Header));
And finally:
BOOL TDataFile::ReadPartPage(const DWORD &index,
void *data,
const DWORD & offset,
DWORD size)
I don't see any inconsistencies here. Maybe it's just too late and I don't
see any calls to TIndexFile::Read or any overloads, but that's what I would
be interested in seeing at this point.
I think I am zooming on the "why" this is happening which is what I
need to continue and not leave this hanging with an non-understood
gremlin.
One of the functions is a recursive b-tree analyzer which is what I am
using for testing. The recursive VerifyPage() is calling Read(). Its
the only function it is calling as it traverses the tree calling
VerifyPage() for each left and right page.
The solution was to remove the &, keeping const was ok. So maybe what
& did was keep a local scope value persistent or something along those
lines.
Make sense?
BOOL TIndexFile::Verify(int &count)
{
TCriticalSectionGrabber grab(Mutex);
ReadRoot();
return VerifyPage(Root, count);
}
BOOL TIndexFile::VerifyPage(const TIndexPage &page, int &count)
{
...
for (DWORD i = 0; i <= page.Header.KeyCount; i++) {
TIndexPage tpage(KeyLen);
if (!page.Header.Child[i]) {
return FALSE;
}
Read(page.Header.Child[i], tpage);
if (tpage.Header.ParentIndex != page.Header.ThisIndex) {
return FALSE;
}
if (!VerifyPage(tpage, count)) {
return FALSE;
}
}
...
return TRUE;
}
When the Read() prototype uses const DWORD & for parameter 1, it
doesn't Verify. Incidentally, as a matter of state info, it failed at
recursive level 2.
Changing it to const DWORD or just DWORD, resolve it.
There is no clobbering, so I like to know why.
Off hand, I would think that const & is more efficient, but it might
not apply here because of the recursive nature of VerifyPage().
---