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 6022119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:48:46+00:00 2026-05-23T03:48:46+00:00

I have a code in which the character array is populated by integers (converted

  • 0

I have a code in which the character array is populated by integers (converted to char arrays), and read by another function which reconverts it back to integers. I have used the following function to get the conversion to char array:

char data[64];
int a = 10;
std::string str = boost::lexical_cast<std::string>(a);
memcpy(data + 8*k,str.c_str(),sizeof(str.c_str()));   //k varies from 0 to 7

and the reconversion back to characters is done using:

char temp[8];
memcpy(temp,data+8*k,8);
int a = atoi(temp);

This works fine in general, but when I try to do it as part of a project involving qt (ver 4.7), it compiles fine and gives me segmentation faults when it tries to read using memcpy(). Note that the segmentation fault happens only while in the reading loop and not while writing data. I dont know why this happens, but I want to get it done by any method.

So, are there any other other functions which I can use which can take in the character array, the first bit and the last bit and convert it into the integer. Then I wouldnt have to use memcpy() at all. What I am trying to do is something like this:

new_atoi(data,8*k,8*(k+1)); // k varies from 0 to 7

Thanks in advance.

  • 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-23T03:48:47+00:00Added an answer on May 23, 2026 at 3:48 am

    You are copying only a 4 characters (dependent on your system’s pointer width). This will leave numbers of 4+ characters non-null terminated, leading to runaway strings in the input to atoi

     sizeof(str.c_str()) //i.e. sizeof(char*) = 4 (32 bit systems)
    

    should be

     str.length() + 1
    

    Or the characters will not be nullterminated

    STL Only:

    make_testdata(): see all the way down

    Why don’t you use streams…?

    #include <sstream>
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <string>
    #include <vector>
    
    int main()
    {
        std::vector<int> data = make_testdata();
    
        std::ostringstream oss;
        std::copy(data.begin(), data.end(), std::ostream_iterator<int>(oss, "\t"));
    
        std::stringstream iss(oss.str());
    
        std::vector<int> clone;
        std::copy(std::istream_iterator<int>(iss), std::istream_iterator<int>(),
                  std::back_inserter(clone));
    
        //verify that clone now contains the original random data:
        //bool ok = std::equal(data.begin(), data.end(), clone.begin());
    
        return 0;
    }
    

    You could do it a lot faster in plain C with atoi/itoa and some tweaks, but I reckon you should be using binary transmission (see Boost Spirit Karma and protobuf for good libraries) if you need the speed.

    Boost Karma/Qi:

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/karma.hpp>
    
    namespace qi=::boost::spirit::qi;
    namespace karma=::boost::spirit::karma;
    
    static const char delimiter = '\0';
    
    int main()
    {
        std::vector<int> data = make_testdata();
    
        std::string astext;
    //  astext.reserve(3 * sizeof(data[0]) * data.size()); // heuristic pre-alloc
        std::back_insert_iterator<std::string> out(astext);
    
        {
            using namespace karma;
            generate(out, delimit(delimiter) [ *int_ ], data);
        //  generate_delimited(out, *int_, delimiter, data); // equivalent
        //  generate(out, int_ % delimiter, data); // somehow much slower!
        }
    
        std::string::const_iterator begin(astext.begin()), end(astext.end());
        std::vector<int> clone;
        qi::parse(begin, end, qi::int_ % delimiter, clone);
    
        //verify that clone now contains the original random data:
        //bool ok = std::equal(data.begin(), data.end(), clone.begin());
    
        return 0;
    }
    

    If you wanted to do architecture independent binary serialization instead, you’d use this tiny adaptation making things a zillion times faster (see benchmark below…):

    karma::generate(out, *karma::big_dword, data);
    // ...
    qi::parse(begin, end, *qi::big_dword, clone);
    

    Boost Serialization

    The best performance can be reached when using Boost Serialization in binary mode:

    #include <sstream>
    #include <boost/archive/binary_oarchive.hpp>
    #include <boost/archive/binary_iarchive.hpp>
    #include <boost/serialization/vector.hpp>
    
    int main()
    {
        std::vector<int> data = make_testdata();
    
        std::stringstream ss;
        {
            boost::archive::binary_oarchive oa(ss);
            oa << data;
        }
    
        std::vector<int> clone;
        {
            boost::archive::binary_iarchive ia(ss);
            ia >> clone;
        }
    
        //verify that clone now contains the original random data:
        //bool ok = std::equal(data.begin(), data.end(), clone.begin());
    
        return 0;
    }
    

    Testdata

    (common to all versions above)

    #include <boost/random.hpp>
    
    // generates a deterministic pseudo-random vector of 32Mio ints
    std::vector<int> make_testdata()
    {
        std::vector<int> testdata;
    
        testdata.resize(2 << 24);
        std::generate(testdata.begin(), testdata.end(), boost::mt19937(0));
    
        return testdata;
    }
    

    Benchmarks

    I benchmarked it by

    • using input data of 2<<24 (33554432) random integers
    • not displaying output (we don’t want to measure the scrolling performance of our terminal)
    • the rough timings were
      • STL only version isn’t too bad actually at 12.6s
      • Karma/Qi text version ran in 18s 5.1s, thanks to Arlen’s hint at generate_delimited 🙂
      • Karma/Qi binary version (big_dword) in only 1.4s (roughly 12x 3-4x as fast)
      • Boost Serialization takes the cake with around 0.8s (or when subsituting text archives instead of binaries, around 13s)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code which collects points (consed integers) from a loop which looks
I have have some code which adds new cells to a table and fills
I have some code which utilizes parameterized queries to prevent against injection, but I
I have some code which I am making available via RMI. If my program
I have some code which needs to ensure some data is in a mysql
I have some code which returns InnerXML for a XMLNode. The node can contain
I have Perl code which relies on Term::ReadKey to get the terminal width. My
I have some code which ignores a specific exception. try { foreach (FileInfo fi
I have some code which I did not originally create that uses _beginthreadex and
On a small embedded system project we have some code which we would like

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.