File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed
Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -896,8 +896,25 @@ bool tb_init(const char *path)
896896 TB_MaxCardinality = TB_MaxCardinalityDTM = 0 ;
897897
898898 if (!pieceEntry ) {
899+ #if defined(__cplusplus ) && (__cplusplus >= 202002L )
900+ /* Fix crash with -std=c++20 by being consistent with BaseEntry initialization:
901+
902+ #if __cplusplus >= 202002L
903+ atomic<bool> ready[3]{false, false, false};
904+ #else
905+ ...
906+
907+ C++ initialization does not work with malloc.
908+ Also: with new, if the allocation fails we get std::bad_alloc exception,
909+ which is better library behavior than just calling exit.
910+ */
911+
912+ pieceEntry = new PieceEntry [TB_MAX_PIECE ]();
913+ pawnEntry = new PawnEntry [TB_MAX_PAWN ]();
914+ #else
899915 pieceEntry = (struct PieceEntry * )malloc (TB_MAX_PIECE * sizeof (* pieceEntry ));
900916 pawnEntry = (struct PawnEntry * )malloc (TB_MAX_PAWN * sizeof (* pawnEntry ));
917+ #endif
901918 if (!pieceEntry || !pawnEntry ) {
902919 fprintf (stderr , "Out of memory.\n" );
903920 exit (EXIT_FAILURE );
@@ -1018,9 +1035,14 @@ bool tb_init(const char *path)
10181035void tb_free (void )
10191036{
10201037 tb_init ("" );
1038+ #if defined __cplusplus && __cplusplus >= 202002L
1039+ delete [] pieceEntry ;
1040+ delete [] pawnEntry ;
1041+ #else
10211042 free (pieceEntry );
1022- pieceEntry = NULL ;
10231043 free (pawnEntry );
1044+ #endif
1045+ pieceEntry = NULL ;
10241046 pawnEntry = NULL ;
10251047}
10261048
You can’t perform that action at this time.
0 commit comments