new fails in this program

I want to create an array of pointers to structure and allocate
the structures one-by-one in a for() loop. This program fails
on or about the 19th iteration with an Access Violation while
executing "particle[p] = new PARTICLE"

The array
PARTICLE *particle[maxParticles];
is local to a procedure and is not static, thus it is created and destroyed every time the procedure is called. However, the procedure uses

if ( theEpoch == 0 ) {
theEpoch++;

to insure that the array is initialized only the first time (ever 2^32 times after that.) That makes no sense. Thus the 2nd time the procedure is called there is a new array with random data in it that does not get initialized. That is the source of the current crash.

Another problem is that you allcoate the PARTICAL objects with new , but never delete them.

You need to take a different approach, but I'm not sure what is best as I'm not sure what your goal is. A quick fix would be to make the array static, but then you would not know when to clean it up and you would have to redisign the code that prevents multiple initializions. There are probably better fixes if you can explain the goal.

Thanks! I'll go with the quick fix of putting "static" in front
of the array definition.

I don't really have a goal other than learning about VC++
and experimenting with things I read on the net and in
magazines. Thanks for the help. I'd love to give some points
to ecw (because he beat you to the answer by 5 mins) but your
second answer was what really helped me.

BTW, is there a way to SEARCH the database of old questions
by keyword?

Strangely enough that does't matter. As far as I can tell, the code is used to randomly set items in the array so the initial value of "t" should not matter.

>>Thanks! I'll go with the quick fix of putting "static" in front
>> of the array definition.
That leaves you with a memory leak. I have no idea how you will fix it.

Also you will need to redo the code that controls the initialization of the array. Use a boolean instead of theEpoch to control the initialization. The way it is now you increment TheEpoch each time the procedure is called. Eventually it will rollover and become 0 again and then you will reinitialize the array again. Use a static boolean that is set to false at first and then change it to true when the initialization is done.

I thought statics were always initialized to ZERO,
[K&R, 1st edition, page 37]
so in this case t is zero at the start
and theEpoch is zero at the start so the "then"
part of "if (theEpoch == 0) {" is only executed once and
will NOT rollover since 'theEpoch++' is inside the 'then'
clause. (Although I agree that I should use a boolean.)

't' is only used as the input to 't++ % maxParticles'
so it should not matter if it rolls over.

I know I have a memory leak, but since the only way out of
the 'while (TRUE) {' loop is to exit the program, it should
not be a big deal, but couldn't I use:

if (some_condition)
for all particle[p]
delete particle[p];

You guys are really great! I really like this web community!
Thanks again!

>> I thought statics were always initialized to ZERO,
Yes, for the built-in types unless explicily initialized to something else.

>> part of "if (theEpoch == 0) {" is only executed once and
>> will NOT rollover since 'theEpoch++' is inside the 'then'
>> clause. (Although I agree that I should use a boolean.)

Yes, my mistake.

>>'t' is only used as the input to 't++ % maxParticles'
>> so it should not matter if it rolls over.

correct.

>> know I have a memory leak, but since the only way out of
>> the 'while (TRUE) {' loop is to exit the program, it should
>> not be a big deal,

I'm not sure what the while loop has to do with anything. Unless your points is that the memory will always be needed while the program is running. However it is a bad practice to not delete all remaining allocated items when the program terminates. That is a way around some of the safegaurds C++ puts in to try to help you write safer code.

>> but couldn't I use:
>>
>> if (some_condition)
>> for all particle[p]
>> delete particle[p];

Yes, but that would have to be done inside of DrawSomething(). You would have to pass some sort of semaphore to DrawSomething() to indicate that it should deleted instead of draw. Doesn't that seem a little weird?. Better to use a global array and have seperate functions for initializing, drawing, and deleting.

Written by John Humphreys
C++ Threading and the POSIX Library
This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.
[s…

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples. You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…

The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.