Re: Results of the memswap() smackdown from the thread "Sorting" assignment



spinoza1111 <spinoza1111@xxxxxxxxx> writes:

<snip>
// ***** spinoza1111's code *****
void spinoza1111_memswap(char *ptrToA,
char *ptrToB,
int intLength)
{
int intIndex1 = 0;
char chrExchange;
while (intIndex1 < intLength)
{
for (;
*(ptrToA + intIndex1) == *(ptrToB + intIndex1);
intIndex1++);

This code is not right. You can access outside the bound of the array
and if I plus this code into my test harness I get a segmentation
fault here on every run. I initially thought the ; at the end was a
typo (because including the if in the for loop, does correct this UB)
but that goes wrong in other cases, I now see. You need to correct
this before your code can be tested again the other suggestions.

if (intIndex1 >= intLength) break;
chrExchange = *(ptrToA + intIndex1);
*(ptrToA + intIndex1) = *(ptrToB + intIndex1);
*(ptrToB + intIndex1) = chrExchange;
intIndex1++;
}
}

--
Ben.
.