简体   繁体   English

如何将char *转换为字符串?

[英]How to convert a char* to a string?

When I convert char* to an string it gives an bad memory allocation error in 'new.cpp' . 当我将char *转换为字符串时,它在'new.cpp'中给出了错误的内存分配错误。 I used following method to convert char* called 'strData' and 'strOrg' to string. 我使用以下方法将名为“ strData”和“ strOrg”的char *转换为字符串。

   const char* strData = dt.data();
   int length2 = dt.length();
   string s1(strData);

First time it work without any problem. 第一次可以正常工作。 But in the second convertion it gives above error. 但是在第二次转换中,它给出了以上错误。 When I swap the two conversion in the order, it give the error always in the second conversion regardless of the char* I am converting. 当我按顺序交换两个转换时,无论我要转换的char *是什么,它总是在第二个转换中给出错误。 Whole code is shown in the following. 整个代码如下所示。

    mysqlpp::Query query = conn.query("SELECT data,origin from image where id =2");
    mysqlpp::UseQueryResult res = query.use();
    mysqlpp::Row eee= res.fetch_row();
    mysqlpp::Row::reference dt = eee.at(0);
    mysqlpp::Row::reference org = eee.at(1);

    const char* strData = dt.data();
    int length2 = dt.length();
    string s1(strData);
    istringstream is1(s1);  
    char * imgData =  new char;
    is1.read(reinterpret_cast<char *> (imgData), length2);
    delete [] strData;

    const char* strOrg = org.data();
    int length3 = org.length();
    string s2(strOrg);
    istringstream is2(s2);  
    char * imgOrg =  new char;
    is2.read(reinterpret_cast<char *> (imgOrg), length3);
    delete [] strOrg;

This where the error comes from 这是错误的来源

    void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
    {       
    void *p;
    while ((p = malloc(size)) == 0)
            if (_callnewh(size) == 0)
            {       // report no memory
            static const std::bad_alloc nomem;
            _RAISE(nomem);
            }

    return (p);
    }

How can I solve this? 我该如何解决?

Instead of 代替

char * imgData = new char;
is1.read(reinterpret_cast<char *> (imgData), length2);

try 尝试

char * imgData = new char[length2];
is1.read(reinterpret_cast<char *> (imgData), length2);

When you read data from an istringstream using read , the buffer you provide must have enough space to hold the results! 使用readistringstream读取数据时,提供的缓冲区必须有足够的空间来保存结果!

If you call new char; 如果您调用new char; you get space for one char . 你有一个char空间。 Use new char[n]; 使用new char[n]; to get space for n. 获得n的空间




    delete [] strData;

This is bad. 这不好。 The line above it probably is also but I know this one is. 它上面的线可能也是,但我知道这是。

You're deleting dt.data(). 您正在删除dt.data()。 If I recall correctly this is guaranteed to be the internal buffer of the string. 如果我没记错的话,那肯定是字符串的内部缓冲区。

This may or may not be your underlying problem, like I said, I suspect the line above it is bad also since you pass in a pointer to a single character to what would seem to expect a buffer of some length. 就像我说的那样,这可能不是您的根本问题,我怀疑它上面的行也是不好的,因为您传递了一个指向单个字符的指针,该指针似乎可以期待一定长度的缓冲区。

I believe the problem (or at least part of the problem) lies with your allocation: 我认为问题(或至少部分问题)与您的分配有关:

char * imgData =  new char;

This only allocates 1 char, and then istream.read will assume that imgData is a buffer of chars (notice plural) and place whatever it reads into the single char you allocated, and then beyond that into the memory used by who knows what. 这只会分配1个char,然后istream.read将假定imgData是chars(注意复数)的缓冲区,并将其读取的内容放入您分配的单个char中,然后将超出的内容放入谁知道的内存中。

The result is typically called "undefined behaviour" - sometimes you'll get away with it as in the first instance, other times you won't, as in the second conversion. 结果通常称为“未定义的行为”-有时您会像第一个实例那样摆脱它,而其他时候则不会,就像第二个转换一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM