Thread: copying strings to heap character arrays

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    copying strings to heap character arrays

    In my class, a data member is a pointer to a character array......which is assigned to memory from the heap when the constructor is called.

    One of the arguments to the constructor is a string, and I want to copy that string to the memory in the heap. Using the copy() member function of the string class does the job, but my compiler (Visual Studio 2005) complains that it is deprecated. Is there a better way to do this?

  2. #2

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The problem with switching to checked_copy is that it is not portable. If you are correctly setting the size of your dynamic array, it should be fine. Of course the truly safe solution is to use a string or vector<char> inside the class itself instead of the character array. Is there a reason you cannot do that?

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    You could easily write your own copy function. Is there any reason not to write it?
    I would do something like this:
    Code:
    #include <string>
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    class MyClass
    {
    	char* str;
    	void mycopy (string&);
    public:
    	MyClass(string s)
    	{
    		str = new char[strlen(s.c_str()) + 1];
    		mycopy(s);
    	}
    	void show()
    	{
    		cout << str;
    	}
    	//take care of rest (deallocation etc...)
    };
    
    void MyClass::mycopy(string& s)
    {
    	size_t i;
    	for(i = 0; i < strlen(s.c_str()); i++)
    	{
    		str[i] = s[i];
    	}
    	str[i] = '\0';
    }
    
    
    int main()
    {
    	string q = "Test";
    	MyClass t(q);
    	t.show();
    }
    Last edited by Micko; 11-26-2006 at 02:16 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why would you write your own when good ones already exist? Your own would have to be debugged and tested, the existing ones have gone through that already.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just a quick question about character arrays
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 04-03-2006, 07:20 AM
  2. Comparing Character Arrays
    By LostNotFound in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 12:42 PM
  3. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. copying character arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 05:39 PM