dyn mem alloc - pointer to pointer problem 
Author Message
 dyn mem alloc - pointer to pointer problem

Hello,

i got a class with a pointer which sould point  to an array of a
structure. Now i like to add an item to the array.
so i wrote a member funktion which lets a pointer to a pointer refer to
the pointer which contains the array.
"Data** pDestData = &pDataArrayOne;" then i delete the array using
"delete [] *DestData;" and finaly allocate it -with the new size-
again:  "*DestData = new Data[*pCount];" but if i try to fill the
allocated array i got an "Access Violation" as soon as the 2. item will
be accessed. i tried to fill the array something like this:

for( i=0 ; i<(pCount); i++ )
{// *pDestData it a pointer to a pointer which sould refer to the array
        *pDestData[i] = pDummyData[i];

Quote:
}

I did not find any hints about this problem at my books, so i hope you
can tell me what i done wrong.
Thank's in advance: -MIKE-

here is an example:

typedef struct MyData
{
    int  SomeData;

Quote:
} Data;

class Dummy
{
private:
   Data NewObject; //New object which sould be added
   Data* pDataArrayOne; //pointer to an Array of  type Data
   int nItemCountOne; // number of items from array pDataArrayOne
   Data* pDataArrayTwo; // pointer to an Array of  type Data
   int nItemCountTwo; // number of items from array pDataArrayTWO
   void AddAnItem(void); // and here is where i like to allocate it

Quote:
}

void Dummy::AddAnItem()
{
  int i;
  Data** pDestData;     // the source array which sould be extend
  Data*  pDummyData;    // to backup the existing array
  int*   pCount;        // contains number of items

 pDestData = &pDataArrayOne; // point to the array which sould be extend
 pCount = &nItemCountOne; // number of items inside pDataArrayOne
 *pCount++; // Increment count to alloc mem for 1 more array item
 pDummyData = new Data[*pCount];// Alloc dummy array to backup source

 for( i=0 ; i<(*pCount-1); i++)      // Backup Sourcearray
 {
   DummyData[i] = *pDestData[i]; // it seems like this is working fine
 }
 DummyData[*pCount-1] = NewObject; // Add the new object to the array

 if((*pDestData)!=NULL)         // Delete sourcearray and ...
     delete [] *DestData;
 *DestData = new Data[*pCount]; // ... allocate it with the new size

 for( i=0 ; i<(pCount); i++ )// copy the items back to "source" array
 {
    *pDestData[i] = pDummyData[i];//Access-Violation exactly here, WHY?
 }

 delete [] pDummyData;          // free the temp. array

Quote:
}

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.


Fri, 01 Mar 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Cast of pointers after mem alloc

2. Help:Dyn.struct alloc

3. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

4. non-local pointers to c(m)alloc'ed arrays

5. Do pointers get alloc

6. COM obj and mem alloc

7. Alloc mem goes away aft ret

8. dynam mem alloc

9. Help: Make a pointer point to Vid mem A000 hex in 32 bit mode (powerpack)

10. Smart pointer and mem leaks

11. huge pointers & large mem model

12. memory leak: pointer->pointer->pointer->struct

 

 
Powered by phpBB® Forum Software