Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Retrieve the length of an int

Retrieve the length of an int

Scheduled Pinned Locked Moved C / C++ / MFC
questionsecurityhelptutorial
22 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Perspx

    I have a string of text displaying the size of a file (in bytes) in the form "File Size: %d bytes" (where %d is the file size) and I would like to allocate enough memory whatever the file size. --PerspX

    "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #7

    Perspx wrote:

    I have a string of text displaying the size of a file (in bytes) in the form "File Size: %d bytes" (where %d is the file size) and I would like to allocate enough memory whatever the file size.

    DWORD dwFileSize = 123456;
    char s[28];
    sprintf(s, "File Size: %lu bytes", dwFileSize);


    "A good athlete is the result of a good and worthy opponent." - David Crow

    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

    L 1 Reply Last reply
    0
    • P Perspx

      That's bad programming and a waste of resources. --PerspX

      "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #8

      Perspx wrote:

      That's bad programming and a waste of resources.

      :laugh:


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      P 1 Reply Last reply
      0
      • P Perspx

        If I have an integer value, and I want to write its value into a string, how do I work out how many characters the integer will take up? For example, if I have the number 100, and I want to put it into a string using sprintf, how do I dynamically work out how many characters the number will take up (in this case 3)? Thanks for your help! --PerspX

        "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #9

        . I leave you as an exercise the extension to n<=0.

        //prerequisite: n>0
        int howmanychars(int n)
        {
        return (int)log10(n) + 1;
        }

        BTW of course you don't need it :-D

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        P 1 Reply Last reply
        0
        • D David Crow

          Perspx wrote:

          That's bad programming and a waste of resources.

          :laugh:


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          P Offline
          P Offline
          Perspx
          wrote on last edited by
          #10

          You can laugh but I bet that the reason that most Microsoft programs are slow and clunky is because the programmers who write them do bad programming techniques such as that.. and it doesn't matter for that particular example, as that only wastes 19 bytes at the maximum.. but if you apply that technique for other data allocation, then you could waste a lot more which makes your application unreliable and not perform so well.. --PerspX

          "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

          C D 2 Replies Last reply
          0
          • P Perspx

            That's bad programming and a waste of resources. --PerspX

            "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #11

            Perspx wrote:

            That's bad programming and a waste of resources.

            :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            P 1 Reply Last reply
            0
            • P Perspx

              You can laugh but I bet that the reason that most Microsoft programs are slow and clunky is because the programmers who write them do bad programming techniques such as that.. and it doesn't matter for that particular example, as that only wastes 19 bytes at the maximum.. but if you apply that technique for other data allocation, then you could waste a lot more which makes your application unreliable and not perform so well.. --PerspX

              "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #12

              Please continue! It's amazing! :-D

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              1 Reply Last reply
              0
              • C CPallini

                . I leave you as an exercise the extension to n<=0.

                //prerequisite: n>0
                int howmanychars(int n)
                {
                return (int)log10(n) + 1;
                }

                BTW of course you don't need it :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                P Offline
                P Offline
                Perspx
                wrote on last edited by
                #13

                Thank you --PerspX

                "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

                C 1 Reply Last reply
                0
                • C CPallini

                  Perspx wrote:

                  That's bad programming and a waste of resources.

                  :-D

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                  P Offline
                  P Offline
                  Perspx
                  wrote on last edited by
                  #14

                  Is that a laugh at me or in agreement? --PerspX

                  "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

                  1 Reply Last reply
                  0
                  • D David Crow

                    Perspx wrote:

                    I have a string of text displaying the size of a file (in bytes) in the form "File Size: %d bytes" (where %d is the file size) and I would like to allocate enough memory whatever the file size.

                    DWORD dwFileSize = 123456;
                    char s[28];
                    sprintf(s, "File Size: %lu bytes", dwFileSize);


                    "A good athlete is the result of a good and worthy opponent." - David Crow

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    L Offline
                    L Offline
                    LordMarv
                    wrote on last edited by
                    #15

                    To know how many characters in the formatted string are needed to allocate, use _vscprintf an then use new or malloc with the number of characters returned.

                    1 Reply Last reply
                    0
                    • P Perspx

                      Thank you --PerspX

                      "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #16

                      Please note: I gave you the function only for informative pourposes (an academic exercise): you really don't need it. Moreover it will slow down your code. BTW: Think a bit before giving judge on programmers slowness, with David Crow you surely miss the target. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                      P R 2 Replies Last reply
                      0
                      • C CPallini

                        Please note: I gave you the function only for informative pourposes (an academic exercise): you really don't need it. Moreover it will slow down your code. BTW: Think a bit before giving judge on programmers slowness, with David Crow you surely miss the target. :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                        P Offline
                        P Offline
                        Perspx
                        wrote on last edited by
                        #17

                        Yeah I know.. I'm not even a professional programmer :p but I really hate Windows programs which are really slow and clunky and don't manage resources well.. I'm not having a go at him, I'm grateful for the advice from him and everyone.. I'm just sure that there must be an efficient way of doing this and throwing memory away unnecessarily is a bit of a waste.. Don't you agree? --PerspX

                        "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

                        D 1 Reply Last reply
                        0
                        • C CPallini

                          Please note: I gave you the function only for informative pourposes (an academic exercise): you really don't need it. Moreover it will slow down your code. BTW: Think a bit before giving judge on programmers slowness, with David Crow you surely miss the target. :)

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                          R Offline
                          R Offline
                          Rage
                          wrote on last edited by
                          #18

                          CPallini wrote:

                          hink a bit before giving judge on programmers slowness, with David Crow you surely miss the target.

                          Yep, that's the least we can say. :cool:

                          Don't follow any man spiritually, don't do anything that will get you in sh*t if god is real - Bradml[^]

                          1 Reply Last reply
                          0
                          • M m dietz

                            To solve that problem mathematically: take the logarithm to base 10 (double log10(double))and add 1. Else you can format it into a CString and check that length. Greetings, Martin edit: sorry, used the wrong log function (base e instead of base 10) -- modified at 10:22 Friday 3rd August, 2007

                            C Offline
                            C Offline
                            CPallini
                            wrote on last edited by
                            #19

                            m.dietz wrote:

                            To solve that problem mathematically: take the logarithm to base 10 (double log10(double))and add 1.

                            First make sure the value is greater than zero. :)

                            m.dietz wrote:

                            Else you can format it into a CString and check that length.

                            the above works anyway. :-D

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                            1 Reply Last reply
                            0
                            • P Perspx

                              Yeah I know.. I'm not even a professional programmer :p but I really hate Windows programs which are really slow and clunky and don't manage resources well.. I'm not having a go at him, I'm grateful for the advice from him and everyone.. I'm just sure that there must be an efficient way of doing this and throwing memory away unnecessarily is a bit of a waste.. Don't you agree? --PerspX

                              "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

                              D Offline
                              D Offline
                              dburns
                              wrote on last edited by
                              #20

                              Perspx wrote:

                              ...don't manage resources well

                              Don't forget, memory is not the only resource a computer has. Sounds to me like you're wasting the CPU time (another resource) in order to save a few bytes of memory. It's hard to imagine a case where those few bytes would ever matter. There's also the factor of code complexity -- sounds like you're making your code more complex than it needs to be. Since you're calculating size, I'd guess you're going to be allocating memory dynamically through malloc or new[] etc. Those schemes will add their own overhead which will swamp the few bytes you're aiming to save.

                              Perspx wrote:

                              Don't you agree?

                              In principal, yes, don't waste resources. However, I think you're going overboard in this case (the other posts seem to agree).

                              P 1 Reply Last reply
                              0
                              • P Perspx

                                You can laugh but I bet that the reason that most Microsoft programs are slow and clunky is because the programmers who write them do bad programming techniques such as that.. and it doesn't matter for that particular example, as that only wastes 19 bytes at the maximum.. but if you apply that technique for other data allocation, then you could waste a lot more which makes your application unreliable and not perform so well.. --PerspX

                                "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

                                D Offline
                                D Offline
                                David Crow
                                wrote on last edited by
                                #21

                                Perspx wrote:

                                I bet that the reason that most Microsoft programs are slow and clunky...

                                I highly doubt that. Reserving 1 byte or 20 bytes from the stack will make no noticeable difference in your code's execution time, but using the log10() function and asking the memory manager for countless small amounts of memory will. Consider the following:

                                for (int x = 0; x < nFileCount; x++)
                                {
                                int nLength = (int) (log10(dwFileSize) + 1.0);
                                char *s = new char[18 + nLength];
                                sprintf(s, "File Size: %lu bytes", dwFileSize);
                                delete [] s;
                                }

                                compared to:

                                char s[28];
                                for (int x = 0; x < nFileCount; x++)
                                {
                                sprintf(s, "File Size: %lu bytes", dwFileSize);
                                }


                                "A good athlete is the result of a good and worthy opponent." - David Crow

                                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                1 Reply Last reply
                                0
                                • D dburns

                                  Perspx wrote:

                                  ...don't manage resources well

                                  Don't forget, memory is not the only resource a computer has. Sounds to me like you're wasting the CPU time (another resource) in order to save a few bytes of memory. It's hard to imagine a case where those few bytes would ever matter. There's also the factor of code complexity -- sounds like you're making your code more complex than it needs to be. Since you're calculating size, I'd guess you're going to be allocating memory dynamically through malloc or new[] etc. Those schemes will add their own overhead which will swamp the few bytes you're aiming to save.

                                  Perspx wrote:

                                  Don't you agree?

                                  In principal, yes, don't waste resources. However, I think you're going overboard in this case (the other posts seem to agree).

                                  P Offline
                                  P Offline
                                  Perspx
                                  wrote on last edited by
                                  #22

                                  Ok yes I see what you are all saying.. Thanks everyone --PerspX

                                  "Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups