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. strlen innards

strlen innards

Scheduled Pinned Locked Moved C / C++ / MFC
htmlquestion
3 Posts 3 Posters 5 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.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    Can someone explain to me in layman's terms how this function works? https://codebrowser.dev/glibc/glibc/string/strlen.c.html Thank you.

    L J 2 Replies Last reply
    0
    • M mike7411

      Can someone explain to me in layman's terms how this function works? https://codebrowser.dev/glibc/glibc/string/strlen.c.html Thank you.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It counts the initial characters in the string until the pointer is aligned on a longword boundary. It then uses some clever bit manipulations to count the rest of the characters four at a time. If you run that code in the debugger you will be able to see exactly what happens.

      1 Reply Last reply
      0
      • M mike7411

        Can someone explain to me in layman's terms how this function works? https://codebrowser.dev/glibc/glibc/string/strlen.c.html Thank you.

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        Presumably already clear what the 4/8 block is used in the first place. Googling with following provides some answers.

        explain strlen himagic lomagic

        Following line is the key

        if (((longword - lomagic) & ~longword & himagic) != 0)

        I tried eye balling that and using examples in my head but still not clear. I believe it is relying on arithmetic overflow. If I wanted to understand that I would write up some test code with examples characters (4 blocks) with zero at the end (position 4, 3, 2, 1). Then have it print the results of each clause in the above if using binary representation (1 and 0) to see how the bits look for each different example and for each part of the clause. Perhaps as a learning experience also copy that code into your own space and then write a test jig to time results via that and using the more straightforward (just by char) code. You should start with a large number of runs like probably at least 100,000.

        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