Creating and deleting objects

I have a function that is called every 16ms. In it I would like it to simulate aircraft entering and exiting an area of interest. Every aircraft is represented by a class which may enter or leave the area based on certain probability concepts.

To simulate this, I could create [xx] objects of the aircraft class at the start and then set a bool which tells me whether they are active, but this seems messy. [xx] is the max number of aircraft I would expect to be simulated at any given time. Is there a method to create and delete the objects as required instead of putting aside a given amount of memory from the start and using bits here and there? I'd love some keywords to research.
Last edited on
Yes. Look up new and delete.
I know these exist, but don't really understand how to use them. This site gives the following example:

http://new.cplusplus.com/reference/std/new/operator%20new/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// operator new example
#include <iostream>
#include <new>
using namespace std;

struct myclass {myclass() {cout <<"myclass constructed\n";}};

int main () {

   int * p1 = new int;
// same as:
// int * p1 = (int*) operator new (sizeof(int));

   int * p2 = new (nothrow) int;
// same as:
// int * p2 = (int*) operator new (sizeof(int),nothrow);

   myclass * p3 = (myclass*) operator new (sizeof(myclass));
// (!) not the same as:
// myclass * p3 = new myclass;
// (constructor not called by function call, even for non-POD types)

   new (p3) myclass;   // calls constructor
// same as:
// operator new (sizeof(myclass),p3)

   return 0;
}


But this only shows new classes being created during a single iteration. How would one keep track of multiple classes throughout several iterations of a function. I don't want all of these classes to be deleted every iteration, I would like to keep track of them until they leave the area 6000 iterations later.

Example: One iteration I may want to create an object:
1
2
3
4
5
6
class aircraft {...};

void function_tick()
{
    aircraft * Aircraft1 = new sizeof(aircraft);
}


Then next iteration I may want to step the calculations for my previous object and call a new one:
1
2
3
4
5
6
7
class aircraft {...}

void function_tick()
{
    Aircraft1.step();
    aircraft * Aircraft2 = new sizeof(aircraft);
}


If I name my class objects like this, you can see that I'd be limited by how many objects I would be able to handle at any given time and it would get messy. Is there any way to avoid this?
Last edited on
You would have to store the pointers somewhere (probably in a std::vector of something similar). Just make sure to delete them when you are done.
So something like this would be standard practice?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class aircraft {...};

int function_tick()
{
	static aircraft* ActiveAC[100] = {NULL};	//Array of possibly active objects
	static int nb_ActiveAC = 0;	//Number of active objects

	//Step 1: Create a new object
	if (aircraft_enters())
	{
		aircraft* ActiveAC[nb_ActiveAC++] = new sizeof (aircraft);
	}
	
	//Step 2: Process existing objects
	for (int i=0 ; i < nb_ActiveAC ; i++)
		ActiveAC[i].step();
		
	//Step 3: Delete objects as required
	for (int i=0 ; i < nb_ActiveAC ; i++)
	{
		if (ActiveAC[i].DeleteMe())
		{
			delete ActiveAC[i];	//Deleting object
			for (j = i ; j < nb_ActiveAC-1 ; j++)
				ActiveAC[j] = ActiveAC[j+1];	//Shifting newer objects down 
				
			ActiveAC[--nb_ActiveAC] = NULL;	//Decreasing number of active objects
		}
	}		

	return 0;
}

Edit *Actually I think I'll have to read to figure out how to replace the array with a vector, but ignore that for now
Last edited on
Topic archived. No new replies allowed.