• Welcome to Valhalla Legends Archive.
 

Question Regarding Arrays

Started by Dyndrilliac, January 31, 2005, 05:18 PM

Previous topic - Next topic

Adron

#15
std::list works. If you have handles associated with each item and want to access them by handle you might want to use a std::map<handle,item> instead.

Example of deletion by moving things from old array to new array:


item *array;
int size;

void DeleteItem(int index)
{
  item *newarray = 0;
  if(--size) {
    int i, j;
    newarray = new item[size];
    for(i = j = 0; j < size; i++)
      if(i != index) newarray[j++] = array[i];
  }
  delete [] array;
  array = newarray;
}

Mephisto

Quote from: Arta[vL] on February 01, 2005, 10:51 AM
If you write your own, linked lists can take a while to get right.  You might prefer to use STL - std::list is what you want, iirc. I'm not an STL person though so perhaps you should ask someone else (eibro?)

While you are correct, there are certain situations where realloc would be a good thing.  For instance, when inserting data into a buffer that may be of unknown size and you run out of room to insert data, it'd be better IMO to resize the buffer to make room then fail the operation as failing the operation may result in a terminal error (such as failing the Battle.net protocol for a bot).

Zakath

It's definitely worth it to learn how to make a linked list. It's an invaluable construct. I highly advise doing it that way.
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

Adron

Quote from: Mephisto on February 01, 2005, 02:00 PM
While you are correct, there are certain situations where realloc would be a good thing.  For instance, when inserting data into a buffer that may be of unknown size and you run out of room to insert data, it'd be better IMO to resize the buffer to make room then fail the operation as failing the operation may result in a terminal error (such as failing the Battle.net protocol for a bot).

This sounds like jibberish? Did you read what everyone has been saying? Did you understand that calling "realloc" in many cases will result in a new allocation and copying of data, and that that's what we're talking about? Did anyone at any time suggest to just fail because your fixed size buffer is full?

Also, you shouldn't assume that you will be able to resize the buffer to make room. Perhaps you'll be out of memory at that time? If you want to be sure to be able to process the packets you want to process, it would actually be a very good method to preallocate a buffer of enough size to be able to hold the packets you are going to proces, and then not reallocate it at all.

Mephisto

#19
Quote from: Adron on February 01, 2005, 09:10 PM
Quote from: Mephisto on February 01, 2005, 02:00 PM
While you are correct, there are certain situations where realloc would be a good thing.  For instance, when inserting data into a buffer that may be of unknown size and you run out of room to insert data, it'd be better IMO to resize the buffer to make room then fail the operation as failing the operation may result in a terminal error (such as failing the Battle.net protocol for a bot).

This sounds like jibberish? Did you read what everyone has been saying? Did you understand that calling "realloc" in many cases will result in a new allocation and copying of data, and that that's what we're talking about? Did anyone at any time suggest to just fail because your fixed size buffer is full?

Also, you shouldn't assume that you will be able to resize the buffer to make room. Perhaps you'll be out of memory at that time? If you want to be sure to be able to process the packets you want to process, it would actually be a very good method to preallocate a buffer of enough size to be able to hold the packets you are going to proces, and then not reallocate it at all.

Edit: Not even going to bother.

I'm sorry if I implied the wrong thing that upsetted you so much.   ::)

Dyndrilliac

I've decided to use a linked list. Thanks guys.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Mephisto

Quote from: Dyndrilliac on February 04, 2005, 07:21 PM
I've decided to use a linked list. Thanks guys.

If you're going to be using the structure in the manner of an array a vector may be a more viable solution.  Do a search on std::vector.