Re: Results of the memswap() smackdown from the thread "Sorting" assignment
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Tue, 12 Feb 2008 12:26:20 +0000
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.
.
- Follow-Ups:
- Re: Results of the memswap() smackdown from the thread "Sorting" assignment
- From: Richard Heathfield
- Re: Results of the memswap() smackdown from the thread "Sorting" assignment
- From: spinoza1111
- Re: Results of the memswap() smackdown from the thread "Sorting" assignment
- From: spinoza1111
- Re: Results of the memswap() smackdown from the thread "Sorting" assignment
- References:
- Results of the memswap() smackdown from the thread "Sorting" assignment
- From: spinoza1111
- Results of the memswap() smackdown from the thread "Sorting" assignment
- Prev by Date: Re: Swap function driver
- Next by Date: Re: "Sorting" assignment
- Previous by thread: Re: Results of the memswap() smackdown from the thread "Sorting" assignment
- Next by thread: Re: Results of the memswap() smackdown from the thread "Sorting" assignment
- Index(es):