这两个指向char初始化的指针之间的区别

时间:2021-11-04 13:20:50

I know this has been answer before, but I can't find the question.
What are the differences between these two initialisations:

我知道之前已经回答了,但我找不到问题。这两个初始化有什么区别:

int main() 
{
    char* pch1;
    char* pch2;

    pch1 = (char*)malloc(sizeof(char) * 5);
    strcpy(pch1, "Text");

    pch2 = "Text";
}

4 个解决方案

#1


2  

First: don't cast the return value from malloc - it's a common source of errors. Do I cast the result of malloc?

第一:不要从malloc转换返回值 - 它是常见的错误来源。我是否施放了malloc的结果?

pch1 = malloc(sizeof(char) * 5);

assigns a pointer to a dynamically allocated block of 5 bytes on the heap.

在堆上分配一个指向动态分配的5个字节块的指针。

pch2 = "Text";

should ideally be avoided because it assigns a pointer to a string literal. String literals are read-only on most OSes and is also a common source of mistakes. If you do this you should make the pointer to const

理想情况下应避免使用它,因为它指定了一个指向字符串文字的指针。字符串文字在大多数操作系统上是只读的,也是错误的常见来源。如果你这样做,你应该指向const

const char * pch2 = "Text";

#2


2  

There are three main differences here:

这里有三个主要区别:

  • The first one copies the content of a string literal into dynamic memory, while the second one points to that literal directly.
  • 第一个将字符串文字的内容复制到动态内存中,而第二个直接指向该文字。

  • Modifying pch1 string is legal; modifying pch2 string is illegal
  • 修改pch1字符串是合法的;修改pch2字符串是非法的

  • You need to free pch1 to avoid memory leak.
  • 您需要释放pch1以避免内存泄漏。

For completeness, consider pch3 which is initialized like this:

为了完整性,请考虑像这样初始化的pch3:

char tmp[] = "Text";
char *pch3 = tmp;

This pch3 is modifiable like your pch1, but it does not need freeing, because the content of the string is copied into automatic memory.

这个pch3可以像你的pch1一样修改,但它不需要释放,因为字符串的内容被复制到自动内存中。

#3


1  

pch1 points to heap

pch1指向堆

you can modify it within bounderies

你可以在bounderies内修改它

plus you have to free it

再加上你必须释放它

other points to static data segment you can not modify it

其他指向静态数据段的您无法修改它

#4


0  

  1. pch1 will use heap memory for storing your data
  2. pch1将使用堆内存来存储您的数据

  3. pch2 - using stack memory
  4. pch2 - 使用堆栈内存

#1


2  

First: don't cast the return value from malloc - it's a common source of errors. Do I cast the result of malloc?

第一:不要从malloc转换返回值 - 它是常见的错误来源。我是否施放了malloc的结果?

pch1 = malloc(sizeof(char) * 5);

assigns a pointer to a dynamically allocated block of 5 bytes on the heap.

在堆上分配一个指向动态分配的5个字节块的指针。

pch2 = "Text";

should ideally be avoided because it assigns a pointer to a string literal. String literals are read-only on most OSes and is also a common source of mistakes. If you do this you should make the pointer to const

理想情况下应避免使用它,因为它指定了一个指向字符串文字的指针。字符串文字在大多数操作系统上是只读的,也是错误的常见来源。如果你这样做,你应该指向const

const char * pch2 = "Text";

#2


2  

There are three main differences here:

这里有三个主要区别:

  • The first one copies the content of a string literal into dynamic memory, while the second one points to that literal directly.
  • 第一个将字符串文字的内容复制到动态内存中,而第二个直接指向该文字。

  • Modifying pch1 string is legal; modifying pch2 string is illegal
  • 修改pch1字符串是合法的;修改pch2字符串是非法的

  • You need to free pch1 to avoid memory leak.
  • 您需要释放pch1以避免内存泄漏。

For completeness, consider pch3 which is initialized like this:

为了完整性,请考虑像这样初始化的pch3:

char tmp[] = "Text";
char *pch3 = tmp;

This pch3 is modifiable like your pch1, but it does not need freeing, because the content of the string is copied into automatic memory.

这个pch3可以像你的pch1一样修改,但它不需要释放,因为字符串的内容被复制到自动内存中。

#3


1  

pch1 points to heap

pch1指向堆

you can modify it within bounderies

你可以在bounderies内修改它

plus you have to free it

再加上你必须释放它

other points to static data segment you can not modify it

其他指向静态数据段的您无法修改它

#4


0  

  1. pch1 will use heap memory for storing your data
  2. pch1将使用堆内存来存储您的数据

  3. pch2 - using stack memory
  4. pch2 - 使用堆栈内存