why the size of FILE structure instance is 216 bytes in 64 bit os and 148 Bytes in 32 bit os?












2















If I print the size of FILE or FILE instance then the results are 216 Bytes in
64 Bit system and 148 Bytes in 32 Bit system (OS : Ubuntu 16.04 , compile :GCC)



 printf("size : %zu",sizeof(FILE)); 


OR



FILE *fp;
printf("size : %zu",sizeof(*fp));


can anyone explain why it is showing this much size , as I checked the structure members are mostly pointers .










share|improve this question




















  • 7





    The implementation's internal format for FILE notwithstanding (you shoudn't have to care) why are you using %d for printing a size_t expression? That should be %zu. And if the members are "mostly pointers" consider that each pointer in a 32bit vs 64bit representation will literally double in size going from the former to the latter.

    – WhozCraig
    Nov 26 '18 at 7:11








  • 7





    It most certainly does play an important role. Using the wrong format specifier is basically lying to printf, and invokes undefined behavior if/when the arguments are not of the precise type expected by the corresponding specifier. There's enough UB in the coding world; no reason to add to it with trivialities, especially when they're simple enough to just do the correct thing in the first place. Ex: if size_t and int are 64 and 32 bit respectively on your architecture (most 64bit arches are such), then you'd be shoving a 64bit value to a function expecting a 32bit value.

    – WhozCraig
    Nov 26 '18 at 7:21






  • 1





    I checked with %zu and updated the same , thank you .

    – new_learner
    Nov 26 '18 at 7:24






  • 1





    BTW, in practice, a FILE takes much more resources: some buffer (kilobytes) and a file descriptor (resource known to the operating system kernel)

    – Basile Starynkevitch
    Nov 26 '18 at 7:53








  • 1





    Which values did you expect and why?

    – Gerhardh
    Nov 26 '18 at 9:04
















2















If I print the size of FILE or FILE instance then the results are 216 Bytes in
64 Bit system and 148 Bytes in 32 Bit system (OS : Ubuntu 16.04 , compile :GCC)



 printf("size : %zu",sizeof(FILE)); 


OR



FILE *fp;
printf("size : %zu",sizeof(*fp));


can anyone explain why it is showing this much size , as I checked the structure members are mostly pointers .










share|improve this question




















  • 7





    The implementation's internal format for FILE notwithstanding (you shoudn't have to care) why are you using %d for printing a size_t expression? That should be %zu. And if the members are "mostly pointers" consider that each pointer in a 32bit vs 64bit representation will literally double in size going from the former to the latter.

    – WhozCraig
    Nov 26 '18 at 7:11








  • 7





    It most certainly does play an important role. Using the wrong format specifier is basically lying to printf, and invokes undefined behavior if/when the arguments are not of the precise type expected by the corresponding specifier. There's enough UB in the coding world; no reason to add to it with trivialities, especially when they're simple enough to just do the correct thing in the first place. Ex: if size_t and int are 64 and 32 bit respectively on your architecture (most 64bit arches are such), then you'd be shoving a 64bit value to a function expecting a 32bit value.

    – WhozCraig
    Nov 26 '18 at 7:21






  • 1





    I checked with %zu and updated the same , thank you .

    – new_learner
    Nov 26 '18 at 7:24






  • 1





    BTW, in practice, a FILE takes much more resources: some buffer (kilobytes) and a file descriptor (resource known to the operating system kernel)

    – Basile Starynkevitch
    Nov 26 '18 at 7:53








  • 1





    Which values did you expect and why?

    – Gerhardh
    Nov 26 '18 at 9:04














2












2








2








If I print the size of FILE or FILE instance then the results are 216 Bytes in
64 Bit system and 148 Bytes in 32 Bit system (OS : Ubuntu 16.04 , compile :GCC)



 printf("size : %zu",sizeof(FILE)); 


OR



FILE *fp;
printf("size : %zu",sizeof(*fp));


can anyone explain why it is showing this much size , as I checked the structure members are mostly pointers .










share|improve this question
















If I print the size of FILE or FILE instance then the results are 216 Bytes in
64 Bit system and 148 Bytes in 32 Bit system (OS : Ubuntu 16.04 , compile :GCC)



 printf("size : %zu",sizeof(FILE)); 


OR



FILE *fp;
printf("size : %zu",sizeof(*fp));


can anyone explain why it is showing this much size , as I checked the structure members are mostly pointers .







c file data-structures file-handling






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 7:23







new_learner

















asked Nov 26 '18 at 7:05









new_learnernew_learner

11410




11410








  • 7





    The implementation's internal format for FILE notwithstanding (you shoudn't have to care) why are you using %d for printing a size_t expression? That should be %zu. And if the members are "mostly pointers" consider that each pointer in a 32bit vs 64bit representation will literally double in size going from the former to the latter.

    – WhozCraig
    Nov 26 '18 at 7:11








  • 7





    It most certainly does play an important role. Using the wrong format specifier is basically lying to printf, and invokes undefined behavior if/when the arguments are not of the precise type expected by the corresponding specifier. There's enough UB in the coding world; no reason to add to it with trivialities, especially when they're simple enough to just do the correct thing in the first place. Ex: if size_t and int are 64 and 32 bit respectively on your architecture (most 64bit arches are such), then you'd be shoving a 64bit value to a function expecting a 32bit value.

    – WhozCraig
    Nov 26 '18 at 7:21






  • 1





    I checked with %zu and updated the same , thank you .

    – new_learner
    Nov 26 '18 at 7:24






  • 1





    BTW, in practice, a FILE takes much more resources: some buffer (kilobytes) and a file descriptor (resource known to the operating system kernel)

    – Basile Starynkevitch
    Nov 26 '18 at 7:53








  • 1





    Which values did you expect and why?

    – Gerhardh
    Nov 26 '18 at 9:04














  • 7





    The implementation's internal format for FILE notwithstanding (you shoudn't have to care) why are you using %d for printing a size_t expression? That should be %zu. And if the members are "mostly pointers" consider that each pointer in a 32bit vs 64bit representation will literally double in size going from the former to the latter.

    – WhozCraig
    Nov 26 '18 at 7:11








  • 7





    It most certainly does play an important role. Using the wrong format specifier is basically lying to printf, and invokes undefined behavior if/when the arguments are not of the precise type expected by the corresponding specifier. There's enough UB in the coding world; no reason to add to it with trivialities, especially when they're simple enough to just do the correct thing in the first place. Ex: if size_t and int are 64 and 32 bit respectively on your architecture (most 64bit arches are such), then you'd be shoving a 64bit value to a function expecting a 32bit value.

    – WhozCraig
    Nov 26 '18 at 7:21






  • 1





    I checked with %zu and updated the same , thank you .

    – new_learner
    Nov 26 '18 at 7:24






  • 1





    BTW, in practice, a FILE takes much more resources: some buffer (kilobytes) and a file descriptor (resource known to the operating system kernel)

    – Basile Starynkevitch
    Nov 26 '18 at 7:53








  • 1





    Which values did you expect and why?

    – Gerhardh
    Nov 26 '18 at 9:04








7




7





The implementation's internal format for FILE notwithstanding (you shoudn't have to care) why are you using %d for printing a size_t expression? That should be %zu. And if the members are "mostly pointers" consider that each pointer in a 32bit vs 64bit representation will literally double in size going from the former to the latter.

– WhozCraig
Nov 26 '18 at 7:11







The implementation's internal format for FILE notwithstanding (you shoudn't have to care) why are you using %d for printing a size_t expression? That should be %zu. And if the members are "mostly pointers" consider that each pointer in a 32bit vs 64bit representation will literally double in size going from the former to the latter.

– WhozCraig
Nov 26 '18 at 7:11






7




7





It most certainly does play an important role. Using the wrong format specifier is basically lying to printf, and invokes undefined behavior if/when the arguments are not of the precise type expected by the corresponding specifier. There's enough UB in the coding world; no reason to add to it with trivialities, especially when they're simple enough to just do the correct thing in the first place. Ex: if size_t and int are 64 and 32 bit respectively on your architecture (most 64bit arches are such), then you'd be shoving a 64bit value to a function expecting a 32bit value.

– WhozCraig
Nov 26 '18 at 7:21





It most certainly does play an important role. Using the wrong format specifier is basically lying to printf, and invokes undefined behavior if/when the arguments are not of the precise type expected by the corresponding specifier. There's enough UB in the coding world; no reason to add to it with trivialities, especially when they're simple enough to just do the correct thing in the first place. Ex: if size_t and int are 64 and 32 bit respectively on your architecture (most 64bit arches are such), then you'd be shoving a 64bit value to a function expecting a 32bit value.

– WhozCraig
Nov 26 '18 at 7:21




1




1





I checked with %zu and updated the same , thank you .

– new_learner
Nov 26 '18 at 7:24





I checked with %zu and updated the same , thank you .

– new_learner
Nov 26 '18 at 7:24




1




1





BTW, in practice, a FILE takes much more resources: some buffer (kilobytes) and a file descriptor (resource known to the operating system kernel)

– Basile Starynkevitch
Nov 26 '18 at 7:53







BTW, in practice, a FILE takes much more resources: some buffer (kilobytes) and a file descriptor (resource known to the operating system kernel)

– Basile Starynkevitch
Nov 26 '18 at 7:53






1




1





Which values did you expect and why?

– Gerhardh
Nov 26 '18 at 9:04





Which values did you expect and why?

– Gerhardh
Nov 26 '18 at 9:04












2 Answers
2






active

oldest

votes


















4















the results are 216 Bytes in 64 Bit system and 148 Bytes in 32 Bit system



can anyone explain why it is showing this much size , as I checked the structure members are mostly pointers .




The contents of FILE structure are implementation-specific (which means they are different in different platforms).



On a 32-bit system, the size of a pointer is usually 4 bytes and on a 64-bit system the size of a pointer is usually 8 bytes.



So the difference in the size you see (216 - 148 = 68) can be easily accounted for. (From what I remember this structure has about 15 pointers in Ubuntu GCC).
And apart from pointers the sizes of other types like int, long (which can be part of FILE structure) etc can differ from 32 and 64 bits systems.



Including @MatteoItalia comments below:



In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).



As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit)






share|improve this answer


























  • thank you , I got your point about difference between 32bit and 64 bit system . Now can you please explain why the size is 148 for 32 bit os.

    – new_learner
    Nov 26 '18 at 7:26






  • 1





    @new_learner because there's 148 byte of data in there? It's really implementation-specific, depending from your system headers; dig them up from your compiler includes directory, sum up the fields sizes (taking padding in account) and you'll see that it'll add up to 148.

    – Matteo Italia
    Nov 26 '18 at 7:28








  • 1





    In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).

    – Matteo Italia
    Nov 26 '18 at 7:35








  • 1





    As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit).

    – Matteo Italia
    Nov 26 '18 at 7:40








  • 1





    @MatteoItalia: Can I include your comments in the answer?

    – P.W
    Nov 26 '18 at 7:42



















0














The contents and size of the FILE structure is entirely specific to the implementation. it happens to be 216 bytes on your OS in 64-bit mode and 148 in 32-bit mode. The difference could come from the differing sizes of some of the members of this structure between 32-bit and 64-bit mode: pointers are larger, as well as size_t and file positions, it really depends on how the C library handles the standard streams.



Note that some extra information might be stored outside the structure... in any case you cannot save a FILE to a local variable and restore it safely.



Note that FILE could also be an incomplete type: a typedef for a pointer to a structure whose definition is absent, in which case sizeof(FILE) would cause a compilation error. The main reason to put the definition in the <stdio.h> header is to allow macros to access members directly. Take a look into your <stdio.h> file and look for other implementations in open source, such as the newlib or the glibc.



Furthermore, the C Standard does not mandate that FILE be a typedef for a structure: it might not be a structure, it could be defined as an int.






share|improve this answer


























  • @Note that FILE might not be a structure at all. It could be defined as an int . FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h)

    – new_learner
    Nov 26 '18 at 9:06








  • 2





    @new_learner FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h) That's true only for the one implementation you looked at. There are others - many others.

    – Andrew Henle
    Nov 26 '18 at 10:12













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476148%2fwhy-the-size-of-file-structure-instance-is-216-bytes-in-64-bit-os-and-148-bytes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4















the results are 216 Bytes in 64 Bit system and 148 Bytes in 32 Bit system



can anyone explain why it is showing this much size , as I checked the structure members are mostly pointers .




The contents of FILE structure are implementation-specific (which means they are different in different platforms).



On a 32-bit system, the size of a pointer is usually 4 bytes and on a 64-bit system the size of a pointer is usually 8 bytes.



So the difference in the size you see (216 - 148 = 68) can be easily accounted for. (From what I remember this structure has about 15 pointers in Ubuntu GCC).
And apart from pointers the sizes of other types like int, long (which can be part of FILE structure) etc can differ from 32 and 64 bits systems.



Including @MatteoItalia comments below:



In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).



As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit)






share|improve this answer


























  • thank you , I got your point about difference between 32bit and 64 bit system . Now can you please explain why the size is 148 for 32 bit os.

    – new_learner
    Nov 26 '18 at 7:26






  • 1





    @new_learner because there's 148 byte of data in there? It's really implementation-specific, depending from your system headers; dig them up from your compiler includes directory, sum up the fields sizes (taking padding in account) and you'll see that it'll add up to 148.

    – Matteo Italia
    Nov 26 '18 at 7:28








  • 1





    In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).

    – Matteo Italia
    Nov 26 '18 at 7:35








  • 1





    As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit).

    – Matteo Italia
    Nov 26 '18 at 7:40








  • 1





    @MatteoItalia: Can I include your comments in the answer?

    – P.W
    Nov 26 '18 at 7:42
















4















the results are 216 Bytes in 64 Bit system and 148 Bytes in 32 Bit system



can anyone explain why it is showing this much size , as I checked the structure members are mostly pointers .




The contents of FILE structure are implementation-specific (which means they are different in different platforms).



On a 32-bit system, the size of a pointer is usually 4 bytes and on a 64-bit system the size of a pointer is usually 8 bytes.



So the difference in the size you see (216 - 148 = 68) can be easily accounted for. (From what I remember this structure has about 15 pointers in Ubuntu GCC).
And apart from pointers the sizes of other types like int, long (which can be part of FILE structure) etc can differ from 32 and 64 bits systems.



Including @MatteoItalia comments below:



In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).



As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit)






share|improve this answer


























  • thank you , I got your point about difference between 32bit and 64 bit system . Now can you please explain why the size is 148 for 32 bit os.

    – new_learner
    Nov 26 '18 at 7:26






  • 1





    @new_learner because there's 148 byte of data in there? It's really implementation-specific, depending from your system headers; dig them up from your compiler includes directory, sum up the fields sizes (taking padding in account) and you'll see that it'll add up to 148.

    – Matteo Italia
    Nov 26 '18 at 7:28








  • 1





    In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).

    – Matteo Italia
    Nov 26 '18 at 7:35








  • 1





    As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit).

    – Matteo Italia
    Nov 26 '18 at 7:40








  • 1





    @MatteoItalia: Can I include your comments in the answer?

    – P.W
    Nov 26 '18 at 7:42














4












4








4








the results are 216 Bytes in 64 Bit system and 148 Bytes in 32 Bit system



can anyone explain why it is showing this much size , as I checked the structure members are mostly pointers .




The contents of FILE structure are implementation-specific (which means they are different in different platforms).



On a 32-bit system, the size of a pointer is usually 4 bytes and on a 64-bit system the size of a pointer is usually 8 bytes.



So the difference in the size you see (216 - 148 = 68) can be easily accounted for. (From what I remember this structure has about 15 pointers in Ubuntu GCC).
And apart from pointers the sizes of other types like int, long (which can be part of FILE structure) etc can differ from 32 and 64 bits systems.



Including @MatteoItalia comments below:



In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).



As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit)






share|improve this answer
















the results are 216 Bytes in 64 Bit system and 148 Bytes in 32 Bit system



can anyone explain why it is showing this much size , as I checked the structure members are mostly pointers .




The contents of FILE structure are implementation-specific (which means they are different in different platforms).



On a 32-bit system, the size of a pointer is usually 4 bytes and on a 64-bit system the size of a pointer is usually 8 bytes.



So the difference in the size you see (216 - 148 = 68) can be easily accounted for. (From what I remember this structure has about 15 pointers in Ubuntu GCC).
And apart from pointers the sizes of other types like int, long (which can be part of FILE structure) etc can differ from 32 and 64 bits systems.



Including @MatteoItalia comments below:



In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).



As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 11:27

























answered Nov 26 '18 at 7:23









P.WP.W

14.6k31250




14.6k31250













  • thank you , I got your point about difference between 32bit and 64 bit system . Now can you please explain why the size is 148 for 32 bit os.

    – new_learner
    Nov 26 '18 at 7:26






  • 1





    @new_learner because there's 148 byte of data in there? It's really implementation-specific, depending from your system headers; dig them up from your compiler includes directory, sum up the fields sizes (taking padding in account) and you'll see that it'll add up to 148.

    – Matteo Italia
    Nov 26 '18 at 7:28








  • 1





    In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).

    – Matteo Italia
    Nov 26 '18 at 7:35








  • 1





    As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit).

    – Matteo Italia
    Nov 26 '18 at 7:40








  • 1





    @MatteoItalia: Can I include your comments in the answer?

    – P.W
    Nov 26 '18 at 7:42



















  • thank you , I got your point about difference between 32bit and 64 bit system . Now can you please explain why the size is 148 for 32 bit os.

    – new_learner
    Nov 26 '18 at 7:26






  • 1





    @new_learner because there's 148 byte of data in there? It's really implementation-specific, depending from your system headers; dig them up from your compiler includes directory, sum up the fields sizes (taking padding in account) and you'll see that it'll add up to 148.

    – Matteo Italia
    Nov 26 '18 at 7:28








  • 1





    In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).

    – Matteo Italia
    Nov 26 '18 at 7:35








  • 1





    As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit).

    – Matteo Italia
    Nov 26 '18 at 7:40








  • 1





    @MatteoItalia: Can I include your comments in the answer?

    – P.W
    Nov 26 '18 at 7:42

















thank you , I got your point about difference between 32bit and 64 bit system . Now can you please explain why the size is 148 for 32 bit os.

– new_learner
Nov 26 '18 at 7:26





thank you , I got your point about difference between 32bit and 64 bit system . Now can you please explain why the size is 148 for 32 bit os.

– new_learner
Nov 26 '18 at 7:26




1




1





@new_learner because there's 148 byte of data in there? It's really implementation-specific, depending from your system headers; dig them up from your compiler includes directory, sum up the fields sizes (taking padding in account) and you'll see that it'll add up to 148.

– Matteo Italia
Nov 26 '18 at 7:28







@new_learner because there's 148 byte of data in there? It's really implementation-specific, depending from your system headers; dig them up from your compiler includes directory, sum up the fields sizes (taking padding in account) and you'll see that it'll add up to 148.

– Matteo Italia
Nov 26 '18 at 7:28






1




1





In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).

– Matteo Italia
Nov 26 '18 at 7:35







In particular, using this definition from glibc (and _IO_USE_OLD_IO_FILE undefined) I do get 148 bytes (with 4-byte int, pointers, size_t and __off_t).

– Matteo Italia
Nov 26 '18 at 7:35






1




1





As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit).

– Matteo Italia
Nov 26 '18 at 7:40







As for the difference, there are more than 17 pointers (plus padding!) which would account for the difference, but there's also some explicit padding at the end that complicates the calculation (it does become smaller on 64 bit, as 15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t) is 40 on 32 bit, but 24 on 64 bit).

– Matteo Italia
Nov 26 '18 at 7:40






1




1





@MatteoItalia: Can I include your comments in the answer?

– P.W
Nov 26 '18 at 7:42





@MatteoItalia: Can I include your comments in the answer?

– P.W
Nov 26 '18 at 7:42













0














The contents and size of the FILE structure is entirely specific to the implementation. it happens to be 216 bytes on your OS in 64-bit mode and 148 in 32-bit mode. The difference could come from the differing sizes of some of the members of this structure between 32-bit and 64-bit mode: pointers are larger, as well as size_t and file positions, it really depends on how the C library handles the standard streams.



Note that some extra information might be stored outside the structure... in any case you cannot save a FILE to a local variable and restore it safely.



Note that FILE could also be an incomplete type: a typedef for a pointer to a structure whose definition is absent, in which case sizeof(FILE) would cause a compilation error. The main reason to put the definition in the <stdio.h> header is to allow macros to access members directly. Take a look into your <stdio.h> file and look for other implementations in open source, such as the newlib or the glibc.



Furthermore, the C Standard does not mandate that FILE be a typedef for a structure: it might not be a structure, it could be defined as an int.






share|improve this answer


























  • @Note that FILE might not be a structure at all. It could be defined as an int . FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h)

    – new_learner
    Nov 26 '18 at 9:06








  • 2





    @new_learner FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h) That's true only for the one implementation you looked at. There are others - many others.

    – Andrew Henle
    Nov 26 '18 at 10:12


















0














The contents and size of the FILE structure is entirely specific to the implementation. it happens to be 216 bytes on your OS in 64-bit mode and 148 in 32-bit mode. The difference could come from the differing sizes of some of the members of this structure between 32-bit and 64-bit mode: pointers are larger, as well as size_t and file positions, it really depends on how the C library handles the standard streams.



Note that some extra information might be stored outside the structure... in any case you cannot save a FILE to a local variable and restore it safely.



Note that FILE could also be an incomplete type: a typedef for a pointer to a structure whose definition is absent, in which case sizeof(FILE) would cause a compilation error. The main reason to put the definition in the <stdio.h> header is to allow macros to access members directly. Take a look into your <stdio.h> file and look for other implementations in open source, such as the newlib or the glibc.



Furthermore, the C Standard does not mandate that FILE be a typedef for a structure: it might not be a structure, it could be defined as an int.






share|improve this answer


























  • @Note that FILE might not be a structure at all. It could be defined as an int . FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h)

    – new_learner
    Nov 26 '18 at 9:06








  • 2





    @new_learner FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h) That's true only for the one implementation you looked at. There are others - many others.

    – Andrew Henle
    Nov 26 '18 at 10:12
















0












0








0







The contents and size of the FILE structure is entirely specific to the implementation. it happens to be 216 bytes on your OS in 64-bit mode and 148 in 32-bit mode. The difference could come from the differing sizes of some of the members of this structure between 32-bit and 64-bit mode: pointers are larger, as well as size_t and file positions, it really depends on how the C library handles the standard streams.



Note that some extra information might be stored outside the structure... in any case you cannot save a FILE to a local variable and restore it safely.



Note that FILE could also be an incomplete type: a typedef for a pointer to a structure whose definition is absent, in which case sizeof(FILE) would cause a compilation error. The main reason to put the definition in the <stdio.h> header is to allow macros to access members directly. Take a look into your <stdio.h> file and look for other implementations in open source, such as the newlib or the glibc.



Furthermore, the C Standard does not mandate that FILE be a typedef for a structure: it might not be a structure, it could be defined as an int.






share|improve this answer















The contents and size of the FILE structure is entirely specific to the implementation. it happens to be 216 bytes on your OS in 64-bit mode and 148 in 32-bit mode. The difference could come from the differing sizes of some of the members of this structure between 32-bit and 64-bit mode: pointers are larger, as well as size_t and file positions, it really depends on how the C library handles the standard streams.



Note that some extra information might be stored outside the structure... in any case you cannot save a FILE to a local variable and restore it safely.



Note that FILE could also be an incomplete type: a typedef for a pointer to a structure whose definition is absent, in which case sizeof(FILE) would cause a compilation error. The main reason to put the definition in the <stdio.h> header is to allow macros to access members directly. Take a look into your <stdio.h> file and look for other implementations in open source, such as the newlib or the glibc.



Furthermore, the C Standard does not mandate that FILE be a typedef for a structure: it might not be a structure, it could be defined as an int.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 22:12

























answered Nov 26 '18 at 8:46









chqrliechqrlie

60k746102




60k746102













  • @Note that FILE might not be a structure at all. It could be defined as an int . FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h)

    – new_learner
    Nov 26 '18 at 9:06








  • 2





    @new_learner FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h) That's true only for the one implementation you looked at. There are others - many others.

    – Andrew Henle
    Nov 26 '18 at 10:12





















  • @Note that FILE might not be a structure at all. It could be defined as an int . FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h)

    – new_learner
    Nov 26 '18 at 9:06








  • 2





    @new_learner FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h) That's true only for the one implementation you looked at. There are others - many others.

    – Andrew Henle
    Nov 26 '18 at 10:12



















@Note that FILE might not be a structure at all. It could be defined as an int . FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h)

– new_learner
Nov 26 '18 at 9:06







@Note that FILE might not be a structure at all. It could be defined as an int . FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h)

– new_learner
Nov 26 '18 at 9:06






2




2





@new_learner FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h) That's true only for the one implementation you looked at. There are others - many others.

– Andrew Henle
Nov 26 '18 at 10:12







@new_learner FILE is a typedefed structure : typedef struct _IO_FILE FILE ; (typedefed in stdio.h and declared in libio.h) That's true only for the one implementation you looked at. There are others - many others.

– Andrew Henle
Nov 26 '18 at 10:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476148%2fwhy-the-size-of-file-structure-instance-is-216-bytes-in-64-bit-os-and-148-bytes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Unable to find Lightning Node

Futebolista