Thread: Swapping Pointers & Arrays

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    76

    Swapping Pointers & Arrays

    Hi I seem to be having some trouble with function in my code. The idea is that I have an array and I want to swap the order of the array around. Everytime I run the function though it just comes up with a assert error and closes. It also doesn't do what the function is meant to do.
    Code:
    void TriBlock::Shuffel()
    {
    	// Create temporary blocks to do the sorting
    	Block * tempBlock = itsBlocks;
    	// Chage the position of the temporary blocks
    	tempBlock[0].setItsY((tempBlock[0].getItsY())+32);
    	tempBlock[1].setItsY((tempBlock[1].getItsY())+32);
    	tempBlock[2].setItsY((tempBlock[2].getItsY())-64);
    	// Assert them back into the array in the new order
    	itsBlocks[0] = tempBlock[2];
    	itsBlocks[1] = tempBlock[0];
    	itsBlocks[2] = tempBlock[1];
    	delete[] tempBlock;
    }
    Here is the class definition as well if it helps
    Code:
    class TriBlock
    {
    public:
    
    	// Constructor Destructor
    	TriBlock();
    	~TriBlock();
    
    	// Initalize the actual Block
    	void CreateBlock();
    
    	// Basic Accessors
    	void SetBlock(int blockNo, Block &theBlock);
    	Block getBlock(int blockNo);
    
    	// Other methods
    	void Shuffel();
    	void CollisionCheck();
    	void Move();
    	void Increment();
    	void Draw();
    
    private:
    	Block * itsBlocks;
    };

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I don't think this will fix the assert error but:
    > delete[] tempBlock;

    You shouldn't be deleting tempBlock here.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Alright I deleted that line and the assert error has gone.

    The program is basically a clone of the old sega game columns. Now that function is supposed to switch the colour blocks around but it doesn't it deletes two of them and just keeps the third why??

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    pointers and arrays are related, but are not the same thing.

    private:
    Block * itsBlocks;

    does not create an array of Blocks. itsBlocks is a pointer to a single Block. That block may be an isolated Block or a Block in an array of Blocks, but we can't say from this code alone. To make itsBlocks an array of Blocks you need to use the [] operator somehow. You could either do this:

    private:
    Block itsBlocks[3];

    which uses static memory to hold the array of Blocks or use dynamic memory like this:
    Code:
    TriBlock::TriBlock()
    {
      itsBlocks = new Block[3];
    };
    where itsBlocks is declared as you have done it. (don't forget to release the dynamic memory in the destructor!)

    Second, you can't assign one array to another, ever. Your attempt to do so with this:

    // Create temporary blocks to do the sorting
    Block * tempBlock = itsBlocks;

    really assigns the address of the first Block in itsBlocks to the pointer called tempBlock. tempBlock is NOT an array. It is a pointer to a single Block. The assignment above is legal, it's just not doing what you want it to do. So, you have two choices. You can either use pointer math with tempBlock (that is tempBlock + 1 is the address of the second item in itsBlock and tempBlock + 2 is the address of the third item in itsBlock) or you can change the code such that tempBlock is an array (use one of the two techniques described above), copy the data in itsBlock into tempBlock (use a loop and the assignment operator that has been overloaded for the Block class to do that), and THEN use the [] operator to access the elements of tempBlock using the syntax you have posted.
    Last edited by elad; 10-24-2003 at 12:56 PM.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Oh thanks mate I honestly didnt know that lol. Serves me right for not using arrays unless I really have too lol. I sorted it now. I just changed

    Code:
    Block * tempBlock = itsBlocks;
    To:
    Code:
    	Block *tempBlock = new Block[3];
    	tempBlock[0] = itsBlocks[0];
    	tempBlock[1] = itsBlocks[1];
    	tempBlock[2] = itsBlocks[2];
    Thanks again

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Bartybasher, you probably know this, but just to be sure, the delete is now needed with your new code:
    delete[] tempBlock;

    The reason being you are now allocating memory with new.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Yeah I did lol but thanks for makin sure. The problems that could happen if i hadn't and you didn't remind me. You could of saved me hours of pain many thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to 2D arrays
    By bertazoid in forum C Programming
    Replies: 16
    Last Post: 02-26-2009, 04:46 PM
  2. Pointers or Arrays?
    By magda3227 in forum C Programming
    Replies: 3
    Last Post: 07-09-2008, 09:52 AM
  3. Need Help with pointers and arrays
    By Dan17 in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2005, 04:46 PM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM