Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Dynamically Allocated Arrays

  1. #1
    Member Contributor
    Join Date
    Oct 2002
    Posts
    55

    Dynamically Allocated Arrays

    I'm working on a .ase loader for loading my models... I'm not all that great with C++, but im trying... Anyhow, i have read in an "int index", which is the number of verticies that are going to be read in next, I now need an array of array[index][3], for the x,y,z of each vertex.. Anyway, i dont know how to make it use "index" as a declarer for the size of the array...
    Can anyone help me out on this? If you could, please post an example...
    Thanks in Advance.
    ImpactDNI

  2. #2
    Senior Member Regular Contributor
    Join Date
    May 2002
    Posts
    189

    Re: Dynamically Allocated Arrays

    There are two ways of managing dynamic memory, only one of which I can do effectively, its actually very easy. Look at the example below:

    int x;
    cin >> x;
    char *data;
    data = new char [x];
    for(int i = 0; i<x; i++) {
    data[i] = "A";
    }
    cout << data << endl;
    delete data[];

    The only line I'm not sure of being correct is the last one, being delete data[] but I'm 50% sure thats correct. Look up new and delete, they rule! Isn't that easy though?

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: Dynamically Allocated Arrays

    Not to be picky, but it would actually be

    delete [] data;

    Deiussum
    Software Engineer and OpenGL enthusiast

  4. #4
    Senior Member Regular Contributor
    Join Date
    May 2002
    Posts
    189

    Re: Dynamically Allocated Arrays

    how sure are you about that? don't the [] always come after the variable name? What's the sense in that?

    Please explain, thanks a lot

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: Dynamically Allocated Arrays

    I'm 100% sure of that.

    delete and delete [] are different "operators."

    You're right that the brackets always come after the variable name when defining the array, but the delete [] operator is a special case where they come first.

    Part of the reason they did it that way I think, was because if you have something like so...

    char** ppChArray = new char*[10];
    for (int c=0;c<10;c++) ppChArray[c] = new char[10];

    // now delete
    for (int c=0;c<10;c++)
    {
    delete [] ppChArray[c]; // seems cleaner somehow than delete ppChArray[c][]
    }

    delete [] ppChArray;



    [This message has been edited by Deiussum (edited 11-22-2002).]
    Deiussum
    Software Engineer and OpenGL enthusiast

  6. #6
    Senior Member Regular Contributor
    Join Date
    May 2002
    Posts
    189

    Re: Dynamically Allocated Arrays

    Yah I guess your right. So what do you do when you wanto delete one of the dimensions of the array x[y]? Is it delete [] x[]?

  7. #7
    Member Contributor
    Join Date
    Oct 2002
    Posts
    55

    Re: Dynamically Allocated Arrays

    WOW!!! lol, thats so helpful, i love this board, you guys rock! I've seen the new and delete commands, but never used them... again, this helps a ton, and you guys rule, (im out of school for the weekend) but if i have any more trouble getting this to work, ill be sure to post back!
    Thanks again!
    ImpactDNI

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: Dynamically Allocated Arrays

    Originally posted by 31337:
    Yah I guess your right. So what do you do when you wanto delete one of the dimensions of the array x[y]? Is it delete [] x[]?
    You'd have to know which element you want to delete. It would be

    delete [] x[nIndexToDelete];
    Deiussum
    Software Engineer and OpenGL enthusiast

  9. #9
    Senior Member Regular Contributor
    Join Date
    Oct 2002
    Location
    King George, Virginia
    Posts
    132

    Re: Dynamically Allocated Arrays

    and remember, if you have an array:

    buf[x]

    and you use this:

    delete buf;

    without the [], this will only delete the first entry in the array.

    jebus

  10. #10
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: Dynamically Allocated Arrays

    Using delete on an array allocated with new[] is undefined behaviour. Most compilers I know of will release the whole memory block though, but don't count on it working properly.

Page 1 of 2 12 LastLast

Similar Threads

  1. Dynamically allocated display lists
    By joao flavio in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-16-2012, 07:37 PM
  2. gluNurbsSurfaces & dynamically allocated arrays
    By adamnation in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-02-2004, 11:23 PM
  3. Dynamically Allocated Vertex Arrays
    By ImpactDNI in forum OpenGL: Basic Coding
    Replies: 12
    Last Post: 12-15-2002, 10:25 AM
  4. Dynamically allocated vertex arrays
    By GarlicGL in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-19-2002, 07:09 PM
  5. Dynamically allocated vertex arrays
    By GarlicGL in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 02-19-2002, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean