Waitrud Weber’s blog

things and reminders for memories

3d: WindowsAPI: Windows-make: How do we face memories problem:

1. Initialize Class-Array:
2. Set nullptr to the Class-Array:
3. If the Class-Array doesn't have enough space, we reallocate it and to No.2.

(*meaning)
lesser:

greater:

other word of scape:

 

 

// Recreated: 20200218
// Recreated: 20200414
// Modified: 20200505
//
//
vPoint* memorizevPoint( float a, float b, float c ) {
	printf("memorizevPoint starts: %f %f %f dummy_vPoint_index %d dummy_vPoint_max %d\r\n", a, b, c, dummy_vPoint_index, dummy_vPoint_max);

	// Mallocation: initialize: 20200414
	if ( dummy_vPoint_max == 0 ) {
		dummy_vPoint_max = 8;
		dummy_vPoint_index = 0;
		dummy_vPoint = (vPoint**) malloc ( sizeof(vPoint*) * dummy_vPoint_max );
		for (int i = 0; i < dummy_vPoint_max; i++)
			dummy_vPoint[i] = nullptr;
	//		dummy_vPoint[i] = new vPoint(0.0f, 0.0f, 0.0f);
	}

	// Rallocation:
	if (dummy_vPoint_index >= dummy_vPoint_max - 1) {
		dummy_vPoint_max = dummy_vPoint_max * 2;
		dummy_vPoint = (vPoint**)realloc(dummy_vPoint, sizeof(vPoint*) * dummy_vPoint_max);
		for (int i = dummy_vPoint_index; i < dummy_vPoint_max; i++)
			dummy_vPoint[i] = nullptr;

	}

	if ( dummy_vPoint_index < dummy_vPoint_max ) {
		dummy_p = dummy_vPoint[dummy_vPoint_index];
		if ( dummy_p != nullptr ) {
			dummy_vPoint_index++;
			printf("aReuse: %d/%d dummy_vPoint=%p\r\n", dummy_vPoint_index -1, dummy_vPoint_max, dummy_vPoint[dummy_vPoint_index - 1]);
			//exit(-1);
			dummy_vPoint[dummy_vPoint_index - 1]->setPoint( a, b, c);
			return dummy_vPoint[dummy_vPoint_index - 1];
		} else {
			// nullptr
			dummy_vPoint[dummy_vPoint_index] = new vPoint( a, b, c );
			dummy_vPoint_index++;
			printf("aNew: %d/%d dummy_vPoint=%p\r\n", dummy_vPoint_index -1, dummy_vPoint_max, dummy_vPoint[dummy_vPoint_index - 1]);
			return dummy_vPoint[dummy_vPoint_index - 1];
		}
	} else {
			printf("dummy_vPoint_index %d dummy_vPoint_max %d\r\n", dummy_vPoint_index, dummy_vPoint_max);
	}

}