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

C / C++ / MFC

 
GeneralRe: Threads Pin
Albert Holguin7-Oct-11 5:07
professionalAlbert Holguin7-Oct-11 5:07 
QuestionAccess Violation Problem C++ Builder 2006 Pin
Turhan Coskun7-Oct-11 0:33
professionalTurhan Coskun7-Oct-11 0:33 
QuestionBest way to store Array of Dynamic Objects Pin
002comp6-Oct-11 21:17
002comp6-Oct-11 21:17 
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 
yogeshs wrote:
bcoz of not deleting pointer it crashes sometimes

Can you clarify that statement, please? Normally, the only abnormal behaviour you can get from not deleting stuff is memory leaks, and, as a result exceptions of the type std::bad_alloc, once you run out of memory. The former will not crash your program though, so may I assume it is the latter? if so, the first thing to do would be to catch that exception.

Apart from that, there's a couple of things you could improve:

1. Storing a map that uses an consecutive integer value as a key indicates that a simple array would do the job just as well, and probably better. The only exception would be if you need to be able to insert values at the start or in the midst of your set later, and you stored key values to specific objects that you want to be able to retrieve later. You haven't mentioned such a requirement, so my advice is to use a C-style array instead. Or, if you need to be able to later add or remove entries, use a std::vector.

2. The objA variable you use to create each entry does contain only one thing, but that thing is being allocated dynamically, on the heap. As you need to make sure this memory gets properly freed again when you no longer need this object, the best way is to make a simple class to hold that variable, and define it's constructor and destructor to automatically allocate and deallocate the memory, as appropriate. That way you no longer need to take care of that anywhere else in your code. See below for an example.

C++
#include <iostream> // for std::cerr
#include <new> // for the definition of std::bad_alloc
class MyData {
private:
   A* myobj;
public:
   MyData(int size = 100) {
      try { // make sure to catch bad_alloc!
         myobj = new A[size];
      }
      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!
      }
   }
   ~MyData() { //destructor
      if (myobj != 0) { // just added for clarity, this is actually redundant
         delete [] myobj; // be sure to use delete [], not just delete
      }
   }
};

int main() {
   int error = 0;
   MyData *arrayData = 0;
   try { // make sure to catch exceptions!
      arrayData = new MyData[200]; // this will call the constructor MyData::MyData() for each element
   }
   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; // this will also call MyData::~MyData() for all 200 elements!
      arrayData = 0;
   }
   return error;
}


P.S.: As I've seen from a more recent response the first point I've raised may not apply to your problem, i. e. you may need a map after all. My point about creating a class to wrap that 100-A array still holds though.

Moreover, you've used a loop to initialize all your A objects. This is another thing you could improve: use your constructor A::A() to do that! If you didn't define one already, do so now:
C++
class A {
   //...
   A() {
      var = TempVar; // or whatever
   }
   //...
};

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 
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 

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.