this-->array(pointer)

This is a discussion on this-->array(pointer) within the C++ Programming forums, part of the General Programming Boards category; i have aproblem in operator, can you help me fix it?thanks for you. Code: const int size =10; // MyStack::MyStack()//constructor ...

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

    this-->array(pointer)

    i have aproblem in operator, can you help me fix it?thanks for you.

    Code:
    const int size =10;
    //
    MyStack::MyStack()//constructor
    {	SIZE=size;
    	Arr= new int[size];
    	index=0;
    }
    //
    MyStack::~MyStack(){//detructor
    	delete []Arr;
    }
    //
    int MyStack::Pop(){
    	if(index==0){
    		cout<<"Stack Underflow.\n";
    		//return NULL;
    		exit(1);
    	}
    	index=index -1;
    	return Arr[index];
    }//
    MyStack &MyStack::operator =(const MyStack &mystack){
    	this->Arr=mystack.Arr;
    	this->index=mystack.index;
    	//this->SIZE=mystack.SIZE;
    	return *this;
    	//this->Pop=mystack.Pop;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You've bolded a line, but you do not state why you believe it to be an error.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    Quote Originally Posted by tabstop View Post
    You've bolded a line, but you do not state why you believe it to be an error.

    Code:
    MyStack s,s1;
    
    	s.Push(8);
    	s.Push(6);
    	s.Push(3);
    	s.Push(2);
    	s.Push(9);
    	s.Push(9);
    	s.Push(6);
    	s1=s;
    cout<<s.Pop()<<endl;	
    	cout<<s1.Pop()<<endl;
    	cout<<s1.Pop();
    it will run to random, i need valuein s1.pop(), but it run to random, you can help me?.thanks .
    Last edited by llynx; 10-12-2009 at 09:46 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Looking at the context, Arr is a pointer. You probably want to perform a deep copy (i.e., copy all the elements of the array) rather than just copy pointers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you mean that Pop will all of a sudden print addresses, that's not true.

    The error on the line is that you leak the old memory of the stack and now your two stacks are sharing that internal array.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    Yes right. i need a deep copy, but when it comple.. it output random. can u instructure for me. thanks.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So copy each element of the array over, is what we're saying.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Also, as tabstop has hinted: if the destination array is not large enough to store copies of all the elements of the source array, then you should create a temporary array at least as large as the source array, copy over the elements of the source array, then destroy the destination array and make the temporary array the new destination array.

    Alternatively and preferably, you use a std::vector or std::deque instead of doing your own manual memory management.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    true. can you example it. thanks

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    Quote Originally Posted by laserlight View Post
    Also, as tabstop has hinted: if the destination array is not large enough to store copies of all the elements of the source array, then you should create a temporary array at least as large as the source array, copy over the elements of the source array, then destroy the destination array and make the temporary array the new destination array.

    Alternatively and preferably, you use a std::vector or std::deque instead of doing your own manual memory management.
    but i study vector yet. if i can use destroy how? create one new array.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by llynx
    but i study vector yet.
    You might want to use proper English so that you can be better understood. In this case, I assume that you meant to say that you have not studied vector yet.

    Quote Originally Posted by llynx
    if i can use destroy how?
    By using delete[].

    Quote Originally Posted by llynx
    create one new array.
    Yes, at least if you need to.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    Good. thanks for laserlight and tabstop

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    but when i used destroy i can used it in function main()?
    MyStack s,s1;

    s.Push(8);
    s.Push(6);
    s.Push(3);
    s.Push(2);
    s.Push(9);
    s.Push(9);
    s.Push(6);

    s1=s;
    cout<<s.Pop()<<endl;
    cout<<s1.Pop()<<endl;
    cout<<s1.Pop();
    s.~MyStack();

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you have a reason to destroy a declared variable, then go ahead. (I strongly doubt that you have such a reason.)

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    beacause this first time i used function destroy, i still dont understand it.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21