将两个字符串一起分配,获取访问读取冲突
Assigning two strings together getting Access Read Violation
我正在尝试将字符串传递给类突变器并将私有成员设置为该字符串 这是发送字符串的代码
void parseTradePairs(Exchange::Currency *curr, std::string *response, int begin, int exit)
{
int start;
int end;
string temp;
string dataResponse;
CURL *tempCurl;
initializeCurl(tempCurl);
int location = response->find("marketid", begin);
if(location <= exit)
{
start = location + 11;
begin = response->find("label", start);
end = begin - start - 3;
findStrings(start, end, temp, response);
getMarketInfo(tempCurl, temp, dataResponse);
curr->_coin->setExch(temp); // here is the line of code that is sending the string
dataResponse >> *(curr->_coin);
curr->_next = new Exchange::Currency(curr, curr->_position + 1);
parseTradePairs(curr->_next, response, begin, exit);
}
}
这是 coin 类中的突变器,它接收字符串并将其分配给_exch
void Coin::setExch(string exch)
{
_exch = exch;
}
我已经逐步浏览了它,并确保 exch 中有字符串。"105",但一旦达到 _exch = exch;我收到阅读违规。我也尝试以指针身份传递。我认为它不应超出范围。并且类中的字符串变量在默认构造函数中初始化为零,但这同样很重要,除非我尝试从中读取而不是写入它。
/* defualt constructor */
Coin::Coin()
{
_id = "";
_label = "";
_code= "";
_name = "";
_marketCoin = "";
_volume = 0;
_last = 0;
_exch = "";
}
Exchange::Exchange(std::string str)
{
_exch = str;
_currencies = new Currency;
std::string pair;
std::string response;
CURL *curl;
initializeCurl(curl);
getTradePairs(curl, response);
int exit = response.find_last_of("marketid");
parseTradePairs(_currencies, &response, 0, exit);
}
int main(void)
{
CURL *curl;
string str;
string id;
Coin coin1;
initializeCurl(curl);
Exchange ex("cryptsy");
curl_easy_cleanup(curl);
system("pause");
return 0;
}
class Exchange
{
public:
typedef struct Currency
{
Currency(Coin *coin, Currency *next, Currency *prev, int position) : _coin(coin), _next(next), _prev(prev), _position(position) {}
Currency(Currency *prev, int position) : _prev(prev), _position(position), _next(NULL), _coin(&Coin()){}
Currency() : _next(NULL), _prev(NULL), _position(0) {}
Coin *_coin;
Currency *_next;
Currency *_prev;
int _position;
};
/* constructor and destructor */
Exchange();
Exchange(std::string str);
~Exchange();
/* Assignment operator */
Exchange& operator =(const Exchange& copyExchange);
/* Parse Cryptsy Pairs */
friend void parseTradePairs(Currency *curr, std::string *response, int begin, int exit);
private:
std::string _exch;
Currency *_currencies;
};
这是我将其更改为修复它的内容。typedef 结构
货币{
Currency(Coin *coin, Currency *next, Currency *prev, int position) : _coin(coin), _next(next), _prev(prev), _position(position) {}
Currency(Currency *prev, int position) : _prev(prev), _position(position), _next(NULL), _coin(&Coin()){}
Currency()
{
_next = NULL;
_prev = NULL;
_position = 0;
_coin = new Coin();
}
Coin *_coin;
Currency *_next;
Currency *_prev;
int _position;
};
Currency
的默认构造函数是:
Currency() : _next(NULL), _prev(NULL), _position(0) {}
并且似乎没有为_coin
分配任何内存。您的Exchange::Exchange(std::string str)
构造函数还执行以下操作:
_currencies = new Currency;
它调用 Currency
的默认构造函数,然后传递给parseTradePairs()
,所以很可能是这一行:
curr->_coin->setExch(temp);
因此,正在尝试取消引用未初始化的指针,结果是访问冲突。
相关文章:
- 获取字符串的长度并将其分配给数组
- 如果 const 不分配内存,为什么我可以获取 const 的地址?
- 为什么在分配给成员变量之前获取unique_ptr的返回是一个问题?
- 获取指针的地址会为其分配一个值
- 创建一个类来访问和指定向量类型,并构建一个获取位置并为其分配区域的类
- windBG DDS - 无法获取分配内存的源
- 从 JNI 调用的 DLL 从哪里获取其内存以进行分配,例如 Malloc
- 呼叫获取后,将共享_ptr分配给另一个
- 获取“将返回值分配给新变量”的警告和输出是内存地址
- 通过指向非多态类型的基类的指针获取已分配内存的地址
- 尝试将返回无符号值的函数分配给指向无符号的指针时,继续获取 SIGSEGV
- 我想从文本文件中获取信息并将该信息分配给类对象
- 尝试分配函数指针时获取"Void value not ignored as it ought to be"
- 尝试从文件中获取数据时,在C 中的分配函数中没有正确的字符串
- 获取 jemalloc 中每个内存分配的 bin 大小
- 从动态分配的阵列获取内存泄漏
- 使用标准化函数/OPENCV和C 获取内存分配错误
- 从动态分配的2D数组指针中获取行
- 如何获取程序分配的内存大小
- 如何在C++中获取动态分配的int数组的大小