我的大学教授最近给了我们一个实现我们自己的智能指针类的任务。在他用于复制字符串的样板代码中,我发现这件作品很漂亮:
while (*sea++ = *river++);// C Sting copy
我进一步研究了这段代码,发现它是 strcpy.c 中的确切代码,并在下面的stackoverflow问题中进一步解释了它的工作原理: How does “while(*s++ = *t++)” copy a string?
当我尝试在我的下面的代码中使用这个语法糖时,它给了垃圾作为结果并删除了存储在" river"中的字符串:
#include<iostream>
#include<cstring>
using namespace std;
void main()
{
const char *river = "water";// a 5 character string + NULL terminator
char *sea = new char[6];
while (*sea++ = *river++);
cout << "Sea contains: " << sea << endl;
cout << "River contains: " << river << endl;
}
结果:
我知道我可以使用以下代码简单地实现所需的结果:
int i = 0;
while (i<6)
{
sea[i] = river[i];
i++;
}
但这不是我想要的答案。我想知道我的while循环的实现或我的char指针的实例化有什么问题吗?
答案 0 :(得分:7)
您正在显示垃圾,因为当您显示它们时,指针指向垃圾。您在循环时推进指针,但在显示数据时需要使用原始指针。
此外,您有内存泄漏,因为您没有释放char[]
缓冲区。
请改为尝试:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const char *river = "water";// a 5 character string + NULL terminator
char *sea = new char[6];
const char *p_river = river;
char *p_sea = sea;
while (*p_sea++ = *p_river++);
cout << "Sea contains: " << sea << endl;
cout << "River contains: " << river << endl;
delete [] sea;
return 0;
}
答案 1 :(得分:5)
“错误”在技术上是一种未定义的行为,因为指针超出了对它们引用的内存的限制。
有趣的部分是为什么会发生这种情况。
这是因为指针之间的混淆以及它们指向的内容。
sea
和river
不是字符串。字符串是驻留在内存中某处的匿名变量,两个指针指示它们的开始。
你永远不应该触摸它们,否则你将无法再进一步访问这些字符串。
如果你需要移动指针,请使用其他指针。
更合适的例子应该是这个
using namespace std;
int main() //< note `void main` is a C++ dialcet
{
// note the use of `const` after the `*`:
// you cannot modify these pointers.
const char * const river = "water"; // a 5 character string + NULL terminator
char * const sea = new char[6];
{
// in this inner scope, other non-const pointers are
// initialized to point to the same memory
const char* r = river;
char* s = sea;
while (*s++ = *r++); // the loop moves the mutable pointers
// note how `river++` or `sea++` is an error, being them `*const`
}
// there are no more `r` and `s` here, but `sea` and `river` are still the same.
cout << "Sea contains: " << sea << endl;
cout << "River contains: " << river << endl;
//now we have an array allocated with new to return to the system
delete[] sea; //< the importance to not move the `sea` pointer
}
请注意delete
如何删除数组,而不删除指针。
要提前做更多事情,可以做两件事。
第一个使内部范围成为正确的功能:
using namespace std;
void copy(const char* r, char* s)
{
// in this function, other non-const pointer (parameters) are
// initialized to point to the same memory upon call
while (*s++ = *r++); // the loops moves the mutable pointers
// note how `river++` or `sea++` is an error, being them not visible.
}
int main() //< note `void main` is a C++ dialect
{
const char * const river = "water"; // a 5 character string + NULL terminator
char * const sea = new char[6];
copy(river, sea);
cout << "Sea contains: " << sea << endl;
cout << "River contains: " << river << endl;
//now we have an array allocated with new to return to the system
delete[] sea; //< the importance to not move the `sea` pointer
}
并且第二在相同的上下文中删除了new/delete
对,例如使用std::unique_ptr<char[]>
但这太过分了!
答案 2 :(得分:2)
因为ASCII-art总是有助于搞清楚指针问题......
这是在你的while循环之前:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
...
<Window.Resources>
<Style x:Key="DashedPolyLine" TargetType="{x:Type Polyline}">
<Setter Property="StrokeDashArray" Value="2 3 2" />
</Style>
</Window.Resources>
...
<chartingToolkit:LineSeries Title="Title" DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding BindingValue}" PolylineStyle="{StaticResource DashedPolyLine}"/>
但是在你的循环之后,你有这个:
SELECT tblSourceData.Athlete,
(SELECT Max(tblSourceData.Swim) from tblSourceData) AS MaxOfSwim,
(SELECT Max(tblSourceData.Bike) from tblSourceData) AS MaxOfBike,
(SELECT Max(tblSourceData.Run) from tblSourceData) AS MaxOfRun,
Count(tblSourceData.Athlete) AS EventsCompleted
FROM tblSourceData
WHERE (((tblSourceData.Gender)="f"))
GROUP BY tblSourceData.Athlete
HAVING (((Count(tblSourceData.Athlete))>=5));
所以你看,副本确实已经完成,但是你也移动了指针。这意味着如果您尝试将它们打印出来,那么在这些内存块之后,您将获得所有垃圾。
如果您要在while循环之前保存指针的副本,并打印出来,那么您将获得您正在寻找的输出。