字符指针(由new分配)及其初始化
我对字符指针有一些疑问:
1.当我们编写声明时:
char *t;
我们需要这样做
t=new char[6];
strcpy(t,"terry");
还是直接
t="terry";
将做..
2.此外,如果我们遵循 do
char *t;
t=new char[6];
t="terry";
将现在指向堆中分配的内存或 terry 的第一个字母(如果我们从指针操作的角度来看)。
3.如果我写:
char *t;
然后我必须将't'初始化为'\0'(但t应该指向分配的内存空间)..我该怎么做,因为我的mvc 2010编译器不允许...
t=new char[5](0);//0 is the ascii value of '\0'
i have some doubts regarding character pointers:
1.when we write the declaration:
char *t;
do we need to do
t=new char[6];
strcpy(t,"terry");
or directly
t="terry";
will do..
2.Also if we follow do
char *t;
t=new char[6];
t="terry";
will t now point to the allocated memory from heap or the first letter of terry(if we look from the point of view of manipulation of pointers).
3.if i write:
char *t;
and then i have to initialise 't' to '\0'(but t should point to an allocated memory space)..how do i do this because my mvc 2010 compiler doesnt allow...
t=new char[5](0);//0 is the ascii value of '\0'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你被标记为 C++。使用 string:
std::string t("terry");
并让语言为您处理细节。无论哪种方式都可以,具体取决于您的需求。如果稍后需要更改字符串,则必须分配内存,并且在分配时始终记住稍后删除它。
文字的第一个字母。
t=新字符[5]; t[0] = 0;
You're tagged C++. Use string:
std::string t("terry");
and let the language take care of the details for you.Either way will do, depending on your needs. If you need to change the string later you have to allocate the memory, and when you allocate always remember to delete it later.
The first letter of the literal.
t=new char[5]; t[0] = 0;
const char *
来执行此操作。memset()
函数将整个数组设置为 0。但是,可以使用
std::string
来代替,不要再考虑这个了。const char *
.memset()
function to set a whole array to 0.But yes, use
std::string
instead and stop thinking about this.1. 如果字符串(例如“terry”)不会在程序中的任何地方发生更改,那么您当然可以更改
并忘记为该字符串分配的内存,因为它将自行释放。但是,对于常量字符串,更好的编程实践是将
t
声明为const char*
,如下所示:但如果要更改它,那么它要么
是 要么
在第一个中在使用
new
运算符的情况下,在不再需要该字符串后,您必须释放使用new
分配的内存:在第二种情况下,是字符串底层的内存当
t
离开时会自动释放它在其中声明的 C++ 作用域(花括号)(但如果 t 是对象的成员,则仅当对象被破坏时才会释放内存)。2.
t
之后确实会指向“terry”,但是释放
new
分配的内存所需的指针/地址会丢失永远。3. 字符串空终止符('\0')就像其他字符一样:它们需要驻留在内存中的某个位置。您很明智地为“terry”分配了 6 个字节,其长度为 5 个字符:在
t
指向的内存块的第 6 个字节保存空终止符之后。但除了
t
指针之外,不分配任何内存。因此,如果您希望字符串仅包含空终止符(长度为零),您可以这样做:1. If the string (e.g. "terry") is not going to get changed anywhere in you program, you can surely do
and forget about the memory allocated for the string as it will be freed on its own. However, a better programming practice for constant strings would be declaring
t
asconst char*
like this:But if it is going to get changed, then it's either
or
In the first case, where
new
operator is used, you will have to free the memory allocated withnew
after you no longer need the string:In the second case, the memory underlying the sting will be freed automatically when
t
leaves the C++ scope (curly braces) it was declared in (but ift
is a member of an object, the memory will be freed only when the object is destructed).2. After
t
would really point at "terry", but the pointer/address you would need to free the memory allocated withnew
is lost for good.3. String null terminators ('\0') are just like other characters: they need reside somewhere in the memory. You were wise enough to allocate 6 bytes for "terry", which is 5 characters in length: after
the 6th byte of the memory block pointed by
t
holds the null terminator. Butdoes not allocate any memory apart from the
t
pointer. So, if you want a string to contain only a null terminator (be zero-length), you could do it in this way:您不应该执行第二个操作,因为
t
是char*
,因此t="terry"
已被弃用(并且您的编译器可能会给出此警告,指示赋值语句。)分配的内存已泄漏,因为您没有使用它,并且
t
指向terry
。同样,这已被弃用(并且您的编译器可能会给出此警告,指示赋值语句)。只需执行:
t = new char[5]();
现在已默认初始化。一般来说,最佳实践是
std::string
。所以使用它:没有内存泄漏,没有不推荐使用的功能,没有警告。没有紧张。
You shoudn't do the second, because
t
ischar*
, and sot="terry"
is deprecated (and your compiler might give this warning indicating the assignment statement.)The allocated memory is leaked, since you didn't use it, and
t
points toterry
. Again, this is deprecated (and your compiler might give this warning indicating the assignment statement).Just do :
t = new char[5]();
It's default initialized now.As the best practice, in general is,
std::string
. So use it:No memory leak, no deprecated feature, no warning. No tension.