Why is the gap between an address of a file pointer, to the one after it, is only 32

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David727
    New Member
    • Jul 2010
    • 10

    Why is the gap between an address of a file pointer, to the one after it, is only 32

    #include<stdio. h>
    char garb;
    FILE *f ,*g;

    int main(){
    f=fopen("try1.t xt","wt");
    g=fopen("try2.t xt","wt");

    printf("f is equal to %ld\n",f);
    //the result is 2009464032

    printf("g is equal to %ld\n",g);
    //the result is 2009464064

    //I expected the gap will be 256 (one byte)...not 32

    scanf("%c", &garb);
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    A FILE* is a pointer. The 32 is the size of the address
    (4 bytes on a 32-bit OS).

    You have no idea of the size of the data that the FILE* points at.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      "I expected the gap will be 256 (one byte)..."
      A byte can take on any value between 0 and 256, but it accomplishes this in just one byte (that is, in 8 bits). The details of pointer encoding are processor dependent, but typically a pointer value is the address of the first byte of the possibly multi-byte thing being pointed at; and addresses typically refer to successive bytes. That is, the address of two successive bytes will typically differ by just one.

      What are you trying to accomplish by printing out the values of these pointers? Such information is more likely to bring confusion than enlightenment. If you insist on printing pointer values then you should use "%p" rather than "%ld".

      By they way, the difference between the two pointers doesn't tell you much about the size of the things being pointed at. You have no way of knowing if the two FILE structures created by the fopen calls are contiguous.

      By the way, a pointer difference of 32 typically refers to a difference of 32 bytes, not 32 bits.

      If you really want to know how large a FILE structure is (in char's, not necessarily in bytes) then do this:
      printf("%ld\n", sizeof(FILE));

      Comment

      • David727
        New Member
        • Jul 2010
        • 10

        #4
        Thank you.
        OK, I don't want to know (for now) the size of the data (the size of the file), but only the size of the memory unit of the pointer itself.

        I have now 2 different answers for that:

        The first answer is that 32 is a gap of 4 bytes (every byte is a 8 bit unit which means that 32 is 32 bits).
        (thanks Weaknessforcats ).

        The second answer is that the gap of 32 is 32 bytes (and not bits).
        (thanks Donbock)

        So actually the question is: which units do we see when we print a memory place like in the example above:
        bytes or bits?

        by the way,
        when I tried to see what is the result of f+1 (instead of creating another pointer g), I got the same result: the origin number + 32.
        the result of f+2 was the origin number + 64 and so on..the diference between one and the one after it is always 32.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          If you want to know the size of the pointer itself, then you can do either of the following:
          printf("%ld\n", sizeof(f));
          printf("%ld\n", sizeof(FILE*));

          Why do you want to know the size of the pointer itself? This size is implementation-dependent. You shouldn't write code that depends on any particular size of a data type.

          Comment

          • David727
            New Member
            • Jul 2010
            • 10

            #6
            Hi, I used your advice and it was very helpful.
            The results were very interesting (by the way, for your question why do I want to know that, the answer is only from a pure curiosity about where and how all the variables are saved).
            ok so that is the case: the size of all the addresses is 4. it means that our friend Weaknessforcats was right.
            32 represents bits and not bytes.
            NOW, there is another way to look at it and its by instead of printing the value of f (f is a pointer to a File which means it is an address (the long number we saw in the program)), We can actually call the addresses of the pointers themselves. then we see the same address (I'm not sure but I guess so) but in a bytes mode and then we get:
            printf(" the adress of f is %ld\n" ,&f);
            // the result here is 4210720
            printf(" the adress of g is %ld\n" ,&g);
            // the result here is 4210724

            We can see that the gap is 4
            when we asked the same questions about f and g (without the sign &)the gap was 32.

            I checked (by using the function sizeof- thanks Donbock ( that all the sizes are 4. It means that because some reason, the File pointers value talks in bits form, but when we use &, the address is in bytes form (and both of them are addresses of the same thing)

            I checked the value of a pointer to a char and the address of this pointer, and I got exactly the same number. The addresses were in bytes. The gap was 4 and not 32.
            The strange form (the big long number which represents bits) is only in File pointers.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              No. The size is bytes, not bits. You are looking at the size of the FILE structure (not the size of the pointers to FILE structures) when you compare the pointer values. You can check this assertion by printing sizeof(FILE), I predict it will be less than or equal to 32.

              Don't commit any of these sizes to memory. The sizes of pointers and the FILE structure size can vary from one platform to another (that's what implementation-dependent means). Compilers are even allowed to have different size pointers for pointing to different data types.

              Comment

              • David727
                New Member
                • Jul 2010
                • 10

                #8
                Hi,
                You were right about the 32 : these are bytes and not bits.

                Ok, so let’s look at the value of f.
                (f is a pointer to try1.txt file).
                The value is the memory address of try1.txt.

                Now let’s look at the value of g.
                (g is a pointer to try2.txt file).
                The value is the memory address of the try2.txt.

                The gap between the values of f and g is 32.
                When I checked the sizeof (*f) it was 32 also.
                So in my head I have the next picture:
                There is a file try1.txt it is in place A.
                There is another file try2.txt it is in place B.
                The distance between them is 32 bytes.
                (In a check with sizeof about the File itself with sizeof(*f) I've seen,
                that the size is also 32 (Everything is logical untill here)).
                The gap is 32 bytes and the size of the try1.txt is 32 bytes also. great.
                All the signs tell me that the size of the file itself
                is 32 bytes, and at the place it is ended, the second text file begins (try2.txt).
                BUT you still tell me that 32 bytes is not the size of the text file(or the text file data as you said (What’s the difference)).
                I created a situation when try1.txt is really a big file (10,000KB).
                I run the program and I was surprised to see that the addresses of the text files remained the same.
                It means you were right. There is no connection between the text file data size, to the gap between the first file text address to the address of the next file text.
                So the situation now is that we have a small gap between the first file address and the second file address (only 32 bytes), and still, the data size of the first file is huge. I understand you were right but how can you explain that? Wasn’t the data in the first text file supposed to be saved in the gap of 32 bytes?
                if not, so where is it saved, and if the answer is that it is being saved in another place, what does try1.txt need 32 bytes place for??
                And one more thing: How did I get the answer for the function sizeof(*f) : 32 ??( when the file is huge).
                Why didn’t this function return me the answer: 10000KB
                What is this: the returned value of 32 bytes??

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  32 is the size of a FILE structure, not the size of the text file itself -- note the difference in capitalization. The text file itself is on the hard drive, it does not reside in any of the program variables unless you fread the entire file into a malloc'ed buffer. The FILE structure contains enough information to allow fread, fwrite, and fclose to do their thing.

                  Let me repeat myself ... stop using the "gap" between pointer values to determine the size of data types. It is luck and coincidence that the gap happened to give the correct answer in this case. The only reliable way to determine the size of a data type is with the sizeof operator.

                  Comment

                  • David727
                    New Member
                    • Jul 2010
                    • 10

                    #10
                    "The only reliable way to determine the size of a data type is with the sizeof operator".
                    I thought that when I use sizeof(f), I get the size of the pointer cell, and when I use sizeof(*f) I get the size of the data inside the text file but now I understand that sizeof (*f) is only a size for the structure. ok..
                    so how can I know the size of the data inside the text file? I mean sizeof(what).

                    Is the only way is to get all the information by fread and only then should I use
                    sizeof on the malloced buffer??

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #11
                      Use fseek to position the file cursor to the end of the file (SEEK_END). Then use ftell to get the value of the file cursor. This value returned by ftell is effectively the size of the file.

                      Don't forget to fseek back to the beginning of the file before trying to fread it.

                      Comment

                      • David727
                        New Member
                        • Jul 2010
                        • 10

                        #12
                        Untill yesterday I was sure that
                        the file pointer points to the file, but it only points to the structure (it explains a lot).
                        The gap of 32 bytes is only the size of the structure (FILE)-
                        -I checked with sizeof and the result was 32- not the size of the file try1.txt (which its data is in another place - the hard drive).
                        Man you helped me a lot. Thank you.

                        Comment

                        Working...