1
1. File *fp;
2. fp = fopen ("/etc/myfile.txt", "w");
3. fclose(fp);

Now

I read this:

In statement 1, a 4 byte memory for the pointer is created of type 'FILE' on stack.

In statement 2, a memory = 'sizeof(FILE)' is allocated on heap, and address of which is assigned to the pointer 'fp'.

Can somebody explain more on statement 2. Are we allocating same heap size for all the files? How does the O.S know the size of FILE it should allocate?
At the low level what exactly happens in st 2.

3
  • A FILE * structure has nothing to do with any actual file in the sense that you think. It's just a descriptor structure, one that the OS uses later to perform I/O on the file. Its size is constant and it is completely unrelated to the size of the file it describes. Commented Feb 1, 2014 at 14:17
  • @H2CO3: To be precise, a FILE is not used by the OS; rather it's used by the C library (at least, this is true for Linux-like OSes). The OS itself works with file descriptors provided via syscalls. Commented Feb 1, 2014 at 14:33
  • @OliCharlesworth Yes, too bad I can't edit my comment now. Of course it is used by the stdlib. Commented Feb 1, 2014 at 14:41

2 Answers 2

5

Step 1 allocates a pointer on the stack (if it's in a function), or will refer to an existing reserved location in global space (data segment or similar) if outside a function - in the latter case this is reserved at compile time, i.e. determined by the compiler, rather than allocated at run time. The pointer will be 4 bytes in a 32 bit environment and 8 bytes in a 64 bit environment.

Step 2 calls fopen(). The line itself doesn't allocate memory, but the implementation of fopen() may well do. You don't know what this allocates, as it's implementation dependent. However, you can be pretty sure it's going to allocate a structure of size sizeof FILE. You also know that fopen() is for buffered I/O, so it might allocate a buffer, or this might be done the first time you use the file. You have no way of knowing, as it's implementation dependent. However, POSIX <stdio.h> specifies the buffer is of length BUFSIZ so it's a fair bet that when it is in fact allocated, it will be at least that size, so when the buffer does get allocated, it will be within a memory structure at least of size BUFSIZ. There is no requirement that this is on the heap (it might directly call mmap with MAP_ANON and retain a pointer to it); again, it's implementation dependent. However, a fair bet is that it's on the heap.

Step 3 calls fclose(). This will free the allocation made at step 2, but not the allocation (if any) made at step 1.

Re your question 'how does the OS know the size of FILE': FILE is a C structure (actually a typedef for a struct). As such its size is known to the C compiler, and will be passed to whatever allocator is used, e.g. the heap allocator. If the heap allocator is used, this won't even be visible to the OS, as it will be handled within your C library.

Perhaps you are confusing the size of the file on disk with the size of FILE. FILE is not the file on disk, it is a C typedef struct which is a control structure for the buffered I/O to that file, and will include (e.g.) the file descriptor. The file isn't (normally) loaded into RAM completely. A small section of it (the buffer) is loaded in, and the POSIX standard says that buffer shall be BUFSIZ in length. See: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/stdio.h.html

Sign up to request clarification or add additional context in comments.

Comments

0

The pointer allocated on the stack in step 1 is not necessarily 4 bytes since it depends on the implementation.

Step 2 allocates not only a FILE structure, but also a buffer (of BUFSIZ bytes) for the file, not space for the whole file. So it does not need to know how big the file is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.