Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6582987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:21:02+00:00 2026-05-25T16:21:02+00:00

While Alchemy supports compiling C++, it seems that using the using the STL is

  • 0

While Alchemy supports compiling C++, it seems that using the using the STL is trouble, mostly due to a problem with std::string. What’s strange is that Alchemy seems to be using GNU libstd++ v3.4.6. It’s hard to believe that std::string is broken in GNU’s STL.

Has anyone figured out any workarounds for this problem? C++ without the STL is like a fish without water.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:21:03+00:00Added an answer on May 25, 2026 at 4:21 pm

    The problem isn’t in the STL as such. The GNU implementation of std::string is reference-counted using the thread-safe functions __gnu_cxx::__exchange_and_add and __gnu_cxx::__atomic_add. The problem is that __exchange_and_add/__atomic_add are broken.

    The solution is to rebuild the STL with good implementations of these functions.

    Fortunately, the Alchemy distro leaves some breadcrumbs for us. See $ALCHEMY_HOME/avm2-libc/README, which tells us how to do it:

    The sources used to build avm2-libstdc++.l.bc can be downloaded here:
      http://download.macromedia.com/pub/labs/alchemy/alchemy_gnucpp3-4library_121008.zip
    
    To build avm2-libstdc++.l.bc:
     cd $ALCHEMY_HOME/avm2-libc
     unzip alchemy_gnucpp3-4library_121008.zip
     mv lib/avm2-libstdc++.l.bc lib/avm2-libstdc++.l.bc.OLD
     make 
    
    You should *not* run achacks prior to using these building this library with make.
    The Makefiles provided have been preconfigured to use LLVM directly where needed.
    

    Normally I’d expect to find the implementations of __exchange_and_add/__atomic_add in libstd++ ($ALCHEMY_HOME/avm2-libc/lib/avm2-libstdc++.l.bc), but for some reason they’re defined in libc ($ALCHEMY_HOME/avm2-libc/lib/avm2-libc.l.bc).

    I’m not sure why this is the case, but we can work around it by hacking atomicity.h, where the prototypes are kept. Note that after unpacking alchemy_gnucpp3-4library_121008.zip, you’ll need to edit two atomicity.h files:

    • $ALCHEMY_HOME/avm2-libc/include/c++/3.4/bits/atomicity.h
    • $ALCHEMY_HOME/avm2-libc/libstdc++/include/bits/atomicity.h

    Here is the code:

      /*
       * __exchange_and_add and __atomic_add are broken in Alchemy's libc.
       * Replace them with functioning implementations.  This isn't
       * cross-platform, but this codebase is only compiling for Alchemy anyway.
       */
      #define __exchange_and_add(x,y) __exchange_and_add_fix((x),(y))
      #define __atomic_add(x,y) __exchange_and_add_fix((x),(y))
    
      /*
       * Correctly implement __exchange_and_add.  It's not thread-safe,
       * but Alchemy isn't threaded, so we should be ok.
       */
      inline _Atomic_word __exchange_and_add_fix(volatile _Atomic_word* __mem, int __val) {
         int orig= *__mem;
         (*__mem)+= __val;
         return orig;
      }
    

    Here is some test code to run to make sure the rebuilt STL works:

    #include <cstdio>
    #include <string>
    #include <map>
    #include <fstream>
    using namespace std;
    
    void string_test() {
        string s1;
        string s2;
    
        s1 = "a";
        s2 = s1; // copy constructor
        s1 = "b";
    
        // use your favorite TRACE function here
        printf("s1= %s \n", s1.c_str()); // expected: "b", actual: "b"
        printf("s2= %s \n", s2.c_str()); // expected: "a", actual: "b", ERROR
    }
    
    void map_test() {
        map<string, int> test_map;
    
        test_map["test1"]= 1;    
        test_map["test2"]= 2;
        test_map["test3"]= 3;    
    
        string tmp= "test1";
        printf("%s : %d \n", tmp.c_str(), test_map[tmp]);
    }
    
    void ifstream_test()
    {
        std::ifstream in("test.txt");
    
        // ERROR 1:
        // Trying to seek file throws an error:
        // Error #1006: value is not a function.
        // at: basic_filebuf::char_traits::seekoff
        in.seekg(0, std::ios::end);
        int length = in.tellg();
        in.seekg(0, std::ios::beg);
        printf("File Length: %d \n", length);
    
        while(in.good()) {
            char buffer[512];
    
            // ERROR 2:
            // RangeError: Error #1125: The index 1092156 is out of range 721.
            // at basic_filebuf::char_traits::underflow::work()
            in.getline(buffer, 512, '\n');
    
            printf("buffer= %s \n", buffer);
        }
    }
    
    int main() {
        string_test();
        map_test();
        ifstream_test();
    
        return 0;
    }
    

    Note that the rebuilt STL appears to fix some related issues with map and ifstream.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying problem 41 from the Euler Project, I ran across what seems to
I'm using WP Alchemy to create a meta box in a post that allows
While using boost::threads I have come across this interruption problem. When I do a
While I grew up using MSWindows, I transitioned to my much-loved Mac years ago.
While reading from a site a read that you can not make a global
While RIA services seems very good for table operations & queries, I am stuck
While Trying to create a Menu to SubMenu using InsertMenuItem: MENUITEMINFO mii = {
While I'm using %g as format specifier in printf() , sometimes it rounds up
While reading answers to this question I noticed that answers ( this for example)
While reading a book called Let us C I read that a function showbit()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.