Advertisement

reallocating memory in C++!!!!!

Started by June 05, 2000 09:30 PM
3 comments, last by BriarLoDeran 24 years, 6 months ago
I'll keep this short and sweet. I'll explain what I am trying to do and (hopefully) someone here has some advice. Sound like a deal? Ok, I want to start off with a pointer. For the sake of simplicity, let's just say:
int *array = NULL;
int count = 0;  
Now, when my program starts, I don't know how many ints I needs, but I will only need more, not less. Say I am initializing array in a function like in the pointless function that follows: (just assume array and count are global)
void Happy(int i)
{
  count ++;
  array = new int[count];
  array[count - 1] = i;
}  
Now, in my code I want something like this. Happy(1); Happy(3); Happy(5); and so forth; Will this work? Is this good coding? I believe that I have tried to use new twice in a row and it works, but does this annoy the compiler or cause memory leaks or something? Am I better off just counting the number of times I plan to call Happy() and just allocate the memory before I start assigning values? Keep in mind that I will be doing this only once at the beginning of the program. Then only accessing the data there after. Based on the nature of what I am trying to do, if I didn't have to allocate at the beginning, but rather as I go, it would be more easier. Also, I assume someone is going to suggest I use link lists or some other technique for this, but I am looking for a strictly non link list answer. Thanks for answering so quickly and accurately , Briar LoDeran Oh, I just thought of something. In the Happy() function, would the following code be more helpful?
void Happy(int i)
{
  if(array)
  {
    delete[] array;
    array = NULL;
  }

  count ++;
  array = new int[count];
  array[count - 1] = i;
}  
I am pretty sure that delele just deletes the pointer, not the actual data, so if I just allocate again I should get back what I had before plus one new int, right? Maybe? Edited by - BriarLoDeran on 6/5/00 9:32:08 PM
void Happy(int i){   count++;   int* newArray = new int[count];   for (int j = 0; j less then count-1; j++)   {      newArray[j] = array[j];   }   newArray[count-1] = i;   delete [] array;   array = newarray;}the way you had it would cause a memory leaknow before your program exits, you need to delete [] array;     


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail


Edited by - ncsu121978 on June 5, 2000 10:43:50 PM
Advertisement
it may have worked once or twice. and that means your getting lucky. this may work but windows does not have to re allocate memory in the same location. try something like this.

void Happy(int i)
{
int *pNewPointer;

pNewPointer = new(int [(count + 1)];
if (pNewPointer != null) {
memcpy(pNewPointer, array, (count * sizeof(int));
pNewPointer[count] = i;
delete(array);
array = pNewPointer;
count++;
}
}

ps: you should try making Happy return a bool (true or false) to make sure the re-allocation worked.

hope this helps.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
delete will just free the memory on the heap, but there is no guarantee (sp?) that the next new operator will fetch the same location in memory.

what you might want to do is allocated memory in blocks
this is a simple dynamic array class that reallocates
memory when it fills up to a specified block size set
a construction

template class CDynArray{  T* m_data;  int m_nSize;  int m_nBlockSize;public:  CDynArray(int nBlocksize = 10) :    m_nSize(0), m_nBlockSize(nBlockSize)    { m_data = new T[nBlockSize]; }  ~CDynArray() { delete[] m_data; }  T& operator[] (int nIndex) { ASSERT(nIndex                               return m_data[nIndex]; }  void Add(T newData)  {    if(!(m_nSize%m_nBlockSize))    {      T* temp = new T[m_nSize + m_nBlockSize];      for(int i=0; i      {  temp = m_data; }<br>      m_nSize += m_nBlockSize;<br>      delete m_data;<br>      m_data = temp;<br>    }<br>    m_data[nSize++] = newData;<br>  }<br>};<br><br> </pre> <br><br>  </i>   
SWEET! That''s what I love about the GameDev community! Post a question and in less than 20 minutes, you''ve got yourself an answer!!! Thanks to all three of you. I should have this little problem well under control now!

-> Briar LoDeran

This topic is closed to new replies.

Advertisement