简体   繁体   English

boost :: scoped_array上的_BLOCK_TYPE_IS_VALID错误

[英]_BLOCK_TYPE_IS_VALID error on boost::scoped_array

After a huge amount of digging and searching I found the root of my problem. 经过大量的挖掘和搜索,我找到了问题的根源。 In essence this code is executed and, in its own project it causes the same error. 本质上,此代码被执行,并且在其自己的项目中会导致相同的错误。 I see that I cannot reset a smart pointer to a new string...but why? 我看到我无法将智能指针重置为新字符串...但是为什么呢? Also is there a simple way around this? 还有解决这个问题的简单方法吗?

scoped_array<char> sptr;
char* nptr = "Hello";

sptr.reset("");
sptr.reset(nptr);

EDIT - 编辑-

I think I've figured it out. 我想我已经知道了。 While resetting, the smart pointer tries to delete and empty character array ("") which, because the new operator was not used, was not allocated on the heap (ahem !!?!!?!???!?!). 重置时,智能指针尝试删除并清空由于未使用new运算符而未在堆上分配的字符数组(“”)(ahem !!?!!!!!! ???!?!!)。 Therefore this program will break miserably when it tries to deallocate the memory. 因此,该程序在尝试重新分配内存时将惨遭破坏。 So correct me if I'm wrong but would the string itself be stored in the program's executable byte stream itself? 因此,如果我错了,请纠正我,但是字符串本身会存储在程序的可执行字节流中吗? If so, just for future reference, is there a way to force the allocation of a new string? 如果是这样,是否仅供参考,是否有办法强制分配新字符串?

You're correct in identifying the error. 您确定错误是正确的。 scoped_array will call delete[] on the object it's holding; scoped_array将对其持有的对象调用delete[] that is, after all, the sole purpose of its existence. 毕竟,这是它存在的唯一目的。 You can't delete[] something that wasn't new[]'d , or you get undefined behavior. 您不能delete[]不是new[]'d东西,否则会得到未定义的行为。

However, you should just use std::string if you want a dynamic string. 但是,如果要使用动态字符串,则应仅使用std::string string。 Anything you try to create to allow scoped_array<char> to act like a string is just going to end up as a shoddy version of std::string . 您尝试创建的任何允许scoped_array<char>像字符串一样工作的东西最终都会变成std::string的伪劣版本。 Then it's as simple as: std::string s; s = ""; s = "Hello" 然后就这么简单: std::string s; s = ""; s = "Hello" std::string s; s = ""; s = "Hello" std::string s; s = ""; s = "Hello" . std::string s; s = ""; s = "Hello"

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

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