allocation of array object

Hallo everyone.
i just wanted to ask if it is possible to create following allocation.

static JobOptions joblist [100];

for (int i = 0; i< 100i++){
delete joblist[i];
joblist[i] = new JobOptions(some parameter);
}

Here JopOptions is a Class an joblist is an object of that class.
did i writte correctly?
And it is enough to dellacated a memory so.
thank ofr your answer.
Almost. Each element is a JobOptions pointer, and you want an array of 100 of those.

If we use a typedef to break up the declarations, you get:
1
2
typedef JobOptions* PJobOptions;
static PJobOptions joblist[100];
When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor).

normally delete is used if the objects which are created using new operator.

be carefull with using delete on static objects, im not clear on this.

one can omit for-loop if:
1
2
JobOptions * joblist = new JobOptions [100];
delete [] joblist ;
what does typedef mean?
can you give em further information?
A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the type-declaration portion of the declaration.

typedef type-declaration synonym;

You can use typedef declarations to construct shorter or more meaningful names for types already defined by the language or for types that you have declared. Typedef names allow you to encapsulate implementation details that may change.

In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types.

Ref: this is from MSDN
Last edited on
well it would like to give you more information about my program.namely about a class JopOption.

a constructor looks like this:
JobOptions::JobOptions(int jobnumber, CString *model, CString *filename )
{
this->jobnumber = jobnumber;

this->model = new CString(model->GetString());
this->filename = new CString(filename->GetString());
}

and a desctructor looks like this:
JobOptions::~JobOptions(){
delete this->model;
this->model=NULL;
delete this->filename;
this->filename=NULL;
}
hallo.
i also wanted to add additional information.
for the above mentioned programm. how could i dellocated this array when i use follow.
first it initialize a:
for (int i = 0; i< 100; i++){
joblist[i] = new JobOptions(some parameter);
}

while (true){
for (int i = 0; i< 100; i++){
Sleep(200);
if ((run == true) && testnumber< number ) {
AfxBeginThread(VTKAppThread,(LPVOID)joblist[i]);
}
}
}


my question is: How can i delocate the object "joblist" inside the while-loop?
You can't "deallocate" joblist because you never "allocated" it in the first place. joblist is a static array, and so its address is fixed when the program starts and never changes.

Becuase joblist is static, it is initialsed to zeros. So all the pointers start of as null, which is exactly want you want.

If you want to delete the content of joblist, just go through it and delete each entry. Remember to set the pointer back to zero to indicate that you no longer have an an object at that location.
1
2
3
4
5
6
7
8
void ClearJoblist()
{
    for (int i = 0; i < 100; ++i)
    {
        delete joblist[i];
        joblist[i] = 0;
    }
}
thanks kbw.
i forgot to post where i allocated joblist.
i did follow:

JobOptions **joblist;

joblist = new JobOptions* [100];
for (int i = 0; i< 100; i++){
joblist[i] = new JobOptions(some parameter);
}



while (true){


for (int i = 0; i< 100; i++){
Sleep(200);
if ((run == true) && testnumber< number ) {
AfxBeginThread(VTKAppThread,(LPVOID)joblist[i]);
}
}
}

how can i dellocated that. i means inside true-loop.
Because i don´t want my program to be stopped.i less i closed it.

If you want to clear it and release it:
1
2
3
4
5
6
7
8
9
10
11
void DeleteJoblist()
{
    for (int i = 0; i < 100; ++i)
    {
        delete joblist[i];
        joblist[i] = 0;
    }

    delete joblist;
    joblist = 0;
}
thank but how should i clear and release it onto my true-loop?
i need my program to work definitly until i close it myself.
Do you have a thread that creates job object in joblist and another thread that loops and hands off jobs with AfxBeginThread(VTKAppThread,(LPVOID)joblist[i]);?
Last edited on
i just have one thread for all.
a thread is called threadjob.
VTKAppThread is a thread to hands jobslist.

UINT threadjob(LPVOID pParam)
{
JobOptions **joblist;

joblist = new JobOptions* [100];
for (int i = 0; i< 100; i++){
joblist[i] = new JobOptions(some parameter);
}



while (true){


for (int i = 0; i< 100; i++){
Sleep(200);
if ((run == true) && testnumber< number ) {
AfxBeginThread(VTKAppThread,(LPVOID)joblist[i]);
}
}
}

return true;
}
Last edited on
If it's all one thread, how do you run the loop and still manage to add jobs all in the same thread?

Given that you're starting the thread using AfxBeginThread, then I take it you're running an MFC Application. I would expect the dispatch to run off a WM_TIMER message, and not a fixed loop.

At any rate, it looks like you should have a queue, not an array as you want one thing to add jobs to one end and take jobs off and dispatch them at the other.

Am I understanding you correctly?
Last edited on
that´s right!
But i didn´t find other way to do that.

it normaly worked corectly. but the only problem was that my program crash sometime due to memory allocation and i can´t find out exactly where it happened.
Maybe it is due to my syntax.
anyway i used if ((run == true) && testnumber< number )
just to check if an thread finished. otherwise the it new joblist can´t be threaded.
in the VTKAppThread function i first incremented testnumber and drecremment it when it finished.
only like that i would be sure that a thread is finished

can you try to post me your solution. I mean how shcould you fix a problem or give post me your code anyway.
Last edited on
In the method I was thinking about, joblist would be a queue of jobs rather than an array.
1
2
3
#include <queue>

std::queue<JobOptions*> joblist;


Your app may want to respond to a form entry or something; in either case, it adds a job to the joblist with:
joblist.push(new JobObject(...));

For the job submit part, you need a to handle WM_TIMER, the wizard will create OnTimer for you. You check if the queue is empty. If it isn't you submit all the jobs and remove them from the queue with:
1
2
3
4
5
while (!joblist.empty())
{
    AfxBeginThread(VTKAppThread, joblist.front());
    joblist.pop();
}


You start the timer with SetTimer and stop it with KillTimer().

Once you've got it working, you'll need to add some kind of concurrency control to stop the queue being accessed at the same time by both methods.

The thread functions should delete the job when it's finished.
thanks a lot kbw.
I am trying now to implement it.

hopefully i would work.

i would post you soon a feedback.
Topic archived. No new replies allowed.