[英]c++ pointer allocating more space than specified when using Visual Studio
When when I run this code in Visual Studio I get a bunch of garbage characters at the end but when I run it in an online compiler I get the exact characters I want?("olleH"). 当我在Visual Studio中运行此代码时,最后得到一堆垃圾字符,但是当我在联机编译器中运行它时,我得到了我想要的确切字符?(“ olleH”)。 Please be gentle, im a noob. 请保持温柔,我是菜鸟。
#include<iostream>
#include<string>
int main() {
std::string stringOne{ "Hello" };
int stringLength = stringOne.length();
char* stringTwo = new char[stringLength];
for (int i = 0; i < stringLength; ++i) {
*(stringTwo + i) = stringOne[stringLength - i - 1];
}
std::string stringThree = stringTwo;
std::cout << stringThree << std::endl;
}
Here's what you get with the char* stringTwo = new char[stringLength];
这就是使用char* stringTwo = new char[stringLength];
statement when stringLength
is five (as it is here): stringLength
为5时的声明(如此处所示):
_____stringTwo_____
/ \
+---+---+---+---+---+------------------+
| ? | ? | ? | ? | ? | <arbitrary data> |
+---+---+---+---+---+------------------+
Then, your loop will populate it as follows: 然后,您的循环将如下填充:
_____stringTwo_____
/ \
+---+---+---+---+---+------------------+
| o | l | l | e | H | <arbitrary data> |
+---+---+---+---+---+------------------+
Then you treat is as a C-style string, despite the fact that there is no null terminator on the string. 然后您将其视为C样式的字符串,尽管该字符串上没有空终止符。 That means string functions will read beyond the end of the data and process whatever exists there. 这意味着字符串函数将读取超出任何存在那里的数据和过程的结束。 What you should be doing is constructing a real C style string, terminator and all: 您应该做的是构造一个真实的C样式字符串,终止符以及所有内容:
char *stringTwo = new char[stringLength+1];
for (int i = 0; i < stringLength; ++i)
*(stringTwo + i) = stringOne[stringLength - i - 1]; // or: stringTwo[i]
*(stringTwo + stringLength) = '\0'; // stringTwo[stringLength]
That will give you the more correct: 那会给你更正确的:
_______stringTwo________
/ \
+---+---+---+---+---+----+-----------------+
| o | l | l | e | H | \0 |<arbitrary data> |
+---+---+---+---+---+----+-----------------+
Of course, the use of C style strings in C++ is pretty much the textbook definition of a bad idea, suitable only in very rare circumstances if any. 当然,在C ++中使用C样式字符串几乎是教科书中定义的一个坏主意,仅适用于非常罕见的情况(如果有)。
Unless you want to be known as a C+ developer (that strange breed of C developer that never quite made the transition to C++ fully), you should embrace the more modern stuff found within C++, including std::string
and the stuff from <algorithms>
(if your need is to get something done rather than learn lower-level programming in an educational context): 除非您想成为C +开发人员(从来没有完全过渡到C ++的那种奇怪的C开发人员),否则您应该接受C ++中发现的更现代的内容, 包括 std::string
和<algorithms>
(如果您需要做点事情而不是在教育背景下学习低级编程):
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string str("Hello");
std::reverse<std::string::iterator>(str.begin(), str.end());
std::cout << str << std::endl;
}
std:string is not null terminated string.
#include<iostream>
#include<string>
int main() {
std::string stringOne{ "Hello" };
int stringLength = stringOne.length();
char* stringTwo = new char[stringLength+1]; // allocate one byte extra to store null character
for (int i = 0; i < stringLength; ++i) {
*(stringTwo + i) = stringOne[stringLength - i - 1];
}
*(stringTwo + stringLength) = '\0'; // add null character
std::string stringThree = stringTwo;
std::cout << stringThree << std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.