简体   繁体   中英

Char* copying to another char* runtime error

When I try to run the folling code it's stops working and I don't get it why.

If I could get some help I would appreciate that.

const char *p="people";
char *q="random";
 while(*p)
{
    *q=*p;
    *p++;
    *q++;
}

在将char *分配给字符串文字( char *q="random"; )时,指针被定向到“只读”数据,因此任何试图修改( *q = ... )的操作都将不起作用如预期的那样,未定义的行为

First of all, the line

char *q="random";

is not correct in C++. If you turn on the warning level of the compiler, you should get some warning. By using -Wall with g++ , I get the following warning:

 warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] char *q="random"; 

It is not OK to modify the contents of what q points. Modifying the contents of what q points to is cause for undefined behavior.

What you need to do is create an array of char and modify the contents of the array.

char q[] = "random";

This allows you to modify the contents of q . However, if you define q to be an array, you can't use ++q or q++ . You will need to use another variable to iterate over the array.

Secondly, dereferencing the pointers the lines below seems to indicate to me that you are not sure what's happening.

*p++;
*q++;

Due to operator precedence, they are equivalent to:

*(p++);
*(q++);

The pointers are incremented, then they are dereferenced but the dereferenced values are not used. Once again, by turning the warning levels up, you can catch such issues. I get the following warnings with g++ -Wall .

  warning: value computed is not used [-Wunused-value] *p++; warning: value computed is not used [-Wunused-value] *q++; 

You can just increment the pointers and not try to dereference them.

Here's an updated version of your program that works for me.

int main()
{
   const char *p="people";
   char q[] = "random";
   char *cp = q;
   while(*p)
   {
      *cp = *p;
      p++;
      cp++;
   }
}

Caveat

The definition of the variable q is fragile in the above program. It works by coincidence that the length of the string "people" is equal to the length of the string "random" . If you were to change p to.

const char *p="a large number of people";

the program will break since q does not have enough space to hold such a large string.

To be safe, you'll have to use dynamic memory allocation.

#include <cstring>

int main()
{
   const char *p="a large number fo people";
   char* q = new char[std::strlen(p)+1];
   char *cp = q;
   while(*p)
   {
      *cp = *p;
      p++;
      cp++;
   }
   *cp = '\0';

   // Use q
   // ...
   //

   // Release dynamically allocated memory.
   delete [] q;
}

Your code is not copying a char* to a char* ; it's copying an array of char to an array of char . And the array that it's copying to is not modifiable.

Both, "people" and "random" are strings usually allocated in the read-only memory. Your program tries to write over the read-only 'random' string. Depending on the compiler and operating system the behavior will be different.

Do not do it!

You'd be better if you use an array for 'random':

char random[] = {"random"};
char *q = random;

or malloc

char *q = (char*)malloc(7);
strcpy(q, "random");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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