title: can't create a file larger than 2GB on 64 bit linux system with mmap/malloc/open etc
body: <p>OK I know questions like this have been asked in various forms before and I have read them all and tried everything that has been suggested but I still cannot create a file that is more than 2GB on a 64bit system using malloc, open, lseek, blah blah every trick under the sun.</p>
  
  <p>Clearly I'm writing c here. I'm running Fedora 20, I'm actually trying to mmap the file but that is not where it fails, my original method was to use open(), then lseek to the position where the file should end which in this case is at 3GB, edit: and then write a byte at the file end position to actually create the file of that size, and then mmap the file. I cannot lseek to past 2GB. I cannot malloc more than 2GB either. ulimit -a etc all show unlimited, /etc/security/limits.conf shows nothing, ....</p>
  
  <p>when I try to lseek past 2GB I get EINVAL for errno and the ret val of lseek is -1.edit: The size parameter to lseek is of type off_t which is defined as a long int (64bit signed), not size_t as I said previously.</p>
  
  <p>edit: 
  I've already tried defining _LARGEFILE64_SOURCE &amp; _FILE_OFFSET_BITS 64 and it made no difference.
  I'm also compiling specifically for 64bit i.e. -m64</p>
  
  <p>I'm lost. I have no idea why I cant do this.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Thanks.</p>
  
  <p>edit: I've removed a lot of completely incorrect babbling on my part and some other unimportant ramblings that have been dealt with later on. </p>
  
  <p>My 2GB problem was in the horribly sloppy interchanging of multiple different types. Mixing of signed and unsigned being the problem. Essentially the 3GB position I was passing to lseek was being interpreted/turned into a position of -1GB and clearly lseek didnt like that. So my bad. Totally stupid.</p>
  
  <p>I am going to change to using posix_fallocate() as p_l suggested. While it does remove one function call i.e. only need posix_fallocate instead of an lseek and then a write, for me that isn't significant, it is the fact that posix_fallocate is doing exactly what I want directly which the lseek method doesn't. So thanks in particular to p_l for suggesting that, and a special thanks to NominalAnimal whose persistence that he knew better indirectly lead me to the realisation that I cant count which in turn led me to accept that posix_fallocate would work and so change to using it.</p>
  
  <p>Regardless of the end method I used. The problem of 2GB was entirely my own crap coding and thanks again to EOF, chux, p_l and Jonathon Leffler who all contributed information and suggestions that lead me to the problem I had created for myself.</p>
  
  <p>I've included a shorter version of this in an answer.</p>