size in kbytes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Jørgensen

    #1

    size in kbytes?

    Hi,

    I allocate some memory of size_t but want to express:

    printf("Total amount of allocated memory is: %lu bytes.\n", (unsigned
    long) total_mem);

    in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
    explanations using google:

    1)
    printf("Total amount of allocated memory is: %lu kb.\n", (unsigned long)
    total_mem/1024);

    2)
    printf("Total amount of allocated memory is: %lu kb.\n", (unsigned long)
    total_mem/1000);



    Best regards / Med venlig hilsen
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
  • Nils O. Selåsdal

    #2
    Re: size in kbytes?

    Martin Jørgensen wrote:[color=blue]
    > Hi,
    >
    > I allocate some memory of size_t but want to express:
    >
    > printf("Total amount of allocated memory is: %lu bytes.\n", (unsigned
    > long) total_mem);
    >
    > in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
    > explanations using google:[/color]

    Depends on your definition of kilobyte

    Comment

    • stathis gotsis

      #3
      Re: size in kbytes?

      "Martin Jørgensen" <unoder.spam@sp am.jay.net> wrote in message
      news:ravsf3-mfa.ln1@news.td c.dk...[color=blue]
      > Hi,
      >
      > I allocate some memory of size_t but want to express:
      >
      > printf("Total amount of allocated memory is: %lu bytes.\n", (unsigned
      > long) total_mem);
      >
      > in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
      > explanations using google:
      >
      > 1)
      > printf("Total amount of allocated memory is: %lu kb.\n", (unsigned long)
      > total_mem/1024);
      >
      > 2)
      > printf("Total amount of allocated memory is: %lu kb.\n", (unsigned long)
      > total_mem/1000);[/color]

      In data storage context the prefix K usually equals 1024, while in network
      transfer context it may equal 1000. The prefix Ki is set to 1024, so 1 KiB =
      1024 bytes.


      Comment

      • Martin Jørgensen

        #4
        Re: size in kbytes?

        Nils O. Selåsdal wrote:[color=blue]
        > Martin Jørgensen wrote:
        >[color=green]
        >> Hi,
        >>
        >> I allocate some memory of size_t but want to express:
        >>
        >> printf("Total amount of allocated memory is: %lu bytes.\n", (unsigned
        >> long) total_mem);
        >>
        >> in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
        >> explanations using google:[/color]
        >
        >
        > Depends on your definition of kilobyte
        > http://en.wikipedia.org/wiki/Kilobyte[/color]

        What would you use and why? What's "normal" ?

        I think 1024 is more correct...?


        Best regards / Med venlig hilsen
        Martin Jørgensen

        --
        ---------------------------------------------------------------------------
        Home of Martin Jørgensen - http://www.martinjoergensen.dk

        Comment

        • Nils O. Selåsdal

          #5
          Re: size in kbytes?

          Martin Jørgensen wrote:[color=blue]
          > Nils O. Selåsdal wrote:[color=green]
          >> Martin Jørgensen wrote:
          >>[color=darkred]
          >>> Hi,
          >>>
          >>> I allocate some memory of size_t but want to express:
          >>>
          >>> printf("Total amount of allocated memory is: %lu bytes.\n", (unsigned
          >>> long) total_mem);
          >>>
          >>> in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
          >>> explanations using google:[/color]
          >>
          >>
          >> Depends on your definition of kilobyte
          >> http://en.wikipedia.org/wiki/Kilobyte[/color]
          >
          > What would you use and why? What's "normal" ?[/color]
          If you're producing harddrives or other storage it's "normal" to
          use 1000.[color=blue]
          > I think 1024 is more correct...?[/color]
          I think so too. I'd use 1024, and the suffix KiB instead
          of KB or kilobyte.

          Comment

          • Eric Sosman

            #6
            Re: size in kbytes?

            Martin Jørgensen wrote:[color=blue]
            > Hi,
            >
            > I allocate some memory of size_t but want to express:
            >
            > printf("Total amount of allocated memory is: %lu bytes.\n", (unsigned
            > long) total_mem);
            >
            > in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
            > explanations using google:
            >
            > 1)
            > printf("Total amount of allocated memory is: %lu kb.\n", (unsigned long)
            > total_mem/1024);
            >
            > 2)
            > printf("Total amount of allocated memory is: %lu kb.\n", (unsigned long)
            > total_mem/1000);[/color]

            Method #2 is correct[*]. The International System of Units (SI)
            defines the prefix "kilo" to mean multiplication by one thousand,
            hence, the conversion from bytes to kilobytes (or volts to kilovolts,
            or manjaros to kilomanjaros) requires dividing by one thousand.
            See <http://physics.nist.go v/cuu/Units/prefixes.html>.
            [*] Since your code performs an integer division, any fractional
            part will be truncated. For example, 1024 bytes will be reported as
            one kilobyte, rather than 1.024 kilobyte. Whether this truncation is
            "correct" or not depends on the use to which the result is put.

            Method #1 is traditional among computer geeks, who noticed long
            ago that 1024 is not terribly different from 1000. Since the number
            1024 cropped up often enough in computerdom to make a convenient
            abbreviation desirable, the geeks co-opted the prefix "kilo" to mean
            multiplication by 1024. This was the Original Sin, from which has
            flowed much grief.

            ... because computers grew larger, and pretty soon the geeks
            needed a convenient way to express multiplication by 1048576, two
            to the twentieth. With the evil precedent of "kilo" already in
            place, it was easy for them to sin again and abuse "mega" in a
            similar fashion. "Giga" followed, and then "tera," and "peta"
            has begun to pop up -- in the last year, I've even seen "zetta"
            abused, skipping right over "exa." SI defines "zetta" to mean
            multiplication by 1,000,000,000,0 00,000,000,000 (ten to the 21st),
            while in geekery it multiplies by 1,180,591,620,7 17,411,303,424
            (two to the seventieth).

            Notice how the Original Sin has become worse with the passage
            of time and the growth of the numbers. The misuse of "kilo" causes
            an error of 2.4%, about one part in forty-two (the significance of
            the denominator is left as an exercise for the hitchhiker). By
            co-opting "mega" the geeks magnified the error to 4.9%, one part
            in twenty-one. "Giga" is off by 7.4% (one part in fourteen), and
            by the time the geeks have gored "zetta" they're off by a whopping
            18% (one part in six). This proves that computer people have no
            interest in accuracy. (It has long been said that mathematicians
            are people who can't add, while computer programmers are people
            who can't even count.)

            Does the inaccuracy cause trouble? Yes! At a PPOE I was doing
            last-minute bug-fixing before product release, and one of the Q/A
            people reported a bug in the installation phase. The documentation
            said installation required X megabytes of available disk space, so
            the Q/A engineer decided to make sure it would actually work. He
            filled a disk with garbage files until only X megabytes of free
            space remained, then tried to install. The installation failed for
            lack of sufficient space, and the Q/A engineer filed a bug. I'm sure
            you can see where this is going: the documentation (and the install
            program) wanted X * 1048576 bytes, while the disk on the test machine
            had only X * 1000000 bytes available. Yes, Virginia, a 4.9% error
            can make a difference.

            The oddest and perhaps most bletcherous abuse in this sad history
            of wilful corruption is the "1.44 megabyte" floppy disk. Said disk
            does *not* hold 1.44 megabytes using either the true SI "mega" or
            the geekish two-to-the-twentieth "mega." What is actually holds is
            1.44 "kilo" (SI) "kilobytes" (geekish), a truly bastardized unit
            unloved by both parents.

            The International Electrotechnica l Commission has made a noble
            attempt to extend an olive branch to the geeks by standardizing
            a set of prefixes for binary multipliers: kibi, mebi, gibi, ...
            (See <http://physics.nist.go v/cuu/Units/binary.html>.) The geeks,
            however, are an ungracious and ungrateful lot and have spurned this
            peace offering; they continue to misuse "kilo" et al. for their own
            dastardly purposes. They are in agreement with Humpty Dumpty, who
            said "When I use a word it means just what I choose it to mean."
            Such an attitude evidences a certain independence, perhaps, but also
            a lack of interest in communicating.

            When all those geeks find themselves condemned to a few megayears
            in Purgatory for their prefix abuse, let's be sure to ask them how
            they'd like "mega" defined, shall we?

            --
            Eric Sosman
            esosman@acm-dot-org.invalid



            Comment

            • Richard G. Riley

              #7
              Re: size in kbytes?

              On 2006-03-30, Nils O. Selåsdal <NOS@Utel.no> wrote:[color=blue]
              > Martin Jørgensen wrote:[color=green]
              >> Hi,
              >>
              >> I allocate some memory of size_t but want to express:
              >>
              >> printf("Total amount of allocated memory is: %lu bytes.\n", (unsigned
              >> long) total_mem);
              >>
              >> in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
              >> explanations using google:[/color]
              >
              > Depends on your definition of kilobyte
              > http://en.wikipedia.org/wiki/Kilobyte[/color]

              And in memory terms, it is reasonably safe and proper to use 1024 as
              the equivalent amount.

              Comment

              • Skarmander

                #8
                Re: size in kbytes?

                Eric Sosman wrote:[color=blue]
                > Martin Jørgensen wrote:[color=green]
                >> Hi,
                >>
                >> I allocate some memory of size_t but want to express:
                >>
                >> printf("Total amount of allocated memory is: %lu bytes.\n", (unsigned
                >> long) total_mem);
                >>
                >> in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
                >> explanations using google:
                >>
                >> 1)
                >> printf("Total amount of allocated memory is: %lu kb.\n", (unsigned
                >> long) total_mem/1024);
                >>
                >> 2)
                >> printf("Total amount of allocated memory is: %lu kb.\n", (unsigned
                >> long) total_mem/1000);[/color]
                >
                > Method #2 is correct[*].[/color]
                <snip>

                Damn, and we were this close to skipping the off-topic flamewar material.
                People even gave a link for self-education.
                [color=blue]
                > When all those geeks find themselves condemned to a few megayears
                > in Purgatory for their prefix abuse, let's be sure to ask them how
                > they'd like "mega" defined, shall we?
                >[/color]

                May you be hurled into the sun at one attoparsec per microfortnight[*].

                S.[*]Caveat lector: not an SI unit.

                Comment

                • Eric Sosman

                  #9
                  [OT] Re: size in kbytes?



                  Skarmander wrote On 03/30/06 11:28,:[color=blue]
                  > Eric Sosman wrote:
                  >
                  >[color=green]
                  > > When all those geeks find themselves condemned to a few megayears
                  > > in Purgatory for their prefix abuse, let's be sure to ask them how
                  > > they'd like "mega" defined, shall we?
                  > >[/color]
                  >
                  > May you be hurled into the sun at one attoparsec per microfortnight[*].
                  >
                  > S.
                  >[*]Caveat lector: not an SI unit.[/color]

                  Geez, that's harsh! All I did was invent a new unit
                  called the "manjaro," a quantity equal to the carcase of
                  one leopard found above the snow line ...

                  --
                  Eric.Sosman@sun .com

                  Comment

                  • Jordan Abel

                    #10
                    Re: size in kbytes?

                    On 2006-03-30, Martin Jørgensen <unoder.spam@sp am.jay.net> wrote:[color=blue]
                    > Hi,
                    >
                    > I allocate some memory of size_t but want to express:
                    >
                    > printf("Total amount of allocated memory is: %lu bytes.\n", (unsigned
                    > long) total_mem);
                    >
                    > in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
                    > explanations using google:
                    >
                    > 1)
                    > printf("Total amount of allocated memory is: %lu kb.\n", (unsigned long)
                    > total_mem/1024);
                    >
                    > 2)
                    > printf("Total amount of allocated memory is: %lu kb.\n", (unsigned
                    > long) total_mem/1000);[/color]

                    Method 1 probably meets user expectations better, and will be more
                    consistent with the way other tools on most systems express amounts of
                    memory. And, actually, you should use "B", not "b" for bytes, "b" is
                    bits. Probably the best solution is to write "KiB" and use 1024.

                    Comment

                    • Jordan Abel

                      #11
                      Re: size in kbytes?

                      On 2006-03-30, Eric Sosman <esosman@acm-dot-org.invalid> wrote:[color=blue]
                      > Does the inaccuracy cause trouble? Yes! At a PPOE I was doing
                      > last-minute bug-fixing before product release, and one of the Q/A
                      > people reported a bug in the installation phase. The documentation
                      > said installation required X megabytes of available disk space, so
                      > the Q/A engineer decided to make sure it would actually work. He
                      > filled a disk with garbage files until only X megabytes of free
                      > space remained, then tried to install. The installation failed for
                      > lack of sufficient space, and the Q/A engineer filed a bug. I'm sure
                      > you can see where this is going: the documentation (and the install
                      > program) wanted X * 1048576 bytes, while the disk on the test machine
                      > had only X * 1000000 bytes available. Yes, Virginia, a 4.9% error
                      > can make a difference.[/color]

                      But when you hit "properties " on the disk drive, it will tell you the
                      amount of free space in units of 1048576 bytes. Your "trouble" is
                      imagined, and you're QA engineer is an idiot. Incidentally, it is
                      impossible to predict _exactly_ the amount of space that will be needed,
                      to the byte, because the size of each file is rounded up to an unknown
                      [well, known, but varies by system and even by drive] power of two, and
                      directories also take up space, so the whole attempt is flawed, and it
                      would probably have failed anyway.

                      Say what you will about the 1024-based units, but with the SOLE
                      exception of the number printed on the box that the drive comes in, it's
                      internally consistent.

                      Comment

                      • Keith Thompson

                        #12
                        Re: size in kbytes?

                        Martin Jørgensen <unoder.spam@sp am.jay.net> writes:[color=blue]
                        > Nils O. Selåsdal wrote:[color=green]
                        >> Martin Jørgensen wrote:[color=darkred]
                        >>> I allocate some memory of size_t but want to express:
                        >>>
                        >>> printf("Total amount of allocated memory is: %lu bytes.\n",
                        >>> (unsigned long) total_mem);
                        >>>
                        >>> in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
                        >>> explanations using google:[/color]
                        >> Depends on your definition of kilobyte
                        >> http://en.wikipedia.org/wiki/Kilobyte[/color]
                        >
                        > What would you use and why? What's "normal" ?[/color]

                        Did you have a question about the C programming language?

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        • Eric Sosman

                          #13
                          Re: size in kbytes?



                          Jordan Abel wrote On 03/30/06 12:39,:[color=blue]
                          > On 2006-03-30, Eric Sosman <esosman@acm-dot-org.invalid> wrote:
                          >[color=green]
                          >> Does the inaccuracy cause trouble? Yes! At a PPOE I was doing
                          >>last-minute bug-fixing before product release, and one of the Q/A
                          >>people reported a bug in the installation phase. The documentation
                          >>said installation required X megabytes of available disk space, so
                          >>the Q/A engineer decided to make sure it would actually work. He
                          >>filled a disk with garbage files until only X megabytes of free
                          >>space remained, then tried to install. The installation failed for
                          >>lack of sufficient space, and the Q/A engineer filed a bug. I'm sure
                          >>you can see where this is going: the documentation (and the install
                          >>program) wanted X * 1048576 bytes, while the disk on the test machine
                          >>had only X * 1000000 bytes available. Yes, Virginia, a 4.9% error
                          >>can make a difference.[/color]
                          >
                          >
                          > But when you hit "properties " on the disk drive, it will tell you the
                          > amount of free space in units of 1048576 bytes. Your "trouble" is
                          > imagined, and you're QA engineer is an idiot.[/color]

                          Mr. Abel, you may insult me all you like, because
                          I'm here to defend myself or ignore you or cave in to
                          your onslaught as I deem proper. Hurling names like
                          "idiot" at a conscientious Q/A engineer whom you've
                          never met or exchanged a word with is another matter.

                          For the record, the system in question reported
                          available disk space in units of bytes. I was there;
                          you were not.

                          Also for the record, there's a difference between
                          "you're" and "your." Learn the difference and apply
                          your knowledge, and your prose will gain an appearance
                          of authority. Perhaps.
                          [color=blue]
                          > Incidentally, it is
                          > impossible to predict _exactly_ the amount of space that will be needed,
                          > to the byte, because the size of each file is rounded up to an unknown
                          > [well, known, but varies by system and even by drive] power of two, and
                          > directories also take up space, so the whole attempt is flawed, and it
                          > would probably have failed anyway.[/color]

                          Mr. Abel, I was there; you were not. When I gave the
                          installation procedure the X * 1048576 bytes it wanted, lo!
                          the product installed just fine. So much for "probably
                          would have failed anyway."

                          For the record, the system in question did not round
                          file sizes up to powers of two.
                          [color=blue]
                          > Say what you will about the 1024-based units, but with the SOLE
                          > exception of the number printed on the box that the drive comes in, it's
                          > internally consistent.[/color]

                          Herein you are right: A mis-measurement is consistent
                          with itself. However, he who calibrates a meter stick by
                          measuring it against itself does surprisingly little to
                          advance the cause of knowledge.

                          --
                          Eric.Sosman@sun .com

                          Comment

                          • Randy Howard

                            #14
                            Re: size in kbytes?

                            Nils O. Selåsdal wrote
                            (in article <442bd948$1@new s.broadpark.no> ):
                            [color=blue]
                            > Martin Jørgensen wrote:[color=green]
                            >> Nils O. Selåsdal wrote:[color=darkred]
                            >>> Martin Jørgensen wrote:
                            >>>
                            >>>> Hi,
                            >>>>
                            >>>> I allocate some memory of size_t but want to express:
                            >>>>
                            >>>> printf("Total amount of allocated memory is: %lu bytes.\n", (unsigned
                            >>>> long) total_mem);
                            >>>>
                            >>>> in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
                            >>>> explanations using google:
                            >>>
                            >>>
                            >>> Depends on your definition of kilobyte
                            >>> http://en.wikipedia.org/wiki/Kilobyte[/color]
                            >>
                            >> What would you use and why? What's "normal" ?[/color]
                            > If you're producing harddrives or other storage it's "normal" to
                            > use 1000.[/color]

                            Solely as a result of lying marketing departments within those
                            companies. Besides, that has more to do with with Megabyte and
                            Gigabyte mangling than kilobytes. I don't recall even one hard
                            drive marketed with a capacity in kilobytes in the last 20
                            years.
                            [color=blue][color=green]
                            >> I think 1024 is more correct...?[/color]
                            > I think so too. I'd use 1024, and the suffix KiB instead
                            > of KB or kilobyte.[/color]

                            Why? KiB seems far less well-known than either of the other
                            two.


                            --
                            Randy Howard (2reply remove FOOBAR)
                            "The power of accurate observation is called cynicism by those
                            who have not got it." - George Bernard Shaw





                            Comment

                            • Martin Jørgensen

                              #15
                              Re: size in kbytes?

                              Jordan Abel wrote:[color=blue]
                              > On 2006-03-30, Martin Jørgensen <unoder.spam@sp am.jay.net> wrote:
                              >[color=green]
                              >>Hi,
                              >>
                              >>I allocate some memory of size_t but want to express:
                              >>
                              >>printf("Tot al amount of allocated memory is: %lu bytes.\n", (unsigned
                              >>long) total_mem);
                              >>
                              >>in kilo-bytes. So is method 1 or 2, the correct one? I've seen both
                              >>explanation s using google:
                              >>
                              >>1)
                              >>printf("Tot al amount of allocated memory is: %lu kb.\n", (unsigned long)
                              >>total_mem/1024);
                              >>
                              >>2)
                              >>printf("Tot al amount of allocated memory is: %lu kb.\n", (unsigned
                              >>long) total_mem/1000);[/color]
                              >
                              >
                              > Method 1 probably meets user expectations better, and will be more
                              > consistent with the way other tools on most systems express amounts of
                              > memory. And, actually, you should use "B", not "b" for bytes, "b" is
                              > bits. Probably the best solution is to write "KiB" and use 1024.[/color]

                              Strange, because you know: I believe that I tried many programs on
                              various systems over the years... And AFAIR I can't think of one single
                              program using "KiB" for output. Therefore I won't use "KiB" - I guess
                              there are people out there who don't even know what KiB is. I still
                              don't know whether to use 1024 or 1000, but I think I'll write KB after
                              the number...


                              Best regards / Med venlig hilsen
                              Martin Jørgensen

                              --
                              ---------------------------------------------------------------------------
                              Home of Martin Jørgensen - http://www.martinjoergensen.dk

                              Comment

                              Working...