Array of pointers not changing out of scope?

This is a discussion on Array of pointers not changing out of scope? within the C++ Programming forums, part of the General Programming Boards category; I have a PiggyBank class that stores "Coins" with the functions DropIn and GrabOut. The functions will store a pointer ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    Array of pointers not changing out of scope?

    I have a PiggyBank class that stores "Coins" with the functions DropIn and GrabOut. The functions will store a pointer to the "Coins" in an growable array of pointers. (coinStorage**) The functions look like this:

    DropIn is passed the array address, size, and a pointer to the coin that is supposed to be "Dropped In".

    Code:
    bool PiggyBank::DropIn(Coin** coinStorage, int& size, Coin* coin) {
    	Coin** temp = 0;
    	temp = new Coin* [size+1];
    	if(!temp)
    		return false;
    
    	for(int x=0; x<size; x++)
    		temp[x] = coinStorage[x];
    	temp[size] = coin;
    	size++;
    
    	if(coinStorage)
    		delete [] coinStorage;
    	coinStorage = temp;
    	return true;
    }
    GrabOut is passed the array address and size, makes a copy of the passed array of pointers with size-1, and returns the Coin* that was "removed" from the array.

    Code:
    Coin* PiggyBank::GrabOut(Coin** coinStorage, int& size) {
    	if(size) {
    		Coin* save = coinStorage[size-1];
    		Coin** temp = new Coin* [size-1];
    
    		for(int x=0; x<size-1; x++)
    			temp[x] = coinStorage[x]; 
    		size--;
    
    		if(coinStorage)
    			delete [] coinStorage;
    		coinStorage = temp;
    		return save;
    	}
    	else {
    		cerr << "GrabOut(): No coins to grab!\n";
    		return 0;
    	}
    }
    My problem is that it appears as though the functions are not actually changing coinStorage "out of scope", because when I call DropIn() twice, on the second call, I assumed that the value from the first call should be in coinStorage[0], but it is not. What am I missing here?

    One solution was for me to modify DropIn() to return temp** instead of a bool, and have the storage = the returned ** outside of the function.. but I'm still curious why my original method didn't work.

    I do realize I could use std::vector, but this is an exercise, and we are supposed to create our own growable array.

    Thanks in advance for any help!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Quote Originally Posted by LaffyTaffy View Post
    I have a PiggyBank class that stores "Coins" with the functions DropIn and GrabOut. The functions will store a pointer to the "Coins" in an growable array of pointers. (coinStorage**) The functions look like this:

    DropIn is passed the array address, size, and a pointer to the coin that is supposed to be "Dropped In".

    Code:
    bool PiggyBank::DropIn(Coin** coinStorage, int& size, Coin* coin) {
        Coin** temp = 0;
        temp = new Coin* [size+1];
        if(!temp)
            return false;
    
        for(int x=0; x<size; x++)
            temp[x] = coinStorage[x];
        temp[size] = coin;
        size++;
    
        if(coinStorage)
            delete [] coinStorage;
        coinStorage = temp;
        return true;
    }
    GrabOut is passed the array address and size, makes a copy of the passed array of pointers with size-1, and returns the Coin* that was "removed" from the array.

    Code:
    Coin* PiggyBank::GrabOut(Coin** coinStorage, int& size) {
        if(size) {
            Coin* save = coinStorage[size-1];
            Coin** temp = new Coin* [size-1];
    
            for(int x=0; x<size-1; x++)
                temp[x] = coinStorage[x]; 
            size--;
    
            if(coinStorage)
                delete [] coinStorage;
            coinStorage = temp;
            return save;
        }
        else {
            cerr << "GrabOut(): No coins to grab!\n";
            return 0;
        }
    }
    My problem is that it appears as though the functions are not actually changing coinStorage "out of scope", because when I call DropIn() twice, on the second call, I assumed that the value from the first call should be in coinStorage[0], but it is not. What am I missing here?

    One solution was for me to modify DropIn() to return temp** instead of a bool, and have the storage = the returned ** outside of the function.. but I'm still curious why my original method didn't work.

    I do realize I could use std::vector, but this is an exercise, and we are supposed to create our own growable array.

    Thanks in advance for any help!
    Are you required to supply that exact interface? Because it's an incredibly ass-backwards design, IMO - it violates encapsulation, RAII, and just plain common sense. The sane approach is to simply design your own vector-type class (or some reasonable facsimile thereof), a shared-pointer class, and then move forward from there. If that isn't allowed, well, I'd say that your professor is teaching you some very bad lessons!

    Anyway, the reason that doesn't work is because pointers are passed by value, so you'd need to pass either a Coin*** or a Coin**&.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  3. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 06:13 AM

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