First of all, 1<<10
is an expression that can be calculated at compile time, and it equals 1024. So your code is equivalent to
#include <stdlib.h>
int main()
{
int num=0;
while(malloc(1024)) ++num;
}
So what it does is to allocate chunks of 1024 bytes of memory until it fails to do so. Each time, the value of num
is increased by one.
Overflowing the variable num
will cause undefined behavior because it is signed. However, since you are not using the variable, it is likely to be optimized away.
3
solved What does the following code segment do? [closed]