atoi将字符数组有很多的整数上 [英] atoi on a character array with lots of integers

查看:193
本文介绍了atoi将字符数组有很多的整数上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code,其中的字符数组被填充的整数(转换为字符数组),并读取由它再转回到整数另一个函数。我用下面的函数来获取转换为字符数组:

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);

这工作在一般的罚款,但是当我尝试做一个项目的一部分,涉及QT(版本4.7),它编译罚款,给了我分割故障时尝试使用的memcpy阅读()。需要注意的是分段错误只发生在写入数据,而在读循环,而不是。我不知道为什么会这样,但我希望得到它通过任何方法来完成。

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.

那么,还有我可以使用可采取的字符阵列,所述第一比特和最后比特中,并将其转换成整数的任何其他其它功能。然后,我不会必须在所有使用的memcpy()。我所试图做的是这样的:

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

先谢谢了。

推荐答案

您只复制4个字符(取决于您的系统指针宽度)。这将使4+字符非空终止的数量,这导致失控的字符串输入到ATOI

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)

 str.length() + 1

还是字符不会nullterminated

Or the characters will not be nullterminated

make_testdata():看到一路下跌

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;
}

您可以做到这一点快了很多符合的atoi / itoa和一些调整纯C,但我想你应该使用二进制传输(见的提升精神噶并的 protobuf的好的库),如果你需要的速度。

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.

#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序列化时能得出:

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

共同上述所有版本的)

#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;
}

基准

我用


  • 使用的输入数据 2';&LT; 24 (33554432)随机整数

  • 不显示输出(我们不希望来衡量我们终端的滚动性能)

  • 粗略计时是

    • STL唯一版本是不是太糟糕实际上是在12.6s

    • 噶/齐文版跑<击>在18S 5.1s 感谢阿伦的提示在 generate_delimited :)

    • 在只有1.4S噶/齐二进制版本(big_dword)(大致<击> 12倍 3-4倍的速度

    • 升压序列化需要的蛋糕周围0.8S(或当subsituting文本存档,而不是二进制文件,13秒左右)

    • 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)

      这篇关于atoi将字符数组有很多的整数上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆