Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 13, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 13, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 13, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 13, 2025

      This $4 Steam Deck game includes the most-played classics from my childhood — and it will save you paper

      May 13, 2025

      Microsoft shares rare look at radical Windows 11 Start menu designs it explored before settling on the least interesting one of the bunch

      May 13, 2025

      NVIDIA’s new GPU driver adds DOOM: The Dark Ages support and improves DLSS in Microsoft Flight Simulator 2024

      May 13, 2025

      How to install and use Ollama to run AI LLMs on your Windows 11 PC

      May 13, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Community News: Latest PECL Releases (05.13.2025)

      May 13, 2025
      Recent

      Community News: Latest PECL Releases (05.13.2025)

      May 13, 2025

      How We Use Epic Branches. Without Breaking Our Flow.

      May 13, 2025

      I think the ergonomics of generators is growing on me.

      May 13, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      This $4 Steam Deck game includes the most-played classics from my childhood — and it will save you paper

      May 13, 2025
      Recent

      This $4 Steam Deck game includes the most-played classics from my childhood — and it will save you paper

      May 13, 2025

      Microsoft shares rare look at radical Windows 11 Start menu designs it explored before settling on the least interesting one of the bunch

      May 13, 2025

      NVIDIA’s new GPU driver adds DOOM: The Dark Ages support and improves DLSS in Microsoft Flight Simulator 2024

      May 13, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»CodeSOD: A Pair of Loops

    CodeSOD: A Pair of Loops

    December 2, 2024

    Alexandra inherited a codebase that, if we’re being kind, could be called “verbose”. Individual functions routinely cross into multiple thousands of lines, with the longest single function hitting 4,000 lines of code.

    Very little of this is because the problems being solved are complicated, and much more of it is because people don’t understand how anything works.

    For example, in this C++ code, they have a vector of strings. The goal is to create a map where the keys are the strings from the vector, and the values are more strings, derived from a function call.

    Essentially, what they wanted was:

    for (std::string val : invec)
    {
        umap[val] = lookupValue(val);
    }
    

    This would have been the sane, obvious way to do things. That’s not what they did.

    unordered_map<string, string> func(vector<string> invec)
    {
        unordered_map<string, string> umap;
        vector<pair<string, string*> idxvec;
        for(string name : invec)
        {
            umap[name] = "";
            idxvec.push_back(make_pair(name, &umap[name]));
        }   
    
        for(auto thingy : idxvec)
        {
            //actual work, including assigning the string
            thingy.get<1>() = lookupValue(thingy.get<0>()); 
        }
        return umap;
    }
    

    I won’t pick on names here, as they’re clearly anonymized. But let’s take a look at the approach they used.

    They create their map, and then create a new vector- a vector which is a pair<string, string*>– a string and a pointer to a string. Already, I’m confused by why any of this is happening, but let’s press on and hope it becomes clear.

    We iterate across our input vector, which this I get. Then we create a key in the map and give it an empty string as a value. Then we create a pair out of our key and our pointer to that empty string. That’s how we populate our idxvec vector.

    Once we’ve looped across all the values once, we do it again. This time, we pull out those pairs, and set the value at the pointer equal to the string returned by lookup value.

    Which leads us all to our favorite letter of the alphabet: WHY?

    I don’t know. I also am hesitant to comment to much on the memory management and ownership issues here, as with the anonymization, there may be some reference management that got lost. But the fact that we’re using bare pointers certainly makes this code more fraught than it needed to be. And, given how complex the STL data structures can be, I think we can also agree that passing around bare pointers to memory inside those structures is a recipe for disaster, even in simple cases like this.

    What I really enjoy is that they create a vector of pairs, without ever seeming to understand that a list of pairs is essentially what a map is.

    In conclusion: can we at least agree that, from now on, we won’t iterate across the same values twice? I think about 15% of WTFs would go away if we all followed that rule.

    Oh, wait, no. People who could understand rules like that aren’t the ones writing this kind of code. Forget I said anything.

    [Advertisement]
    Keep all your packages and Docker containers in one place, scan for vulnerabilities, and control who can access different feeds. ProGet installs in minutes and has a powerful free version with a lot of great features that you can upgrade when ready.Learn more.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleDistroWatch Weekly, Issue 1099
    Next Article osync – two way filesync script

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 14, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2024-52290 – LF Edge eKuiper Cross-Site Scripting (XSS)

    May 14, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    CVE-2025-46326 – Snowflake-Connector-Net TOCTOU Race Condition Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    20 Best Free and Open Source Linux Synthesizers

    Linux

    ChemCanvas – 2D chemical drawing tool

    Linux

    CVE-2025-3819 – PHPGurukul Men Salon Management System SQL Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)
    GetResponse

    Highlights

    Development

    Fota Wildlife Park Confirms Cyberattack, Investigates Data Exposure

    August 30, 2024

    Fota Wildlife Park, one of Ireland’s premier wildlife attractions, has fallen victim to a cyberattack…

    Using LM Studio to Run LLMs Easily, Locally and Privately

    June 24, 2024

    1Panel – modern web-based control panel for Linux server management

    January 7, 2025

    What happens when facial recognition gets it wrong – Week in security with Tony Anscombe

    June 1, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.