DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29

Thread: Move Semantics Techniques Program Error

  1. #16
    Join Date
    Nov 2003
    Posts
    4,118
    Just as you exchange (i.e., swap, which is the more common term) strings:
    string s1, s2, temp;
    temp = s1
    s1 = s2;
    s2 = temp;

    replace string with a pointer, and that's basically it.
    Danny Kalev

  2. #17
    Join Date
    May 2007
    Posts
    843
    Is it something like that but how come in the articles, we need to calculate the pointer bytes.

    Sorry for my stupidity.

    int *first, int *temp;
    int *second;

    tmp = first;
    first = second;
    second = tmp;
    Thanks for your help.
    A billion thanks for your help.

  3. #18
    Join Date
    Nov 2003
    Posts
    4,118
    A tiny fix:

    int *first, *temp, *second;
    tmp = first;
    first = second;
    second = tmp;

    Effectively, yes, that's pointer swapping. When dealing with pointers to large arrays, you can improve performance significantly by swapping pointers instead of copying the data they're pointing to.
    Danny Kalev

  4. #19
    Join Date
    May 2007
    Posts
    843
    For instance, when we deal with char *. I want assign one char * to another char *;

    I have try below but i don't whether it is correct.

    class sentence
    {
    private:
    char *aSentence;
    public:
    sentence& operator=(const Sentence &);
    };


    sentence& sentence::operator=(Const Sentence &rhs)
    {


    if (this != &rhs)
    {
    try
    {
    aSentence = new char(sizeof(rhs));
    }
    catch(std::bad_alloc& msg)
    {
    cerr << msg;
    }

    delete this->aSentence;
    this->aSentence = rhs;

    return *this;


    }
    }

  5. #20
    Join Date
    Nov 2003
    Posts
    4,118
    what you have defined is copy semantics (inefficient). Move semantics:

    Code:
    sentence& sentence::operator=(Const Sentence &rhs)
    {
    if (this != &rhs)
    {
    //pilfer rhs's resource
     aSentence = rhs.aSentence; 
     asize=rhs.size; //you need to add a size member to your class!
     //clear rhs
     rhs.asentence=0
     rhs.size=0;
    }
    return *this;
    }
    As an exercise, try to implement the copy ctor along these lines.
    Remember: when you move an object, the source becomes empty and the target owns the resources of the source. You don't get two copies!
    Last edited by Danny; 04-26-2008 at 04:27 AM.
    Danny Kalev

  6. #21
    Join Date
    May 2007
    Posts
    843
    Below is what i have try :

    Code:
    #include<iostream>
    #include<iomanip>
    #include<exception>
    #include<cstdlib>
    
    
    #include "MoveSemantics1.h"
    
    
    using namespace std;
    
    // ------------------------------------------------
    int main(int argc, char *argv[])
    {
    	sentence carL;
    	sentence carP;
    
    	carL.setSentence("Lamborghini");
    	cout << carL.getSentence();
    	
    	carP = carL;
    
    
    //	cout << carL.getSentence();
    	cout << carP.getSentence();
    
    	return 0;
    }
    // ------------------------------------------------
    sentence::sentence(const sentence &rhs)
    {
    	this->aSentence = rhs.aSentence;
    	this->size = rhs.size;
    }
    // ------------------------------------------------
    sentence& sentence::operator =(const sentence &rhs)
    {
    	if (this != &rhs)
    	{
    		this->aSentence = rhs.aSentence;
    		this->size = rhs.size;
    
    		delete [] rhs.aSentence;
    	}
    
    	return *this;
    }
    // --------------------------------------------------
    sentence::~sentence()
    {
    	delete [] aSentence;
    	size = 0;
    }
    // --------------------------------------------------
    void sentence::setSentence(const char* userSentence)
    {
    	try
    	{
    		aSentence = new char [strlen(userSentence) + 1];
    	}
    	catch(exception& e)
    	{
    		cout << "Exception Occured " << e.what();
    	}
    
    	strcpy(aSentence, userSentence);
    }
    // ------------------------------------------------
    sentence& sentence::getSentence()
    {
    	return *this;
    }
    // ------------------------------------------------
    ostream& operator<<(ostream& output, const sentence& rhs)
    {
    	output<< rhs.aSentence;
    
    	return output;
    }
    // ------------------------------------------------
    Code:
    #ifndef _MoveSemantics_
    #define _MoveSemantics_
    
    #include<iostream>
    #include<new>
    
    
    using std::bad_alloc;
    
    
    using namespace std;
    
    /*
    	Prototype must be seen compiler in order to
    	be friend
    */
    
    // Forward Declaration
    class sentence;
    
    ostream& operator<<(ostream &, const sentence &);
    
    class sentence
    {
    	private:
    
    		char *aSentence;
    		int size;
    
    	public:
    
    		sentence() : aSentence(0), size(0){}
    		
    		sentence(const sentence &);
    		sentence& operator=(const sentence &);
    
    		void setSentence(const char *);
    		sentence& getSentence();
    
    		friend ostream& operator<<(ostream&, const sentence &);
    		~sentence();
    };
    
    
    
    
    #endif
    Why my second carP just print garbage value since i already copied to it ? I don't feel strange for carL is empty because it just movie its contents to carP.

    I don't know whether my copy constructor is in good direction.

    Thanks for your help.

  7. #22
    Join Date
    Nov 2003
    Posts
    4,118
    First of all, don't confuse delete and delete[].
    Secondly, your copy ctor misses the part that empties the rhs object.
    Danny Kalev

  8. #23
    Join Date
    May 2007
    Posts
    843
    Thanks for your two point. I will remember forever.

    Is it possible declare two types of copy ctor and assignment operator in one class.

    One is copy semantics and another is move semantics.

    How do i explicit call them(with signature) ?

    Why carP printed garbage value since i have copied from carL ?

    carP = carL;


    // cout << carL.getSentence();
    cout << carP.getSentence();

    A billion thank again.

  9. #24
    Join Date
    Nov 2003
    Posts
    4,118
    It's a good question. C++09 will probably have move constructors, not just copy constructors. However, you can't have both a copy ctor and a move constructor in the same class. It's either this or that. A move constructor will look pretty much like a copy constructor except that it takes an rvalue reference:
    C(C&& rhs) //rvalue reference
    {
    myptr=rhs.ptr;
    rhs.ptr=0;
    }

    Alternatively, you can define a move() function.
    Danny Kalev

  10. #25
    Join Date
    May 2007
    Posts
    843
    I will choose second solution which is declare another function called move();

    In post 23, Why carP printed garbage value since i have copied from carL ?

    Thanks again.

  11. #26
    Join Date
    Nov 2003
    Posts
    4,118
    How does your modified code look like? Did you call move instead of assigning the objects?
    Also, remove the delete[] from the assignment operator:
    delete [] rhs.aSentence;

    You can't delete the char array because the other object now owns it! When you move a pointer, don't delete it after assigning it to the target object. Simply set it to 0 in the source object:
    this->aSentence=rhs.aSentence;
    rhs->size= rhs.size;
    rhs.aSentence=0;
    rhs.size=0;
    Danny Kalev

  12. #27
    Join Date
    May 2007
    Posts
    843

    Thumbs up

    I didn't modified my code.

    This is my latest code :

    Code:
    #ifndef _MoveSemantics_
    #define _MoveSemantics_
    
    #include<iostream>
    #include<new>
    
    
    using std::bad_alloc;
    
    
    using namespace std;
    
    /*
    	Prototype must be seen compiler in order to
    	be friend
    */
    
    // Forward Declaration
    class sentence;
    
    ostream& operator<<(ostream &, const sentence &);
    
    class sentence
    {
    	private:
    
    		char *aSentence;
    		int size;
    
    	public:
    
    		sentence() : aSentence(0), size(0){}
    		
    		sentence(const sentence &);
    		sentence& operator=(const sentence &);
    
    		void setSentence(const char *);
    		sentence& getSentence();
    
    		friend ostream& operator<<(ostream&, const sentence &);
    		~sentence();
    };
    
    
    
    
    #endif
    
    
    
    #include<iostream>
    #include<iomanip>
    #include<exception>
    #include<cstdlib>
    
    
    #include "MoveSemantics1.h"
    
    
    using namespace std;
    
    // ------------------------------------------------
    int main(int argc, char *argv[])
    {
    	sentence carL;
    	sentence carP;
    
    	carL.setSentence("Lamborghini");
    	cout << carL.getSentence();
    	
    	carP = carL;
    
    
    //	cout << carL.getSentence();
    	cout << carP.getSentence();
    
    	return 0;
    }
    // ------------------------------------------------
    sentence::sentence(const sentence &rhs)
    {
    	this->aSentence = rhs.aSentence;
    	this->size = rhs.size;
    
    	delete [] rhs.aSentence;
    }
    // ------------------------------------------------
    sentence& sentence::operator =(const sentence &rhs)
    {
    	if (this != &rhs)
    	{
    		this->aSentence = rhs.aSentence;
    		this->size = rhs.size;
    
    		delete [] rhs.aSentence;
    	}
    
    	return *this;
    }
    // --------------------------------------------------
    sentence::~sentence()
    {
    	delete [] aSentence;
    	size = 0;
    }
    // --------------------------------------------------
    void sentence::setSentence(const char* userSentence)
    {
    	try
    	{
    		aSentence = new char [strlen(userSentence) + 1];
    	}
    	catch(exception& e)
    	{
    		cout << "Exception Occured " << e.what();
    	}
    
    	strcpy(aSentence, userSentence);
    }
    // ------------------------------------------------
    sentence& sentence::getSentence()
    {
    	return *this;
    }
    // ------------------------------------------------
    ostream& operator<<(ostream& output, const sentence& rhs)
    {
    	output<< rhs.aSentence;
    
    	return output;
    }
    // ------------------------------------------------

  13. #28
    Join Date
    Nov 2003
    Posts
    4,118
    As I said, your copy constructor and assignment operators shouldn't delete[] the pointer of rhs. They should assign 0 to that pointer *without* deleting it because the pointer is transferred to the new object!
    The deletion should take place only in the destructor.
    Danny Kalev

  14. #29
    Join Date
    May 2007
    Posts
    843
    A billion thanks for your help.

    I very appreciated your help and i hope other did so too.

    Thanks again.

Similar Threads

  1. Pseudocode Problem
    By zobi316 in forum VB Classic
    Replies: 3
    Last Post: 03-10-2008, 07:05 AM
  2. Replies: 0
    Last Post: 11-24-2007, 07:34 AM
  3. Replies: 5
    Last Post: 01-17-2007, 02:37 PM
  4. Sample Sites.
    By Murray Foxcroft in forum Web
    Replies: 5
    Last Post: 11-02-2000, 03:42 AM
  5. run program with error runtime 430
    By mrazek in forum VB Classic
    Replies: 0
    Last Post: 05-22-2000, 08:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center