Try to allocate in smaller chunks. At some point, you will allocate a size that cannot fit consecutively in memory in one block - and when this happens, the allocate will fail.
If you have 2 GB ram and 2 GB swap, you cannot expect that a 2GB alloc and another 2GB alloc will succeed. Perhaps even 3 x 1 GB allocs would fail, depending on the system and how it handles memory management. (If you are doing this in C, C++, remember you are dealing with the C libraries, generally and not the Memory manager itself)
Also - ensure that the c libraries you use are capable of handling large allocations. Some libraries have problems with pointer arithmetic at large allocation sizes as they navigate their structures of allocations.
And finally with regards to memory allocations, check to see if your c standard libraries creat a second heap, or if it is possible to create a heap and work it yourself with the system memory management calls rather than the c library itself.
The app shouldn't have crashed unless the code is not checking the return on an allocate or something like that. (the C libraries may do this too - they are not totally impervious to that sort of thing!)