I'm trying to write a program that finds the distance of an arbitrary amount of tugboats from a main ship and orders them from closest to furthest. This also involves removing tugboats that are deemd to be at an invalid location. However, in my sorting function I've encountered strange and seemingly inexplicable behavior in one of the local variables named validTugboatCount. This function is shown below:
void sortTugboats(tugboat * unsorted, long long int tugboatCount){
long long int validTugboatCount = 0;
for(int i = 0; i < tugboatCount; i++){
if(!unsorted[i].isInvalid)
validTugboatCount++;
} //ValidTugboatCount holds correct data, e.g 4
tugboat validUnsorteds[validTugboatCount]; //validTugboatCount is still at 4
for(int i = 0; i < tugboatCount; i++){
if(!unsorted[i].isInvalid)
validUnsorteds[i] = unsorted[i];
}
mergeSort(validUnsorteds, 0, validTugboatCount-1); //validTugboatCount's value inexplicably changes here, e.g becoming 140733193388031 in one instance
unsorted = validUnsorteds;
}
Weirdly enough, after introducing a printf() call to display the value of validTugboatCount right after the first for loop establishing its value, the problem seemingly corrects itself, as shown below:
void sortTugboats(tugboat * unsorted, long long int tugboatCount){
long long int validTugboatCount = 0;
for(int i = 0; i < tugboatCount; i++){
if(!unsorted[i].isInvalid)
validTugboatCount++;
} //validTugboatCount holds correct data, e.g 4
printf("%lld\n", validTugboatCount); //printf prints "4"
tugboat validUnsorteds[validTugboatCount]; //array becomes size 4
for(int i = 0; i < tugboatCount; i++){
if(!unsorted[i].isInvalid)
validUnsorteds[i] = unsorted[i];
}
mergeSort(validUnsorteds, 0, validTugboatCount-1); //validTugboatCount is at 4??
unsorted = validUnsorteds;
}
My initial guess was that the issue has something to do with the conversion of long long ints to ints, but even after converting every function's arguments to long long ints the problem still persists. This also doesn't explain why the issue corrects itself after inserting a printf(). Help would be greatly appreciated.
validTugboatCount is at 4 right before the call to mergeSort()
With the addition of the printf() statement
Tried: creating a variable that would store the number of valid tugboats by looping through a list of all tugboats and seeing whether the isInvalid flag is set to 0
Expected: validTugboatCount would hold the correct amount of valid tugboats
Actual: validTugboatCount holds correct data at first and then suddenly switches to holding random garbage data, always an extremely large number
As for the statement "tugboat validUnsorteds[validTugboatCount];" Will there be enough memory for a local variable array of that size? The stack's size is limited. I'd suggest using the heap instead.
Strange behaviour that is fixed by a printf was on my side often a result of another function not always returning a value when it should do so.
E.g. int32_t a(void) {
}
might sometimes lead to strange behaviour in other parts of the program.
Do all your other functions that return values do so in all execution parts?