-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Fix Not by reference. But the value was updated. #3279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Not by reference. But the value was updated. #3279
Conversation
retrieveCharacteristics(); | ||
} | ||
log_v("<< getCharacteristics() for service: %s", getUUID().toString().c_str()); | ||
*pCharacteristicMap = m_characteristicMapByHandle; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not by reference?
Now 1.0.3void BLERemoteService::getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic*>* pCharacteristicMap) {
pCharacteristicMap = &m_characteristicMapByHandle;
} Use casestd::map<uint16_t, BLERemoteCharacteristic*> *mapCharacteristics;
pRemoteService->getCharacteristics(mapCharacteristics); mapCharacteristics is not update. Change to referencevoid BLERemoteService::getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic*>* &pCharacteristicMap) {
if (!m_haveCharacteristics) {
retrieveCharacteristics();
}
pCharacteristicMap = &m_characteristicMapByHandle;
} Argument type : pCharacteristicMap -> &pCharacteristicMap I want m_characteristicMapByHandle. |
|
commnetNow the value is copied and another variable is passed. Some countermeasure is required.
This function is difficult. test codeint a = 1;
int* b = NULL;
int c = 0;
// now function
void func_copy( int *d ) {
// d is copy value.(&d != &b)
Serial.printf("func_copy\n");
Serial.printf(" d:%p\n", d);
Serial.printf(" &d:%p\n", &d);
d = &a;
}
// use reference
void func_reference( int* &d ) {
// d is original value.(&d == &b)
Serial.printf("func_reference\n");
Serial.printf(" d:%p\n", d);
Serial.printf(" &d:%p\n", &d);
d = &a;
}
// use pointer
void func_pointer( int *d ) {
// d is original pointer address.(d == &b)
Serial.printf("func_pointer\n");
Serial.printf(" d:%p\n", d);
Serial.printf(" &d:%p\n", &d);
*d = a;
}
void setup() {
Serial.begin(115200);
delay(1000);
// Init
Serial.printf("Init\n");
Serial.printf(" &a:%p\n", &a);
Serial.printf(" &b:%p\n", &b);
Serial.printf(" &c:%p\n", &c);
Serial.printf("=================\n");
// Now(copy value)
b = NULL;
func_copy(b);
Serial.printf(" finish b:%p\n", b);
Serial.printf("=================\n");
// to reference
b = NULL;
func_reference(b);
Serial.printf(" finish b:%p\n", b);
Serial.printf("=================\n");
// to pointer
func_pointer(&c);
Serial.printf(" finish c:%d\n", c);
}
void loop() {
} output
And, warning output
|
No description provided.