Waitrud Weber’s blog

things and reminders for memories

3d: windows-make: How do we check if the array is allocated or not in C code ?

 How do we check if the array is allocated or not in C code ?

 lines_2D[2] is allocated but nullptr so that we can set *vLine.

 It is going to be segmentation error if we don't check lines_2d[2] as one of arrays.

We had better array_index which ensured the maximum allocated point.

And, we have the max_num as it, so, we changed that check should be skipped.

// 20210311
int convert_model_lines( vLine **lines, vLine **lines_2D, int line_index, int max_num ) {

	printf("int convert_model_lines( vLine **lines, vLine **lines_2D, int *line_index, int max_num ) starts.\r\n");

	// convertion
	for ( int i = line_index; i<max_num; i++ ) {
		//debug 20210311
//		if ( i == 2 ) {
//			printf("lines[%d]\r\n", i);
//			exit(-1);
//		}

		if ( lines != nullptr && lines[i] != nullptr && lines_2D != nullptr && lines_2D[i] != nullptr ) {
			lines_2D[i] = (vLine*) to_screen_line ( lines[i] ) ;
			if ( i == 4 ) {
				printf("i=4 after axes convertion is going to print:\r\n");
				lines[i]->print();
				lines_2D[i]->print();
				//exit(-1);
			}
		} else {
			printf("lines[%d] is not recognised \r\n", i);
			printf("lines[%d] |%p| \r\n", i, lines[i] );
			printf("lines_2D[%d] |%p| nullptr = |%p|\r\n", i, lines_2D[i], nullptr );
			exit(-1);
		}
	}

	printf("int convert_model_lines( vLine **lines, vLine **lines_2D, int *line_index, int max_num ) ends. from %d to %d \r\n", line_index, max_num );
	return 1;
}
--- execution 001 ---
003 x, y = 524.124146 , -113.553345 height 480.000000
get_cooordinate_on_screen ends. 001
get_cooordinate_on_screen ends. 002
vLine* to_screen_line( vLine* ll ) ends.
lines[2] is not recognised
lines[2] |005111D8|
lines_2D[2] |00000000| nullptr = |00000000|
>
--- execution 001 ---

And calculation is well, we recognised. 

--- execution 002 ---
lines[ 38 ] p( -180.000000, 0.000000, 200.000000)
p( -180.000000, 0.000000, 200.000000)
lines_2D[ 38 ] p( 409.851135, 112.669220, 200.000000)
p( 409.851135, 112.669220, 200.000000)
lines[ 39 ] p( -180.000000, 0.000000, 200.000000)
p( -180.000000, 0.000000, 200.000000)
lines_2D[ 39 ] p( 409.851135, 112.669220, 200.000000)
p( 409.851135, 112.669220, 200.000000)
lines[ 40 ] p( -180.000000, 0.000000, 200.000000)
p( -180.000000, 0.000000, 200.000000)
lines_2D[ 40 ] p( 409.851135, 112.669220, 200.000000)
p( 409.851135, 112.669220, 200.000000)
lines[ 41 ] p( -180.000000, 0.000000, 200.000000)
p( -180.000000, 0.000000, 200.000000)
lines_2D[ 41 ] p( 409.851135, 112.669220, 200.000000)
p( 409.851135, 112.669220, 200.000000)
lines[ 42 ] p( -180.000000, 0.000000, 200.000000)
p( -180.000000, 0.000000, 200.000000)
lines_2D[ 42 ] p( 409.851135, 112.669220, 200.000000)
p( 409.851135, 112.669220, 200.000000)
v3dCalculation:: calculation_thread_015 () ends.
main ends.
--- execution 002 ---