Hello Everyone. I am having trouble creating new objects in a loop.
I am using:

DataType *Ptr = new DataType ();

I use this in the beggining of the loop then end the loop with:

delete Ptr;

Each constructor and destructor gets called each time the loop is executed.... but for some reason the constructor is not seeding correctly. I have the Class constructor to be:

1. DataType:: DataType ()
2. {
3.   srand(time(0));
4.  }

Which is to have a different seed when the constructor is called and give different sets of random numbers. For some reason it seems that every time the constructor is called in the While Loop it generates the same random numbers, which makes me think that my constructor is giving me the same seed.

If anyone can help I can provide the full code if needed. Thanks for any assistance.

Recommended Answers

All 11 Replies

Of course it's giving the same seed. time(0) returns the current second. So, unless each call to srand() if over 1 second apart, the seeds are identical, giving identical random values.

The seeding really only needs to occur once in the entire program (at the start). You don't need and shouldn't re-seed in the constructor of that object. Just call the seed once at the start of the main() function, and that's it. Try it, it will work as you want it to.

Also: Why are you writing

DataType *Ptr = new DataType();

instead of simply writing

DataType Obj;

?

commented: Good point! And welcome back! +13

@ arkoenig

I know it makes sence to initialize the object by:

DataType Obj;

Unfortunately this means that I would have to know how many objects I need in order to initialize them all in the code. The purpose is to create a brand new object each time the loop is executed for as long as the loop runs. Once the loop terminates the objects will be destroyed.

That's the whole point. In C++, local variables (objects) get created at the point where they are declared (i.e., the DataType Obj; statement), and get destroyed at the end of the first enclosing scope (i.e., at the first closing curly-braces). As in this:

while(/* .. some condition .. */) {
  // ... some code..
  DataType Obj;  // the object 'Obj' gets created here.
  // .. .some more code..
}; // the object 'Obj' get destroyed here, before starting the next iteration.

From what I understand of your problem, it seems like the above is a lot more appropriate and easier than doing a new/delete allocation at the beginning and end of the while loop iteration.

Mike, he said:

The purpose is to create a brand new object each time the loop is executed for as long as the loop runs.

so he wants

while(/* .. some condition .. */) {
  // ... some code..
  create a DataType Obj;  // another object 'Obj' gets created here.
  // .. .some more code..
}; 
delete all objects

so if the loop runs 6 times, 6 objects get created. 20 times, 20 objects.

@WaltP:
I think he just mispoke on that sentence, if you read all the posts, especially his original description:

I am using:

DataType *Ptr = new DataType ();

I use this in the beggining of the loop then end the loop with:

delete Ptr;

Each constructor and destructor gets called each time the loop is executed....

I believe it is pretty clear that he is currently doing something like this (and that's how arkoenig interpreted it too):

while(/*...*/) {
  DataType *Ptr = new DataType ();
  // ...
  delete Ptr;
};

He is only making a poor choice of words when describing it. When he says loop, he really means iteration. That's a very common linguistic slip that I'm used to hearing all the time. When he says "in the beginning and end of the loop", there is no way that this can mean the entire loop, he must be meaning to say "in the beginning and end of every iteration of the loop". Don't expect a novice (possibly, second-language writer) to be so rigorous about the words he uses, I think the meaning was obvious here, especially knowing that this particular wrong choice of word is extremely common.

Each constructor and destructor gets called each time the loop is executed...

If this is what you want, arkoenig has already given you the solution.

The purpose is to create a brand new object each time the loop is executed for as long as the loop runs. Once the loop terminates the objects will be destroyed.

If this is really what you need to do, use a std::vector<>

{
    std::vector<DataType> objects ;
    while( whatever ) // each time through the loop
    {
        objects.emplace_back( /*...*/ ) ; // construct a new object in situ
        objects.back().do_something() ; // use the object
        // ...
    }
}
// all the objects would have been destroyed by this tine

Hello Everyone!

Thanks for your help. I apologize for my poor use of words... it is in fact creating an object on each iteration. :) also I am a she not a he ;) not that it matters anyway... I hope. :)

I fixed the issue by removing the seeding from the constructor... since it was giving the exact same seed each time it initiated the object in the iteration. Instead I placed:

srand(time(0));

Whithin main, and I only execute it once when the program starts, not in the actual loop. My only issue now is that for some reason it is not respecting the parameter I set for the numbers ex:

......
{
return (3 + rand() % 14);
}

when I execute the program it gives me random numbers but it has given me anything from 3- 16.... instead of 3-14. I apologize if I am not too clear... I do appreaciate all the help I have gotten so far, my program is now working but if I can get that small issue corrected it will be exceptional!

thanks!

when I execute the program it gives me random numbers but it has given me anything from 3- 16.... instead of 3-14.

Then, you should use:

return (3 + rand() % 12);

Thanks for your help. I apologize for my poor use of words... it is in fact creating an object on each iteration. :) also I am a she not a he ;) not that it matters anyway... I hope. :)

Glad to help! Sorry for assume you were a "he"... and no it doesn't matter that you're a lady ;) If anything, you'll get more gallantry ;)

Thanks!!!

The program is now running. By calling the seed once from main... and fixing the random number shifting statementto:

return (3 + rand() % 12);

although I dont understand why it didnt work how I originally had it... but with the above statement the program is now giving me random numbers from 3-14, which is what I needed. Thank You so Much everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.