Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Problème de cast

1 réponse
Avatar
JKB
Bonjour à tous,

Je ne suis pas un spécialiste du C++ et je suis devant un problème
de cast que je ne saisi pas bien (donc pas taper).

Considérons un fichier header au format C qui est appelé par un
source C++. Le .h contient :

void analyse_post_mortem();
void *debug_memoire_ajout(size_t taille, const unsigned char *fonction,
unsigned long ligne, const unsigned char *argument);
void debug_memoire_initialisation();
void *debug_memoire_modification(void *pointeur, size_t taille,
const unsigned char *fonction, unsigned long ligne,
const unsigned char *argument);
void debug_memoire_retrait(void *ptr);
void debug_memoire_verification();

#define free(s) debug_memoire_retrait(s)
#ifdef RPLCXX
#define malloc(s) debug_memoire_ajout(s, __FUNCTION__, __LINE__, \
const_cast<unsigned char *>(#s))
# define realloc(s, t) debug_memoire_modification(s, t, \
__FUNCTION__, __LINE__, \
const_cast<unsigned char *>(#t))
#else
# define malloc(s) debug_memoire_ajout(s, __FUNCTION__, __LINE__, \
(const unsigned char *) #s)
# define realloc(s, t) debug_memoire_modification(s, t, \
__FUNCTION__, __LINE__, (const unsigned char *) #t)
#endif
#define fork() debug_fork(s_etat_processus)

Il s'agit de routine d'instrumentation des allocations mémoire en C
(malloc/free/realloc). Cela compile parfaitement en C (gcc -Wextra)
et cela fonctionne.

Dans mon programme, j'ai un seul fichier source en C++ pour
interfacer une bibliothèque écrite en C++. Lorsque je compile cela
(g++), j'obtiens un certain nombre d'erreurs :

In file included from interface_cas-conv.cpp:55:
interface_cas-conv.cpp: In function ‘unsigned char* conversion_rpl_vers_cas(struct_processus*, struct_objet**)’:
rpl-conv.h:741:47: error: invalid const_cast from type ‘const char*’ to type ‘unsigned char*’
741 | const_cast<unsigned char *>(#t))
| ^
interface_cas-conv.cpp:247:17: note: in expansion of macro ‘realloc’
247 | realloc(resultat, (strlen(reinterpret_cast<char *>(
| ^~~~~~~
interface_cas-conv.cpp: In function ‘void conversion_cas_vers_rpl(struct_processus*, unsigned char*)’:
rpl-conv.h:738:47: error: invalid const_cast from type ‘const char*’ to type ‘unsigned char*’
738 | const_cast<unsigned char *>(#s))
| ^
interface_cas-conv.cpp:381:29: note: in expansion of macro ‘malloc’
381 | malloc(6 * sizeof(unsigned char))))
| ^~~~~~
interface_cas-conv.cpp: In function ‘void librpl_interface_cas(struct_processus*, t_rplcas_commandes)’:
rpl-conv.h:738:47: error: invalid const_cast from type ‘const char*’ to type ‘unsigned char*’
738 | const_cast<unsigned char *>(#s))
| ^
interface_cas-conv.cpp:596:34: note: in expansion of macro ‘malloc’
596 | (malloc((strlen((const char *)
| ^~~~~~
rpl-conv.h:738:47: error: invalid const_cast from type ‘const char*’ to type ‘unsigned char*’
738 | const_cast<unsigned char *>(#s))
| ^
interface_cas-conv.cpp:632:34: note: in expansion of macro ‘malloc’
632 | (malloc((strlen((const char *)
| ^~~~~~

Et là, je ne comprends pas bien comment corriger le problème. La
fonction C attend un (const unsigned char *) et je ne vois pas mon
erreur.

Toute idée est la bienvenue,

JB

--
Si votre demande me parvient sur carte perforée, je titiouaillerai très
volontiers une réponse...
=> http://grincheux.de-charybde-en-scylla.fr
=> http://loubardes.de-charybde-en-scylla.fr

1 réponse

Avatar
JKB
Le 15 Jan 2020 09:18:52 GMT,
JKB écrivait :
Bonjour à tous,
Je ne suis pas un spécialiste du C++ et je suis devant un problème
de cast que je ne saisi pas bien (donc pas taper).
Considérons un fichier header au format C qui est appelé par un
source C++. Le .h contient :
void analyse_post_mortem();
void *debug_memoire_ajout(size_t taille, const unsigned char *fonction,
unsigned long ligne, const unsigned char *argument);
void debug_memoire_initialisation();
void *debug_memoire_modification(void *pointeur, size_t taille,
const unsigned char *fonction, unsigned long ligne,
const unsigned char *argument);
void debug_memoire_retrait(void *ptr);
void debug_memoire_verification();
#define free(s) debug_memoire_retrait(s)
#ifdef RPLCXX
#define malloc(s) debug_memoire_ajout(s, __FUNCTION__, __LINE__,
const_cast<unsigned char *>(#s))
# define realloc(s, t) debug_memoire_modification(s, t,
__FUNCTION__, __LINE__,
const_cast<unsigned char *>(#t))
#else
# define malloc(s) debug_memoire_ajout(s, __FUNCTION__, __LINE__,
(const unsigned char *) #s)
# define realloc(s, t) debug_memoire_modification(s, t,
__FUNCTION__, __LINE__, (const unsigned char *) #t)
#endif
#define fork() debug_fork(s_etat_processus)
Il s'agit de routine d'instrumentation des allocations mémoire en C
(malloc/free/realloc). Cela compile parfaitement en C (gcc -Wextra)
et cela fonctionne.
Dans mon programme, j'ai un seul fichier source en C++ pour
interfacer une bibliothèque écrite en C++. Lorsque je compile cela
(g++), j'obtiens un certain nombre d'erreurs :
In file included from interface_cas-conv.cpp:55:
interface_cas-conv.cpp: In function ‘unsigned char* conversion_rpl_vers_cas(struct_processus*, struct_objet**)’:
rpl-conv.h:741:47: error: invalid const_cast from type ‘const char*’ to type ‘unsigned char*’
741 | const_cast<unsigned char *>(#t))
| ^
interface_cas-conv.cpp:247:17: note: in expansion of macro ‘realloc’
247 | realloc(resultat, (strlen(reinterpret_cast<char *>(
| ^~~~~~~
interface_cas-conv.cpp: In function ‘void conversion_cas_vers_rpl(struct_processus*, unsigned char*)’:
rpl-conv.h:738:47: error: invalid const_cast from type ‘const char*’ to type ‘unsigned char*’
738 | const_cast<unsigned char *>(#s))
| ^
interface_cas-conv.cpp:381:29: note: in expansion of macro ‘malloc’
381 | malloc(6 * sizeof(unsigned char))))
| ^~~~~~
interface_cas-conv.cpp: In function ‘void librpl_interface_cas(struct_processus*, t_rplcas_commandes)’:
rpl-conv.h:738:47: error: invalid const_cast from type ‘const char*’ to type ‘unsigned char*’
738 | const_cast<unsigned char *>(#s))
| ^
interface_cas-conv.cpp:596:34: note: in expansion of macro ‘malloc’
596 | (malloc((strlen((const char *)
| ^~~~~~
rpl-conv.h:738:47: error: invalid const_cast from type ‘const char*’ to type ‘unsigned char*’
738 | const_cast<unsigned char *>(#s))
| ^
interface_cas-conv.cpp:632:34: note: in expansion of macro ‘malloc’
632 | (malloc((strlen((const char *)
| ^~~~~~
Et là, je ne comprends pas bien comment corriger le problème. La
fonction C attend un (const unsigned char *) et je ne vois pas mon
erreur.
Toute idée est la bienvenue,
JB

Trouvé.
# define malloc(s) debug_memoire_ajout(s,
const_cast<const unsigned char *>
(reinterpret_cast<unsigned char *>
(const_cast<char *>(__FUNCTION__))), __LINE__,
const_cast<const unsigned char *>
(reinterpret_cast<unsigned char *>
(const_cast<char *>(#s))))
On doit pouvoir retirer un const_cast<> mais ça fonctionne comme ça.
JKB
--
Si votre demande me parvient sur carte perforée, je titiouaillerai très
volontiers une réponse...
=> http://grincheux.de-charybde-en-scylla.fr
=> http://loubardes.de-charybde-en-scylla.fr