Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 21994

Is there a compiler warning for casting double pointer to single pointer?

$
0
0

Consider the following code which is a simplified version of something I did IRL (and have done a couple of times) where there is an "extra" address-of operator which should not be there.

#define HEAD \    int typestruct my_struct {    HEAD;};struct my_struct_extended {    HEAD;    int a;    int b;};int my_function(const struct my_struct *data) {    return data->type;}struct my_struct_extended global = { 2, 3, 4 };int main (int argc, char **argv) {    struct my_struct_extended *local = &global;    //        ADDRESS-OF SHOULD NOT BE HERE \/    return my_function((struct my_struct *) &local);}

There are to structs, one base struct and one extended struct. An instance of the extended struct is casted to the base struct to be used in my_function(). Due to the misplaced address-of operator however, the function basically starts working on trash data.

The compiler (in my case GCC) always warns when you pass a double pointer to a function expecting single pointer, but when a struct needs to be casted, such an error is masked. I guess the compiler interprets the cast as "YES! I REALLY WANT THIS" or something.

Is there a warning which can be enabled to trap this?

Note: The real-world case is with casts involving struct sockaddr:

ret = rrr_ip_send (&err,    ip_data->ip_udp.fd,// EXTRA ADDRESS-OF    \/    (struct sockaddr *) &addr,    addr_len,    (void *) send_data,    send_size);

Viewing all articles
Browse latest Browse all 21994

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>