字符指针(由new分配)及其初始化

发布于 2024-12-05 14:53:14 字数 548 浏览 0 评论 0原文

我对字符指针有一些疑问:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

む无字情书 2024-12-12 14:53:14

你被标记为 C++。使用 string:

std::string t("terry"); 并让语言为您处理细节。

  1. 无论哪种方式都可以,具体取决于您的需求。如果稍后需要更改字符串,则必须分配内存,并且在分配时始终记住稍后删除它。

  2. 文字的第一个字母。

  3. 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.

  1. 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.

  2. The first letter of the literal.

  3. t=new char[5]; t[0] = 0;

独﹏钓一江月 2024-12-12 14:53:14
  1. 您可以执行后一个选项,但指针很可能指向只读内存区域,因此您应该仅使用 const char * 来执行此操作。
  2. 在这种情况下,t 最终将指向只读内存区域,从而泄漏这 6 个字节。
  3. 您可以使用 memset() 函数将整个数组设置为 0。

但是,可以使用 std::string 来代替,不要再考虑这个了。

  1. You can do the latter option, but the pointer will most likely point to a read-only memory region, so you should only do that with a const char *.
  2. In this case, t will ultimately point to a read-only memory region, leaking those 6 bytes.
  3. You can use the memset() function to set a whole array to 0.

But yes, use std::string instead and stop thinking about this.

一曲琵琶半遮面シ 2024-12-12 14:53:14

1. 如果字符串(例如“terry”)不会在程序中的任何地方发生更改,那么您当然可以更改

char* t = "terry";

并忘记为该字符串分配的内存,因为它将自行释放。但是,对于常量字符串,更好的编程实践是将 t 声明为 const char*,如下所示:

const char* t = "terry";

但如果要更改它,那么它要么

char* t = new char[6];
strcpy(t, "terry");

是 要么

char t[6];
strcpy(t, "terry");

在第一个中在使用 new 运算符的情况下,在不再需要该字符串后,您必须释放使用 new 分配的内存:

delete[] t;

在第二种情况下,是字符串底层的内存当 t 离开时会自动释放它在其中声明的 C++ 作用域(花括号)(但如果 t 是对象的成员,则仅当对象被破坏时才会释放内存)。

2. t 之后

char* t;
t = new char[6];
t = "terry";

确实会指向“terry”,但是释放 new 分配的内存所需的指针/地址会丢失永远。

3. 字符串空终止符('\0')就像其他字符一样:它们需要驻留在内存中的某个位置。您很明智地为“terry”分配了 6 个字节,其长度为 5 个字符:在

char* t = new char[6];
strcpy(t, "terry");

t 指向的内存块的第 6 个字节保存空终止符之后。但

char* t;

除了 t 指针之外,不分配任何内存。因此,如果您希望字符串仅包含空终止符(长度为零),您可以这样做:

char* t = new char[1];
t[0] = '\0';

1. If the string (e.g. "terry") is not going to get changed anywhere in you program, you can surely do

char* t = "terry";

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 as const char* like this:

const char* t = "terry";

But if it is going to get changed, then it's either

char* t = new char[6];
strcpy(t, "terry");

or

char t[6];
strcpy(t, "terry");

In the first case, where new operator is used, you will have to free the memory allocated with new after you no longer need the string:

delete[] t;

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 if t is a member of an object, the memory will be freed only when the object is destructed).

2. After

char* t;
t = new char[6];
t = "terry";

t would really point at "terry", but the pointer/address you would need to free the memory allocated with new 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

char* t = new char[6];
strcpy(t, "terry");

the 6th byte of the memory block pointed by t holds the null terminator. But

char* t;

does 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:

char* t = new char[1];
t[0] = '\0';
森林散布 2024-12-12 14:53:14
  1. 您不应该执行第二个操作,因为 tchar*,因此 t="terry" 已被弃用(并且您的编译器可能会给出此警告,指示赋值语句。)

  2. 分配的内存已泄漏,因为您没有使用它,并且 t 指向 terry。同样,这已被弃用(并且您的编译器可能会给出此警告,指示赋值语句)。

  3. 只需执行:t = new char[5]();现在已默认初始化。

一般来说,最佳实践是 std::string。所以使用它:

std::string t = "terry";

没有内存泄漏,没有不推荐使用的功能,没有警告。没有紧张。

  1. You shoudn't do the second, because t is char*, and so t="terry" is deprecated (and your compiler might give this warning indicating the assignment statement.)

  2. The allocated memory is leaked, since you didn't use it, and t points to terry. Again, this is deprecated (and your compiler might give this warning indicating the assignment statement).

  3. Just do : t = new char[5](); It's default initialized now.

As the best practice, in general is, std::string. So use it:

std::string t = "terry";

No memory leak, no deprecated feature, no warning. No tension.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文