Z Kim
Z Kim

Reputation: 417

Where is the FILE struct allocated?

In C, when opening a file with

FILE *fin; 
fin=fopen("file.bin","rb"); 

I only have a pointer to a structure of FILE. Where is the actual FILE struct allocated on Windows machine? And does it contain all the necessary information for accessing the file?

My aim is to dump the whole data segment to disk and then to reload the dumped file back to the beginning of the data segment. The code that reloads the dumped file is placed in a separate function. This way, the fin pointer is local and is on the stack, thus is not being overwritten on reload. But the FILE struct itself is not local. I take care not to overwrite the memory region of size sizeof(FILE) that starts at the address fin.

The

fread(DataSegStart,1,szTillFin,fin);
fread(dummy,1,sizeof(FILE),fin);
fread(DataSegAfterFin,1,szFinTillEnd,fin);

operations completes successfully, but I get an assertion failure on

fclose(fin)

Do I overwrite some other necessary file data other than in the FILE struct?

Upvotes: 1

Views: 970

Answers (4)

Rob
Rob

Reputation: 15158

FILE is defined in stdio.h. It contains all the information about the file but, looking at the code you show, I think you don't understand its purpose. It is created and run through the operating system with the C library which fills FILE with information about the file but it is not contained in the file itself.

Upvotes: 0

CB Bailey
CB Bailey

Reputation: 792497

Well, you have a pointer to a FILE object, so technically you know where it is but you should be aware that FILE is deliberately an opaque type. You shouldn't need to know what it contains, you just need to know that you can pass it to functions that know about it to perform certain actions. Additionally, FILE may not be a complete type so sizeof(FILE) might not be correct and, additionally, the object might contain pointers to other structures. Simply avoiding overwriting the FILE object is not likely to be sufficient for you to avoid corrupting the program by writing over most of its memory.

Upvotes: 1

ugoren
ugoren

Reputation: 16441

It seems like you're trying to do something dangerous, unlikely to work.

fopen allocates a FILE structure and initializes it. fclose releases it. How it allocates it and what it puts in it is implementation dependent. It could contain a pointer to another piece of memory, which is also allocated somewhere (since it's buffered I/O, I guess it does allocate a buffer somewhere).

Writing code that relies on the internals of fopen is dangerous, most likely won't work, and surely won't be stable and portable.

Upvotes: 2

Greg Hewgill
Greg Hewgill

Reputation: 993891

The actual instance of the FILE structure exists within the standard library. Typically the standard library allocates some number of FILE structures, which may or may not be a fixed number of them. When you call fopen(), it returns a pointer to one of those structures.

The data within the FILE structure likely contains pointers to other things such as buffers. You're unlikely to be able to save and restore those structures to disk without some really deep integration with your standard library implementation.

You may be interested in something like CryoPID which does process save and restore at a different level.

Upvotes: 4

Related Questions