I read that maximum memory malloc can
allocate is limited to physical
memory.(on heap)
Wrong: most computers/OSs support virtual memory, backed by disk space.
Some questions: Does malloc allocate memory from HD also?
malloc
asks the OS, which in turn may well use some disk space.
What was the reason for above
behaviour? Why didn't loop breaked at
any point of time.?
Why wasn't there any allocation
failure?
You just asked for too little at a time -- the loop would have broken eventually (well after your machine slowed to a crawl due to the large excess of virtual vs physical memory and the consequent super-frequent disk access, an issue known as "thrashing") but it exhausted your patience well before then. Try getting e.g. a megabyte at a time instead.
when a program exceeds consumption of
memory to a certain level, the
computer stops working because other
applications do not get enough memory
that they require.
A total stop is unlikely, but when an operation that normally would take a few microseconds ends up taking (e.g.) tens of milliseconds, those four orders of magnitude may certainly make it feel as if the computer had basically stopped -- what would normally take a minute would take a week, etc;-).