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. Anything wrong

Anything wrong

Scheduled Pinned Locked Moved C / C++ / MFC
question
18 Posts 4 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.
  • W Waldermort

    Since you are dealing with strings there isn't a need to clear the whole buffer. char x[100] = { 0 }; internally calls memset. In your case it's a waste of (7 or so not including the loop inside memset) CPU cycles. char x[100]; x[0] = 0; initially creates an empty string. Just 1 CPU cycle. But then, calling functions like strcpy and sprintf don't require the buffer to be empty whereas strcat NEEDS a string (empty or otherwise) to append to.

    Waldermort

    S Offline
    S Offline
    Smith
    wrote on last edited by
    #7

    whereas strcat NEEDS a string (empty or otherwise) to append to. so I'll have to memset everything to 0 or initialize everything to 0 right?

    :beer:

    W 1 Reply Last reply
    0
    • S Smith

      whereas strcat NEEDS a string (empty or otherwise) to append to. so I'll have to memset everything to 0 or initialize everything to 0 right?

      :beer:

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #8

      No. When dealing with string buffers you only need to set the first char/wchar/tchar to 0. char myString[100]; // Is a buffer of 100 chars the a string may be copied into. strcat( myString, "Hello World!" ); // will fail because the buffer is full of junk. // but strcpy( myString, "World!" ); // will work because it doesn't care what is in the buffer myString[0] = 0; // copies an empty string to the buffer strcat( myString, "Hello World!" ); // appends the string to the empty string ( also sets the character after '!' to \0 or NULL. The new string is actually 13 chars because there is a 0 at the end. On another note. You shouldn't be using those string functions. There are safer versions available: strcpy_s( myString, 100, "Hello World!" ); These check to make sure the buffer is large enough for the string Also, you might want to read up on TCHAR and the unicode/ansi string functions TCHAR myString[100]; _tcscpy_s( myString, 100, _T("Hello World!");

      Waldermort

      1 Reply Last reply
      0
      • C CPallini

        The code (albeit not beautiful) is correct. With szLogBuf[512]={0}; you actually initialize the whole buffer with 0 (that is '\0'). :)

        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.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #9

        CPallini wrote:

        The code ... is correct.

        :thumbsdown:

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        C 1 Reply Last reply
        0
        • S Smith

          The first yes makes me not to click on the x of this window. Could you please tell me what is wrong with the code before I click on x?

          :beer:

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #10

          Smith# wrote:

          char chz[4]={0};

          this buffer is too small, your sprintf statement may generate a minus sign, three digits, a space, and a NULL, that is 6 characters. 60 of these could take up to 301 chars, so char szLogBuff [512]={0}; is sufficiently large, and as others already pointed out, your total initialization is a waste, as each strcat will move the initial terminating NULL backwards. BTW: you could get the correct result with less code, and save some cycles, and never have the bug you had, by having sprintf() fill the final buffer right away; all it takes is a variable pointer as the destination, initialized to szLogBuff, and incremented by the return value of sprintf! :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          C 1 Reply Last reply
          0
          • L Luc Pattyn

            CPallini wrote:

            The code ... is correct.

            :thumbsdown:

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

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

            It isn't wrong. It isn't elegant nor optimal, but not wrong. I think this is a great achievement, after all. (It's a subliminal suggestion for you: could you please hire the OP?)

            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.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            L 1 Reply Last reply
            0
            • L Luc Pattyn

              Smith# wrote:

              char chz[4]={0};

              this buffer is too small, your sprintf statement may generate a minus sign, three digits, a space, and a NULL, that is 6 characters. 60 of these could take up to 301 chars, so char szLogBuff [512]={0}; is sufficiently large, and as others already pointed out, your total initialization is a waste, as each strcat will move the initial terminating NULL backwards. BTW: you could get the correct result with less code, and save some cycles, and never have the bug you had, by having sprintf() fill the final buffer right away; all it takes is a variable pointer as the destination, initialized to szLogBuff, and incremented by the return value of sprintf! :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

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

              Luc Pattyn wrote:

              your sprintf statement may generate a minus sign

              No, it cannot. ;P

              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.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              L 1 Reply Last reply
              0
              • C CPallini

                It isn't wrong. It isn't elegant nor optimal, but not wrong. I think this is a great achievement, after all. (It's a subliminal suggestion for you: could you please hire the OP?)

                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.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #13

                CPallini wrote:

                It isn't elegant

                true.

                CPallini wrote:

                nor optimal

                true.

                CPallini wrote:

                not wrong

                false. see my reply to OP. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                C 1 Reply Last reply
                0
                • C CPallini

                  Luc Pattyn wrote:

                  your sprintf statement may generate a minus sign

                  No, it cannot. ;P

                  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.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #14

                  from the code snippet I can't tell what the definition of BYTE is, could be signed, could be unsigned; I prepare for the worst. Anyway, it isn't really relevant, the buffer isn't sufficiently large. :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  C 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    CPallini wrote:

                    It isn't elegant

                    true.

                    CPallini wrote:

                    nor optimal

                    true.

                    CPallini wrote:

                    not wrong

                    false. see my reply to OP. :)

                    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

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

                    See my reply[^]. :)

                    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.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      from the code snippet I can't tell what the definition of BYTE is, could be signed, could be unsigned; I prepare for the worst. Anyway, it isn't really relevant, the buffer isn't sufficiently large. :)

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

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

                      You didn't read the snippet, did you? (hint: it _s_prints the index, the BYTE part is completely insignificant). :)

                      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.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      L 1 Reply Last reply
                      0
                      • C CPallini

                        You didn't read the snippet, did you? (hint: it _s_prints the index, the BYTE part is completely insignificant). :)

                        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.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #17

                        How silly. So that must be wrong too. :laugh:

                        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                        C 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          How silly. So that must be wrong too. :laugh:

                          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

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

                          :laugh:

                          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.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          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