Click here to Skip to main content
15,891,889 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Best way to store Array of Dynamic Objects Pin
Richard MacCutchan6-Oct-11 22:34
mveRichard MacCutchan6-Oct-11 22:34 
AnswerRe: Best way to store Array of Dynamic Objects Pin
_AnsHUMAN_ 6-Oct-11 22:47
_AnsHUMAN_ 6-Oct-11 22:47 
GeneralRe: Best way to store Array of Dynamic Objects Pin
002comp6-Oct-11 22:56
002comp6-Oct-11 22:56 
GeneralRe: Best way to store Array of Dynamic Objects Pin
Stefan_Lang6-Oct-11 23:42
Stefan_Lang6-Oct-11 23:42 
AnswerRe: Best way to store Array of Dynamic Objects Pin
Stefan_Lang6-Oct-11 23:06
Stefan_Lang6-Oct-11 23:06 
GeneralRe: Best way to store Array of Dynamic Objects Pin
002comp7-Oct-11 2:29
002comp7-Oct-11 2:29 
GeneralRe: Best way to store Array of Dynamic Objects Pin
002comp10-Oct-11 18:55
002comp10-Oct-11 18:55 
GeneralRe: Best way to store Array of Dynamic Objects Pin
Stefan_Lang10-Oct-11 23:48
Stefan_Lang10-Oct-11 23:48 
I suppose in that case, using a loop will be the most obvious solution. You should still define a constructor for A, but instead of allocating the array, just initialize the array pointer to 0, so you can be sure that pointer is at least valid.

You could still avoid the loop and do the allocation in your constructor, but that will take a bit more effort:

C++
#include <iostream> // for std::cerr
#include <new> // for the definition of std::bad_alloc

// declare an allocator interface
class MyAllocatorHelper {
public:
   virtual void* allocate() const = 0;
}

// declare a global accessor to the allocator interface
class MyArrayAllocator {
public:
   static MyAllocatorHelper const* p;
   static void* makeArray() {
      return p ? p->allocate() : 0;
   }
}

// data array wrapper class
class MyData {
private:
   A* myobj;
public:
   MyData();
   ~MyData() { //destructor
      if (myobj != 0) { // just added for clarity, this is actually redundant
         delete [] myobj; // be sure to use delete [], not just delete
      }
   }
};

// implement the MyData constructor:
MyData::MyData() {
   try { // make sure to catch bad_alloc!
      myobj = MyArrayAllocator::makeArray();
   }
   catch (std::bad__alloc& ba) {
      myobj = 0; // now at least this MyData object is properly initialized
      throw ba; // rethrow - your application should catch this and deal with it appropriately!
   }
}

// declare an actual implementation for that allocator interface
class MyDataArray : public MyAllocatorHelper {
private:
   MyData* the_array;
   std::size_t current_index;
   void* allocate() const; // MyDataArray implements the allocator interface!
public:
   MyDataArray(std::size_t size);
   MyData* get(std::size_t index);
};

// and now the implementation
MyDataArray::MyDataArray(std::size_t size) : the_array(0), current_index(0) {
   try { // make sure to catch exceptions!
      the_array = new MyData[size];
   }
   catch (std::bad_alloc& ba) {
      if (the_array != 0) {
         // error occurred during element allocation
         // the array elelmnts aren't fully usable
         // - you could check the current_index to see at what point the error occurred
         // anyway, clean up to make sure nothing bad happens:
         delete[] the_array;
         the_array = 0;
      }
      throw(ba); // rethrow to main()
   }
}

void* MyDataArray::allocate() const {
   A* result = 0;
   std::size_t alloc_size = 0;
   // now determine the actual size for the array with index current_index
   // ...
   // increment index counter
   ++current_index;
   // allocate array
   try {
      result = new A[alloc_size];
   }
   catch (std::bad_alloc& ba) {
      throw(ba);
   }
   return result;
}

int main() {
   int error = 0;
   MyDataArray* arrayData = 0;
   try { // make sure to catch exceptions!
      arrayData = new MyDataArray(200);
   }
   catch (std::bad_alloc& ba) {
      std::cerr << "Error - bad alloc: " << ba.what() << std::endl;
      error = 1;
   }
   if (arrayData != 0) {
      // do something
      // ...
      // now clean up
      delete arrayData;
      arrayData = 0;
   }
   return error;
}

I wrote this a bit in a hurry and didn't take the time to test it, there may even be typos. However, I hope even if this doesn't compile it might give you an idea of what I did here:

1. An abstract interface for allocating some unspecified data, which later gets implemented by the wrapper to your data array.
2. A 'static class' that your constructors can access without having to rely on parameters.
3. A local counter variable 'current_index' that keeps track of the number of allocations that have already taken place, implying the index of the data object within your array. You can use this counter to determine the correct size for that particular object in your array.
QuestionFunction "WriteAllText" Pin
i52camam6-Oct-11 8:52
i52camam6-Oct-11 8:52 
AnswerRe: Function "WriteAllText" Pin
TheGreatAndPowerfulOz6-Oct-11 8:59
TheGreatAndPowerfulOz6-Oct-11 8:59 
GeneralRe: Function "WriteAllText" Pin
i52camam6-Oct-11 9:06
i52camam6-Oct-11 9:06 
AnswerRe: Function "WriteAllText" Pin
Rajesh R Subramanian6-Oct-11 9:11
professionalRajesh R Subramanian6-Oct-11 9:11 
AnswerRe: Function "WriteAllText" Pin
TheGreatAndPowerfulOz6-Oct-11 9:14
TheGreatAndPowerfulOz6-Oct-11 9:14 
GeneralRe: Function "WriteAllText" Pin
i52camam8-Oct-11 10:07
i52camam8-Oct-11 10:07 
GeneralRe: Function "WriteAllText" Pin
TheGreatAndPowerfulOz10-Oct-11 7:45
TheGreatAndPowerfulOz10-Oct-11 7:45 
QuestionVC++ Excel programing about formatting output??? Pin
Falconapollo6-Oct-11 6:36
Falconapollo6-Oct-11 6:36 
AnswerRe: VC++ Excel programing about formatting output??? Pin
David Crow6-Oct-11 9:34
David Crow6-Oct-11 9:34 
AnswerRe: VC++ Excel programing about formatting output??? Pin
André Kraak6-Oct-11 11:04
André Kraak6-Oct-11 11:04 
Questionc LANGUAGE PROGRAM Pin
Marium Malik6-Oct-11 5:09
Marium Malik6-Oct-11 5:09 
AnswerRe: c LANGUAGE PROGRAM Pin
Erudite_Eric6-Oct-11 5:29
Erudite_Eric6-Oct-11 5:29 
QuestionRe: c LANGUAGE PROGRAM Pin
David Crow6-Oct-11 9:34
David Crow6-Oct-11 9:34 
AnswerRe: c LANGUAGE PROGRAM Pin
Erudite_Eric6-Oct-11 22:13
Erudite_Eric6-Oct-11 22:13 
GeneralRe: c LANGUAGE PROGRAM Pin
Marium Malik19-Oct-11 4:33
Marium Malik19-Oct-11 4:33 
GeneralRe: c LANGUAGE PROGRAM Pin
Erudite_Eric20-Oct-11 7:49
Erudite_Eric20-Oct-11 7:49 
QuestionProblem with overlapping alphablended lines in GDI+ Pin
Code-o-mat6-Oct-11 0:56
Code-o-mat6-Oct-11 0:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.