Skip to content

Commit daea7c5

Browse files
authored
Merge pull request #16 from cristivlas/fix-c++20-crash
Fix c++20 issue when allocation/deallocating pawnEntry and pieceEntry
2 parents 2251e99 + eb5fe56 commit daea7c5

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/tbprobe.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff 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)
10181035
void 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

0 commit comments

Comments
 (0)