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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. behavior of strlen() [modified]

behavior of strlen() [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
11 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.
  • K koumodaki

    I would like to know the behavior of strlen() when an uninitilized char array is passed as a parameter char my_str[6]; int len = strlen(my_str); HKEY hKey; RegOpenKey(HKEY_LOCAL_MACHINE,"HKEY_LOCAL_MACHINE\\SOFTWARE\\Analog Devices\\SMWDMIF\\Settings",&hKey); RegQueryValueEx(hKey, "Mode", 0, NULL, (LPBYTE)my_str, &len); what value would len have when the above code gets executed? Will it cause any compiler/runtime errors?

    modified on Wednesday, March 12, 2008 11:33 AM

    R Offline
    R Offline
    Rajkumar R
    wrote on last edited by
    #2

    still, it searches for the '\0' character.

    koumodaki wrote:

    what value would len have when the above code gets executed?

    len = number of character preceeding a '\0' character.

    koumodaki wrote:

    RegQueryValueEx(hKey, "Mode", 0, NULL, (LPBYTE)my_str, &len);

    here it expects the buffer size not the string len.

    K 2 Replies Last reply
    0
    • R Rajkumar R

      still, it searches for the '\0' character.

      koumodaki wrote:

      what value would len have when the above code gets executed?

      len = number of character preceeding a '\0' character.

      koumodaki wrote:

      RegQueryValueEx(hKey, "Mode", 0, NULL, (LPBYTE)my_str, &len);

      here it expects the buffer size not the string len.

      K Offline
      K Offline
      koumodaki
      wrote on last edited by
      #3

      but '\0' is not present. So how does it know the end of the char array? what value gets stored in len?

      R D 2 Replies Last reply
      0
      • K koumodaki

        but '\0' is not present. So how does it know the end of the char array? what value gets stored in len?

        R Offline
        R Offline
        Rajkumar R
        wrote on last edited by
        #4

        i modified my reply please look at it.

        1 Reply Last reply
        0
        • K koumodaki

          but '\0' is not present. So how does it know the end of the char array? what value gets stored in len?

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

          koumodaki wrote:

          but '\0' is not present.

          It is, just not at the location you desire. For example, if my_str resides at memory address 0x1234, then strlen() will start counting from that locaton until it finds a '\0' character. Use your debugger to see this in action.

          "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

          1 Reply Last reply
          0
          • R Rajkumar R

            still, it searches for the '\0' character.

            koumodaki wrote:

            what value would len have when the above code gets executed?

            len = number of character preceeding a '\0' character.

            koumodaki wrote:

            RegQueryValueEx(hKey, "Mode", 0, NULL, (LPBYTE)my_str, &len);

            here it expects the buffer size not the string len.

            K Offline
            K Offline
            koumodaki
            wrote on last edited by
            #6

            my_str is actualy buffer to store the registry key data. Since I am using strlen() to ge the buffer size, I wanted to know the side-effects of using the function this way. Also I have not initilized my_str to any value. As far as I know only memory is alocatd by the compiler when I define the array. So can I safetly assume that the variable len = 6, the size of decalred array?

            D R C 3 Replies Last reply
            0
            • K koumodaki

              my_str is actualy buffer to store the registry key data. Since I am using strlen() to ge the buffer size, I wanted to know the side-effects of using the function this way. Also I have not initilized my_str to any value. As far as I know only memory is alocatd by the compiler when I define the array. So can I safetly assume that the variable len = 6, the size of decalred array?

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

              koumodaki wrote:

              Since I am using strlen() to ge the buffer size...

              strlen() dos not get the size of anything. Use sizeof instead.

              koumodaki wrote:

              ...I wanted to know the side-effects of using the function this way.

              At a minimum, the program won't work.

              koumodaki wrote:

              Also I have not initilized my_str to any value.

              But it does get initialized, whether you did it or not.

              koumodaki wrote:

              So can I safetly assume that the variable len = 6, the size of decalred array?

              You can, but you'd be wrong.

              "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

              K 1 Reply Last reply
              0
              • K koumodaki

                my_str is actualy buffer to store the registry key data. Since I am using strlen() to ge the buffer size, I wanted to know the side-effects of using the function this way. Also I have not initilized my_str to any value. As far as I know only memory is alocatd by the compiler when I define the array. So can I safetly assume that the variable len = 6, the size of decalred array?

                R Offline
                R Offline
                Rajkumar R
                wrote on last edited by
                #8

                koumodaki wrote:

                Since I am using strlen() to ge the buffer size

                buffer size and string len cannot interpretted same.

                koumodaki wrote:

                As far as I know only memory is alocatd by the compiler when I define the array

                size of array is the buffer size here.

                koumodaki wrote:

                So can I safetly assume that the variable len = 6, the size of decalred array?

                yes, but if your regvalue string length is greater than 5 you won't get the complete string.

                K 1 Reply Last reply
                0
                • R Rajkumar R

                  koumodaki wrote:

                  Since I am using strlen() to ge the buffer size

                  buffer size and string len cannot interpretted same.

                  koumodaki wrote:

                  As far as I know only memory is alocatd by the compiler when I define the array

                  size of array is the buffer size here.

                  koumodaki wrote:

                  So can I safetly assume that the variable len = 6, the size of decalred array?

                  yes, but if your regvalue string length is greater than 5 you won't get the complete string.

                  K Offline
                  K Offline
                  koumodaki
                  wrote on last edited by
                  #9

                  Thanks

                  1 Reply Last reply
                  0
                  • D David Crow

                    koumodaki wrote:

                    Since I am using strlen() to ge the buffer size...

                    strlen() dos not get the size of anything. Use sizeof instead.

                    koumodaki wrote:

                    ...I wanted to know the side-effects of using the function this way.

                    At a minimum, the program won't work.

                    koumodaki wrote:

                    Also I have not initilized my_str to any value.

                    But it does get initialized, whether you did it or not.

                    koumodaki wrote:

                    So can I safetly assume that the variable len = 6, the size of decalred array?

                    You can, but you'd be wrong.

                    "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

                    K Offline
                    K Offline
                    koumodaki
                    wrote on last edited by
                    #10

                    Thanks for explaining

                    1 Reply Last reply
                    0
                    • K koumodaki

                      my_str is actualy buffer to store the registry key data. Since I am using strlen() to ge the buffer size, I wanted to know the side-effects of using the function this way. Also I have not initilized my_str to any value. As far as I know only memory is alocatd by the compiler when I define the array. So can I safetly assume that the variable len = 6, the size of decalred array?

                      C Offline
                      C Offline
                      Chris Losinger
                      wrote on last edited by
                      #11

                      koumodaki wrote:

                      I wanted to know the side-effects of using the function this way.

                      high probability of an Access Violation error, followed by a nasty dialog box and the termination of your application.

                      image processing toolkits | batch image processing

                      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