Why must a pointer to a char array need strcpy to assign characters to its array and double quotes assignment will not work?

前端 未结 9 1783
耶瑟儿~
耶瑟儿~ 2021-02-14 09:23

The first example does not work when you go to delete the pointer. The program either hangs when I add the null terminator or without it I get:

Debug Assertion Fai

相关标签:
9条回答
  • 2021-02-14 10:01

    You mistake two things: making pointer point to something different (this is what assignment does) and copying some data to a place pointed by pointer.

    at = "tw";
    

    this code makes at point to a literal "tw" created somewhere in read-only memory. Trying to write to it is an undefined behaviour.

    char *at = new char [3];
    strcpy(at,"t");
    

    this code allocates memory for three chars and makes at point to this part of memory (line 1) and then copies some data to memory pointed by at.

    And remember, that memory allocated with new[] should be deallocated with delete[], not delete

    I advice you to learn more about pointers. This discussion covers this.

    0 讨论(0)
  • 2021-02-14 10:05

    There are 3 things to understand:

    1) char *at; is just a pointer variable.
    A pointer variable simply means that it holds a memory address.

    2) new char[3] returns the starting address of the memory allocated on the heap.

    3) "hello" returns the address of the string literal.

    char *at = new char [3];
    //at now contains the address of the memory allocated on the heap
    
    
    at = "hello";
    //at now contains the address of the static string. 
    // (and by the way you just created a 3 byte memory leak)
    
    
    delete[] at; 
    //WOOPS!!!! you can't do that because you aren't deleting 
    // the original 3 chars anymore which were allocated on the heap!
    //Since at contains the string literal's memory address you're 
    // trying to delete the string literal.
    

    A note about modifying read only memory:

    Also you should never be modifying a string literal. I.e. this should never be done:

    char *at = "hello";
    at[2] = '\0'; 
    

    The memory for string literals must be read only and if you change it, the results are undefined by the C++ language.

    Since you're using C++:

    Since you're using C++ please consider using the std::string type instead.

    #include <string>
    
    using namespace std;
    
    int main(int argc, char **argv)
    {
      string s = "hello";
      s += " world!";
    
      //s now contains "hello world!"
    
      s = "goodbye!";
    
      //Everything is still valid, and s contains "goodbye!"
    
    
      //No need to cleanup s. 
    
      return 0;
    }
    
    0 讨论(0)
  • 2021-02-14 10:07

    A pointer holds an address. The = operator for a pointer changes the address held.

    at = "tw";
    

    Makes at point to the array "tw" (an array created by the compiler to hold the characters tw), it no longer points to the array you created with new. created in the file.

    at[2] = '\0';
    

    Adds a NULL to the end of the complier array.

    0 讨论(0)
提交回复
热议问题