Results 1 to 8 of 8

Thread: [Resolved] Is there a better way?

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    [Resolved] Is there a better way?

    I just spent forever finding a way to do this:

    Code:
    #include <iostream>
    using namespace std;
    
    double *incArray(double *arr);
    
    int main() {
        double *vars;
    
        for (int i = 0; i < 4; i++) {
            vars = incArray(vars);
    
            vars[i] = i;
            cout << vars[i] << endl;
        }
        
        cout << endl << "After: " << endl << endl;
    
        for (i = 0; i < 4; i++) {
            cout << vars[i] << endl;
        }
    
        return 0;
    }
    
    double *incArray(double *arr) {
        
        double *arrtemp = arr;
    
        arr = new double[sizeof(arr)];
    
        for (int i = 0; i < sizeof(arrtemp); i++) {
            arr[i] = arrtemp[i];
        }
    
        return arr;
    
    }
    Is that good or is there a better way?
    Last edited by The Hobo; Nov 12th, 2002 at 12:09 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    What do you want to do? I can't deduct that from the code.
    But I think you're doing something wrong in the incArray function. You're taking the size of an unintialized pointer. That doesn't work here. (Although you can except that it will return the value 4).
    Besides, if you initialize the size of an array with 'new', you should 'delete' it after usage, in order to free the claimed memory.

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I'm just trying to create a dynamic array and write a function to increase its size.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Why not use a vector?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Is this better?

    Code:
    #include <iostream>
    using namespace std;
    
    double *incArray(double *arr);
    
    int main() {
        double *vars;
    
        vars = new double[1];
        vars[0] = 22;
    
        for (int i = 1; i < 5; i++) {
            vars = incArray(vars);
    
            vars[i] = i;
            cout << vars[i] << endl;
        }
        
        cout << endl << "After: " << endl << endl;
    
        for (i = 0; i < 5; i++) {
            cout << vars[i] << endl;
        }
    
        return 0;
    }
    
    double *incArray(double *arr) {
        
        double *arrtemp = arr;
        arr = new double[sizeof(arr)];
    
        for (int i = 0; i < sizeof(arrtemp); i++) {
            arr[i] = arrtemp[i];
        }
        
        delete[] arrtemp;
    
        return arr;
    
    }
    I tried also deleting vars in main(), but it gave me a debug error.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by parksie
    Why not use a vector?
    because I have no idea what vector is.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Search the forums, but basically, it's a dynamic array. It's fast, and very easy to use.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by parksie
    Search the forums, but basically, it's a dynamic array. It's fast, and very easy to use.
    Okay. I'm convinced.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width