简体   繁体   English

将字符串形式的char指针复制到char指针

[英]Copy string form char pointer to char pointer

char * p_one = "this is  my first char pointer";
char * p_two= "this is second";
strcpy(p_one ,p_two);

consider the above code. 考虑上面的代码。 This is giving access violation error. 这给出了访问冲突错误。 So please help to understand 所以请帮忙了解

  1. where is the current "this is my first char pointer" string stored in memory? 当前的"this is my first char pointer"字符串在哪里存储在内存中? heap or stack 堆或栈
  2. why I need to allocate memory for p_one before call strcpy , even it's already storing the first string. 为什么我需要在调用strcpy之前为p_one分配内存,即使它已经存储了第一个字符串。 why "this is second" string cannot copy to same location? 为什么"this is second"字符串无法复制到同一位置?
  3. If I allocate memory for p_one before call strcpy then what happen to "this is my first char pointer" string that was pointed by p_one ? 如果在调用strcpy之前为p_one分配了内存,那么p_one指向的"this is my first char pointer"字符串会发生什么情况? is it keep in memory? 它保存在内存中吗?
  4. How strcpy knows specific pointer have allocated memory or not? strcpy如何知道特定的指针是否分配了内存?
  1. Implementation defined(usually read only) memory. 实现定义(通常为只读)内存。 [Ref 1] [参考资料1]
  2. You do not need to as long as you don't modify the source string literal. 只要您不修改源字符串文字,就不需要。
  3. If you allocate memory to p_one , then it will point to the newly allocated memory region, the string literal may/may not stay in the memory, but it is guaranteed to be alive throughout the lifetime of the program.String literals have static duration lifetime. 如果您为p_one分配内存,则它将指向新分配的内存区域,字符串文字可能会/可能不会保留在内存中,但可以保证在程序的整个生命周期中都处于活动状态。 。 [Ref 2] [参考2]
  4. It doesn't. 没有。 It is users responsibility to ensure that. 用户有责任确保做到这一点。

Good Read: 读得好:
[Ref 1] What is the difference between char a[] = ?string?; [参考文献1] char a [] =?string有什么区别? and char *p = ?string?;? 和char * p =?string?;?
[Ref 2] "life-time" of string literal in C [参考文献2] C中字符串文字的“生存期”

First off your compiler should be warning that the p_one and p_two are actually const char * because the compiler allocates the storage of this string at compile time. 首先,编译器应警告p_one和p_two实际上是const char *因为编译器会在编译时分配此字符串的存储。

The reason you cannot modify them is because in theory you could overwrite memory after them, this is what causes hack attack with a stackoverflow. 无法修改它们的原因是,从理论上讲,您可以在它们之后覆盖内存,这就是使用stackoverflow引起黑客攻击的原因。

Also the compiler could be smart and realize that you you use this string in 10 places but notices it is the same, so modifying from one place changes it - but that destroys the logic of the other 9 places that uses it 同样,编译器可能很聪明,意识到您在10个地方使用了此字符串,但是注意到它是相同的,因此从一个地方进行修改会更改它-但这会破坏使用该字符串的其他9个地方的逻辑

Answering all the questions in order 依次回答所有问题

  1. It's bit straight forward that your char pointer is always stored in stack. 直截了当,您的char指针始终存储在堆栈中。 Remember even though you are using Memory allocation, it is only for determining the length of the string and appending the '\\0' character. 请记住,即使您正在使用内存分配,它也仅用于确定字符串的长度并附加'\\ 0'字符。

This would be one solution, according to code you have mentioned: 根据您提到的代码,这将是一种解决方案:

int main()
{
   char * p_one = "this is  my first char pointer";
   char * p_two= "this is second";
   size_t keylen=strlen(p_two);
   p_one=(char *)malloc(keylen*sizeof(char));
   strncpy(p_one ,p_two,strlen(p_one));
   printf("%s",p_one);

   return 0;
}
  1. When you have declared a char pointer it only points to the memory allocation. 声明了char指针后,它仅指向内存分配。 So string copy doesn't point to the end of character. 因此,字符串复制不指向字符的末尾。 Hence it is always better to use strncpy, in this conditions. 因此,在这种情况下,最好使用strncpy。

  2. Yes it is allocating memory. 是的,它正在分配内存。

  3. it is bad practice to cast the result of malloc as you will inhibit possible runtime errors being thrown, thanks Gewure 强制转换malloc的结果是错误的做法,因为您将阻止抛出可能的运行时错误,谢谢Gewure

When you have a string literal in your code like that, you need to think of it as a temporary constant value. 当您的代码中有这样的字符串文字时,您需要将其视为一个临时常量值。 Sure, you assigned it to a char* , but that does not mean you are allowed to modify it. 当然,您已将其分配给char* ,但这并不意味着您可以对其进行修改。 Nothing in the C specification says this is legal. C规范中没有任何内容说这是合法的。

On the other hand, this is okay: 另一方面,这没关系:

const size_t MAX_STR = 50;
char p_one[MAX_STR] = "this is  my first char pointer";
const char *p_two = "this is second";
strcpy( p_one, p_two );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM