malloc tries to reserve a memory block in my virtual memory, isn't it?
What happens if I request 3MB of virtual memory but only 2MB are free. 
Will the OS then provide more memory? So can virtual memory grow?
            Asked
            
        
        
            Active
            
        
            Viewed 78 times
        
    1
            
            
         
    
    
        今天春天
        
- 941
- 1
- 13
- 27
- 
                    5This is a very broad question that cannot be answered in a simple answer. Read up on memory management, heap management and virtual memory. https://en.wikipedia.org/wiki/C_dynamic_memory_allocation – xxbbcc Nov 19 '15 at 15:41
- 
                    1https://en.wikipedia.org/wiki/Virtual_memory – xxbbcc Nov 19 '15 at 15:42
- 
                    1(Not that relevant but) but a great read >>> http://stackoverflow.com/questions/3509714/i-can-use-more-memory-than-how-much-ive-allocated-with-malloc-why – Syed Mauze Rehan Nov 19 '15 at 15:46
- 
                    Actually I just wanted to know whether the system takes care about the process and continuously feeding it with memory or whether the process must request memory "personally". – 今天春天 Nov 19 '15 at 15:49
- 
                    I believe the behavior is unexpected, it could result in a buffer overflow scenario. You should read up. – Syed Mauze Rehan Nov 19 '15 at 15:50
- 
                    1I don't think this is a duplicate of the linked question. This is about requesting more virtual memory than available and a modern OS would handle that. The linked one is simply a writing beyond the allocated memory. – P.P Nov 19 '15 at 15:52
- 
                    About the buffer overflow: malloc can return addresses which were already allocated by a previous malloc? – 今天春天 Nov 19 '15 at 15:53
- 
                    1Regarding the second question (requesting 3 MB of virtual memory when only 2 MB are free): read about [paging](https://en.wikipedia.org/wiki/Paging) – R_Kapp Nov 19 '15 at 15:55
- 
                    The duplicate linked question does not provide an answer to my question at all. – 今天春天 Nov 19 '15 at 15:55
- 
                    @R_Kapp I request 3MB of virtual memory but only 2MB of virtual (not physical) memory are free. – 今天春天 Nov 19 '15 at 16:00
- 
                    In that event, in theory, you should get a NULL return. In practice, that's not exactly what happens due to settings in the OS that typically default to optimistically assume that the `malloc` worked until you try to use the pointer, when it will crash. – R_Kapp Nov 19 '15 at 16:03
- 
                    @R_Kapp so I get a pointer but I cannot use it because no memory is actually available? – 今天春天 Nov 19 '15 at 16:09
- 
                    @今天春天 On the buffer overflow, yes it might happen if your program keeps on doing malloc. – Syed Mauze Rehan Nov 19 '15 at 16:15
- 
                    @今天春天: That is correct. – R_Kapp Nov 19 '15 at 16:31
- 
                    Thanks, this actually helped alot. So I can assume that malloc only returns null if the system sees no way to get more memory? – 今天春天 Nov 19 '15 at 16:32