problem with delete

P

ppavlo

Hello all,

I have class of such tye

class Manager {
public:
static Manager* getInstance();
Worker* getWorker(char* name);
private:
Manager(...);
Worker** workers;
int numberOfWorkers;
static Manager* instance;
};

It is sort of singleton implementation. First call to getInstatnce()
calls Manager() constructor which creates dynamically the workers array
of pointers to Worker objects and asignes to numberOfWorkers size of
that array. getWorker() method searches the proper worker by the name
in the workers array, if there is no such worker it looks to specific
file that may have the needed record, then It takes from that file
creates the new Worker object, and returns the worker to user, but I
also want to append this worker to existing array, So I'm creating new
array of pointers bigger than previous, write all pointers from old
array to new, and also that new worker that was created from file's
record. Then I want to delete the old "Worker** workers" pointer. So
when aplying delete to that pointer I'm having Segmentation Fault
error. Can anybody suggest me possible troubles that I'm getting into?
I'll appreciate any answers. If something is not cleare please ask.
 
T

Thomas Jakob

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello all,

I have class of such tye

class Manager {
public:
static Manager* getInstance();
Worker* getWorker(char* name);
private:
Manager(...);
Worker** workers;
int numberOfWorkers;
static Manager* instance;
};

Better use std::string and std::deque or std::list in this case.

So I'm creating new
array of pointers bigger than previous, write all pointers from old
array to new, and also that new worker that was created from file's
record. Then I want to delete the old "Worker** workers" pointer. So
when aplying delete to that pointer

std::deque or std::list would do all this work :)
I'm having Segmentation Fault
error. Can anybody suggest me possible troubles that I'm getting into?
I'll appreciate any answers. If something is not cleare please ask.

Well how do you create this array (Code) and how to you try to resize it
(Code)?

- --
Greetings, Thomas Jakob
quicix (at) gmail (dot) com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)

iD8DBQFDunk6KzGu1S9H/nsRAjMJAJ0UL8kWkve087Eb8FjIAJkm8kQ4dQCfVfxO
4oVhcbpoTxHbtBTQYvkyMSE=
=UAc0
-----END PGP SIGNATURE-----
 
M

Marcelo Pinto

Thomas Jakob wrote:

[snip]
std::deque or std::list would do all this work :)

I believe a std::map<std::string, Worker> or std::map<std::string,
Worker *> would solve it better.

[snip]

HTH,

Marcelo Pinto
 
E

Earl Purple

Marcelo said:
I believe a std::map<std::string, Worker> or std::map<std::string,
Worker *> would solve it better.

HTH,

Marcelo Pinto

If Worker is not a trivially copyable class (i.e. could be a base class
or has pointers etc) then the best solution would be std::map<
std::string, boost::shared_ptr< Worker > >
(or another shared_ptr implementation if you prefer).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,126
Latest member
FastBurnketoIngredients
Top