动态分配char [英] dynamically allocated char

查看:172
本文介绍了动态分配char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main()

{
   char *second= new char("hello");
   char *first="hi";
   char third[]="new";
}

我是c ++的新手,并不真正理解char是如何工作的,为什么第一个给出编译器错误,以及这三种声明方式的区别,以特定方式声明它们的优点和优点。

I am new to c++ and don't really understand how char works, why the first one give a compiler error, and what are the differences of those 3 way of declaration , the strength and benefits of declaring it in a particular way.

谢谢

有人提到第二种形式是只读,为什么我可以改变它。假设我有下面的代码

Hmm, as someone mentions that the second form is read only, why could I Change it. suppose I have the code below

 int main()

{
   char *second= new char("hello");
   char *first="hi";
   char third[]="new";
   first="world";
}



如上所述的代码仍然会执行,为什么会这样? ,那么哪种形式更好,如果我想读取输入,但不知道字符串的大小?

code like above will still execute, why is it so? , then which form is better if I want to read an input but don't know the size of the string?

推荐答案

知道

"abc"

在某处分配静态存储,这会持续整个程序的生命周期。你不能写入这个存储,所以C ++给它的类型 char const [N] (N个常量字符的数组)。现在,以下指向该存储的指针

allocates static storage somewhere, which lasts the whole program lifetime. You cannot write to that storage, so C++ gives it the type char const[N] (an array of N constant characters). Now, the following makes a pointer point to that storage

char *first = "hi";

因为这会删除 const 的初始化指针已被弃用。它的工作原理是保持与C的向后兼容性,其中字符串文字没有一个const类型(但仍然是只读)。

Since that drops a const, that way of initializing the pointer is deprecated. That it works at all is just to keep backward compatibility with C, where a string literal does not have a const type (but is still read only). Prefer the following instead

char const *first = "hi";

相反,你显示的最后一种方式将字符串文字的内容复制到数组,可写,并且大小使得字符串字面只适合它。

In constrast, the last way you have shown copies the string literal's content to an array, which will be writable, and be sized so the string literal just fits into it.

char third[] = "new";

如果你在一个函数中这样做,那么作为所有变量,离开其范围。现在,你所展示的第一种方式是不同的。它动态创建一个字符。您可以像这样初始化

If you do that in a function, then as all variables, that array will be cleaned up when you leave its scope. Now, the first way you have shown is different. It creates a character dynamically. You could have initialized it like this

char *c = new char('A');

因为这是动态发生的,你需要明确告诉编译器何时应该释放内存



And since that happens dynamically, you need to tell the compiler explicitly when it should free the memory

delete c;

但是不能使用字符串字面量初始化字符。你可能想到的是动态创建存储,用字符串字面量初始化。这不可能使用 new 。初始化动态数组的唯一形式是将其置零,但不能使用字符串字面量或另一个数组的内容直接初始化。对于这种使用 new 的形式,很少需要直接这样做。如果需要,您可以创建一个适当大小的动态数组,然后将字符串字符串复制到该缓冲区

But you cannot initialize the character with a string literal. What you probably had in mind is creating storage dynamically, initialized with the string literal. That's not possible using new. The only form of initializing a dynamic array is to zero it out, but you cannot directly-initialize with the content of a string literal or another array. For this form of using new, there is rarely a need to do that directly. If you want, you can do it by creating a dynamic array of the right size, and then copy bytes from the string literal to that buffer

char *c = new char[sizeof "hello"]; // sizeof "hello" will give 6
std::strcpy(c, "hello");

delete[] c; // delete[] is for deleting dynamic arrays

请记住,这是相当低级别,建议您使用字符串

Remember that this is quite low-level, and i recommend you to use strings

std::string s = "hello"; // s.size() gives you its size

连接,索引和这些东西也是可用的。

It completely manages memory for you. Concatenation, indexing and that stuff is available too.

这篇关于动态分配char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆