简体   繁体   English

C ++ Delete []在VS 2013中给出错误

[英]C++ Delete[] giving error in VS 2013

I am trying to remove a `const char * 我正在尝试删除`const char *

int len = str.length();
const char * c = str.c_str();
unsigned short * s = new unsigned short[len];
for (int i = 0; i < len; i++) {
    s[i] = c[i] - '0'; // I dislike this cheat
}


delete[] c; // Gives error.

c is also not used after it is deleted. 删除后也不会使用c I am newer to C++ and I am trying to learn the delete operator and it is not going so well :/ 我是C ++的新手,正在尝试学习delete运算符,但操作不太顺利:/

I don't support the usage of new and delete in cases like these, but to point out your error, you're deleting memory not allocated via new . 在这种情况下,我不支持使用newdelete ,但是要指出您的错误,您正在删除未通过new分配的内存。

You probably meant to write delete[] s; 您可能打算写delete[] s;
(Because s is allocated via new[] ) (因为s是通过new[]分配的)

Also, s could easily be a std::vector<unsigned short> : 另外, s很容易成为std::vector<unsigned short>

// Initializes a vector of unsigned shorts with a specified size.
std::vector<unsigned short> s(len);
const char * c = str.c_str();
delete[] c; // Gives error.

c is a pointer to the contents of str that's still owned and managed by the str object itself, may not be dynamically allocated (eg it could be a pointer to a Short String Optimisation buffer internal to the str object), let alone by new[] , and even if it happens to be the str object will do a delete[] when it goes out of scope or has copied the data to a larger buffer for whatever reason. c是一个指向的内容str是仍然拥有和管理的str对象本身,可能无法动态分配(例如,它可能是一个指向一个简短的字符串优化缓冲区内的str对象),更不用说new[] ,即使str对象恰好超出范围或出于任何原因将数据复制到更大的缓冲区时,即使str对象也会执行delete[] Summarily, after calling c_str() you can use the pointer until the str object is resized or "destructed", but don't need to do any clean-up yourself. 概括地说,在调用c_str() ,可以使用指针,直到调整str对象的大小或对其进行“销毁”为止,但是您无需自己进行任何清理。


Looking at the rest of your code... 查看其余的代码...

int len = str.length();
const char * c = str.c_str();
unsigned short * s = new unsigned short[len];
for (int i = 0; i < len; i++) {
    s[i] = c[i] - '0'; // I dislike this cheat
}
delete[] c; // Gives error.

...the only error per se is that you delete[] c , but you don't delete[] s and probably should somewhere, some time after it's no longer needed. ...本身的唯一错误是您delete[] c ,但您没有delete[] s并且可能应该在某个地方,不再需要它了一段时间。

That said, you can use std::vector to store the ASCII codes like this (for C++11): 也就是说,您可以使用std::vector来存储这样的ASCII代码(对于C ++ 11):

std::vector<unsigned short> s;

for (char c : str)
    s.push_back(c - '0');

If you don't have C++11, then: 如果您没有C ++ 11,则:

for (int i = 0; i < str.length(); ++i)
    s.push_back(c[i] - '0');

...or if you want to try iterators ... ...或者如果您想尝试迭代器 ...

std::vector<unsigned short> s;
for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
    s.push_back(*i - '0');

You could also use std::copy with an inserter, more declarative but a bit over-the-top IMHO: 您还可以将std::copy与插入程序配合使用,但更具声明性,但有点头疼,恕我直言:

std::copy(str.begin(), str.end(), std::back_inserter(s));

No need to delete c, it's only aa pointer to an array (that contains a null-terminated sequence of characters of "str"). 无需删除c,它只是一个指向数组的指针(该数组包含以空终止的“ str”字符序列)。

The array is still stored in variable "str". 该数组仍存储在变量“ str”中。

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

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