I am studying max heap and in it there is a createheap function. In it , there is a for loop like this
for (i = 0; i < capacity; i++) {
h->arr[i] = nums[i];
}
if size of nums is not equal to capacity then does the variable i gives the size of nums or is it equal to capacity? (the for loop runs till i is equal to capacity ,right?)
...the for loop runs till i is equal to capacity ,right?
~~No.~~ Maybe.
When capacity
exceeds "the size of nums", once i
is equal to "the size of nums", further iterations lead to undefined behavior (UB). Anything may happen, including executing until i == capacity
, crashing due to accessing memory it doesn't own, replacing your hotel with a football stadium, etc.