Creating New Dynamic Arrays

This is a discussion on Creating New Dynamic Arrays within the C++ Programming forums, part of the General Programming Boards category; I need to create a new array every time i run a method. I need to make a brand new ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    Creating New Dynamic Arrays

    I need to create a new array every time i run a method.


    I need to make a brand new array every time I run the method below.
    Code:
    int Solve::whereAmI(GridSpot finalArray[]){
    
    //then in here I mess with the array then do some recursion
    whereAmI(finalArray);
    
    }
    I did some checking and found out every time I run the method the array uses the same memory location making me think it is actually the same array. I was wondering if anyone could please tell my on how I could make a new dynamic array every time I run the method.

    Thank You
    -Mason

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess you could use a vector instead and not pass it as a reference, to get a copy.

    Otherwise you'd probably have to allocate memory, copy the array, and free the memory afterwards yourself. (Which would be a pain...)

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Like anon said, a vector would be easiest:
    Code:
    #include <vector>
    .
    .
    int Solve::whereAmI(vector<GridSpot> finalArray){
    Arrays are passed by reference. One idea I can think of is, you could create a new array which is local to the function, copy the passed array to the local array, change it, then call the function passing the local array.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by mas0nite
    I did some checking and found out every time I run the method the array uses the same memory location making me think it is actually the same array
    this does not necessarily indicate it is using the "same" array. It's quite common for variables in a stack frame to occupy the same memory location (especially in a debug build).

    what could be happening is that your array is allocated at address 0xDEADBEEF (or whatever), used, freed and then allocated at the same address again next time you create it.

    Plus are you sure you need a "brand new array" each time? what's wrong with reusing old memory (unless you're relying on it not to change).
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    Well I think the main issue is that with the recursion I am using I am taking an array and trying to solve a puzzle. The way I have it set up is that I pass around an array that is a 3x3 grid which is actually just an array of size 9. I go around this grid and try to find a solution and when I swap 2 pieces on my grid that same grid is being messed with by another recursive call at the same time I think. That is what my testing has told me at least if that makes any sense at all.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you don't want to use vectors, then maybe you could do something like this:
    Code:
    int Solve::whereAmI(GridSpot finalArray[]){
    
       Gridspot grid;
       //First copy finalArray to grid
    
       //then in here I mess with the array then do some recursion
       whereAmI(grid);
    
    }

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could also put the array in a struct and pass the struct to the function:
    Code:
    struct GridInfo
    {
       GridSpot array[3];
    }
    .
    .
    int Solve::whereAmI(GridInfo info){

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    Lets say I was to make a new local array each time. Do you think this code would work?

    Code:
    int Solve::whereAmI(GridSpot finalArray[]){
    
       //creating a local array then assigning all the values to it
        GridSpot = localArray[9]
        for(int count=0; count < 9; count++){
               localArray[count]=finalArray[count];
        } 
    
        //then in here I mess with the array then do some recursion
        whereAmI(localArray);
    
    }

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> GridSpot = localArray[9]
    That line isn't quite right, but yes, the idea is correct assuming a GridSpot object can be safely copied.

    A GridSpot can be safely copied if you have defined a copy constructor and copy assignment operator, or if all of the data members are simple datatypes or safely copyable datatypes. For example, if the only member variables of GridSpot are ints, doubles and std::strings, then it is safe to be copied. However, if you have arrays or other pointer variables, it is quite possible that copying is not safe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic arrays
    By s.nelson64 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2008, 06:27 AM
  2. Seg Fault AND 2d dynamic arrays
    By lord in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2008, 02:07 PM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  4. dynamic arrays
    By sokermaniac in forum C++ Programming
    Replies: 34
    Last Post: 05-12-2008, 01:41 PM
  5. Replies: 6
    Last Post: 11-28-2004, 11:01 AM

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