5

I'm having some problems with g++ and the compiling process for a C/C++ program which use Bluetooth libraries under Ubuntu.

If i use gcc, it works fine with no warning; on the contrary, if i use g++ i get this warning:

warning: taking address of temporary

even if the program compiles fine and it works.

The involved lines reporting the error are:

        bdaddr_t *inquiry(){
       // do some stuff.. 
    bacpy(&result[mote++], BDADDR_ANY);
    return result;
}
//...
void zeemote(){
while (bacmp(bdaddr, BDADDR_ANY)){
/..
}
}

In both the cases, BDADDR_ANY is involved.

How can i solve this warning?

BDADDR_ANY is defined in bluetooth.h like:

/* BD Address */
typedef struct {
    uint8_t b[6];
} __attribute__((packed)) bdaddr_t;

#define BDADDR_ANY   (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})

3 Answers 3

5
(&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})

Constructs a temporary object and uses its address. This isn't allowed in C++.

You can fix this by creating a named temporary variable and using bacpy and bacmp on it:

bdaddr_t tmp = { };

bacpy(&result[mote++], &tmp);

and

while (bacmp(bdaddr, &tmp)) {
    //
}
5
  • How can i fix this? I'm not a c++ expert programmer :( Mar 17, 2012 at 16:37
  • @MarcusBarnet create a named variable and use bacpy on it Mar 17, 2012 at 16:38
  • thanks for the answer! but i have to modify the bluetooth.h file or i have to modify only my program? Mar 17, 2012 at 16:39
  • @MarcusBarnet good job on paying attention to warnings, most people ignore them at their own peril since they don't stop the program from compiling. Mar 17, 2012 at 16:48
  • i'm not an expert programmer, but i'd like to solve all the little "invisible" problems because they can be very dangerous sometimes :) Mar 17, 2012 at 17:06
2

Create a variable (in whatever scope's convenient) rather than a temporary...

bdaddr_t my_bdaddr_any = { 0 };
while (bacmp(bdaddr, my_bdaddr_any))
    ...
0
1

If you look into the bluetooth.h file you will see you can simply do this yourself in C++ by just going

bdaddr_t bdaddr_any =  {{0, 0, 0, 0, 0, 0}};

sockaddr_rc server;
server.rc_family = AF_BLUETOOTH;
server.rc_bdaddr = bdaddr_any;
server.rc_channel = (uint8_t)1;
1
  • Best KISS answer ,
    – user2839574
    Jan 2, 2020 at 17:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.