views:

80

answers:

3

Suppose I want to read an entire file in memory. I'd open it in binary mode, use fseek to get to the end of the file, then do ftell to get its size. Then I would allocate a string with the same size as the file, and just read it in, right?

The problem is that ftell returns a long int, and malloc should receive a size_t argument. Now, size_t can be larger than long int as far as I can tell (I've just checked C: a reference manual by Harbison and Steele, and Chapter 11 mentions that size_t can be defined as unsigned long long, depending on the compiler). I suppose the opposite could be true (and that would be a real problem, since I'd be casting a long long into a long, or something like that)

So my question is how do I deal with that?

Thanks!

edit: thanks guys, that was really quick! I'll use mmap (and posix_madvise too, which I've found after diggin up info about mmap)!

+3  A: 

I think you want to use mmap() instead.

Nico
`mmap` here is incorrect because the string will not be null-terminated. Well, it will except in the corner case where the file size is exactly a multiple of the operating system's page size, and in that specific case, your program will crash or read into random other memory following the mapping.
R..
The assumption of using a null-terminated string to deal with bytes from a disk file is most likely wrong, especially when the word "binary" appears. Unless the file happens to be a serialized null-terminated string, it will in general not be possible to grab those bytes and treat them as a null-terminated string, whether through mmap or any other way of reading the file. In fact, if you do, it will most likely appear much shorter than your actual file size since there will probably be a null byte at some point in the file before the last byte of the file.
Nico
+1  A: 

I think the issue of casting is moot. Since the smaller of the two, long, has a maximum of +2^31 (if signed), if you read all of that at once you'd be occupying 2GB of memory. This is usually a bad idea.

Reinderien
+1  A: 

If your files are large enough for this to be an issue, they are also large enough for the memory allocation to almost certainly fail.

Either don't worry about it, or switch to an OS specific memory mapping technique.

Darron