I'm doing it to learn. thanks. I should've seen that. it was ratherh late. 
Also, Would there be a way to hold the memory while I use new/delete to 
reallocate?
Thanks,
~~TheCreator~~
Visit TDS for quality software and website production
http://tysdomain.com
msn: [EMAIL PROTECTED]
skype: st8amnd127
  ----- Original Message ----- 
  From: Thomas Hruska 
  To: [email protected] 
  Sent: Tuesday, April 29, 2008 10:36 PM
  Subject: Re: [c-prog] help with a bit of code: removing a value from a 
templated container


  Tyler Littlefield wrote:
  > Hello list,
  > I've got a class defined, with a template, to create a sort of container.
  > I'm using a pointer to hold the top element of the container, and with that 
pointer, I can enlarge or shrink the container with new.
  > Now, I've got an add function that works great. What I need to get going, 
is the remove command.
  > My theory is this:
  > If I have 20 elements, and it's 0 based, I can remove say element 5, then I 
would have an empty spot.
  > So, with that empty spot, I could shrink the array, by taking element 6, 
and setting 5 equal to that, 7=6 8=7, etc, until I'm finally left with a blank 
element at the end, which I could dispose of, by resizing the array and 
shrinking it to the number of elements in the code.
  > apparently this isn't working to well.
  > Here's the code; I'm getting an access violation.
  > //code:
  > BOOL Remove(int elem) {
  > 
  > if (elem<0||elem>this->size)
  > 
  > return false;
  > 
  > //now we do the fun thing, and decompress the container, and then delete 1 
element from the bottom.
  > 
  > int i=0;
  > 
  > for(int counter=++elem;counter++;counter<this->size) {

  That line looks seriously problematic. Increment and comparison appear 
  to be swapped...

  > i=counter--;
  > 
  > top[i]=top[counter];
  > 
  > }
  > 
  > --this->size;
  > 
  > this->top=new T[this->size];

  Looks like a memory leak. 'new' allocates, 'delete' frees, there is no 
  "reallocation" - you are only calling 'new' with no 'delete'.

  Looks like an attempt to re-invent the wheel (e.g. std::vector<>, Safe 
  C++ Block). Any particular reason?

  -- 
  Thomas Hruska
  CubicleSoft President
  Ph: 517-803-4197

  *NEW* MyTaskFocus 1.1
  Get on task. Stay on task.

  http://www.CubicleSoft.com/MyTaskFocus/



   

[Non-text portions of this message have been removed]

Reply via email to