Hi.
I wonder about the do{}while(0) loop but I don't know what is the adventage of using this code in the esp_gattc_cb() function.
Could anybody explain it?
BLE gatt_client ( do while(0) )
-
- Posts: 9715
- Joined: Thu Nov 26, 2015 4:08 am
Re: BLE gatt_client ( do while(0) )
Is this part of a macro (#define bla)? If so, this is probably what's happening: https://gcc.gnu.org/onlinedocs/cpp/Swal ... colon.html
Re: BLE gatt_client ( do while(0) )
I mean about this code:
Code: Select all
do {
int idx;
for (idx = 0; idx < PROFILE_NUM; idx++) {
if (gattc_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */
gattc_if == gl_profile_tab[idx].gattc_if) {
if (gl_profile_tab[idx].gattc_cb) {
gl_profile_tab[idx].gattc_cb(event, gattc_if, param);
}
}
}
} while (0);
Re: BLE gatt_client ( do while(0) )
Maybe so that they could declare a new variable int idx; vs. declaring it at the top of the function.
That would have also been possible without the do{}while(0).
You could have just started a new section with a curly bracket i.e.
That would have also been possible without the do{}while(0).
You could have just started a new section with a curly bracket i.e.
Code: Select all
void function()
{
blah;
blah;
{
int idx;
for (idx = 0; idx < PROFILE_NUM; idx++) {
blah;
}
}
}
Re: BLE gatt_client ( do while(0) )
OK, and what is the difference between use:
and:
?
Code: Select all
void function() { blah; blah; { int idx; for (idx = 0; idx < PROFILE_NUM; idx++) { blah; } } }
Code: Select all
void function() { int idx; blah; blah; { for (idx = 0; idx < PROFILE_NUM; idx++) { blah; } } }
Re: BLE gatt_client ( do while(0) )
I think this discussion is getting off-topic from the forum ESP32 IDF, but it's interesting...
In the trivial case that you provided, I don't think there is much if any difference in function, performance, or memory usage.
But in some cases you may want to wait to put variables on the stack until they are needed. This is good practice to keep the stack small.
Notice that there is never a time that both big_buffer1 and big_buffer2 are both on the stack.
Note you could have achieved the same by making each section into a function, but for style/readability this explicit inline may be preferred.
In the trivial case that you provided, I don't think there is much if any difference in function, performance, or memory usage.
But in some cases you may want to wait to put variables on the stack until they are needed. This is good practice to keep the stack small.
Code: Select all
void function()
{
blah;
blah;
{
char big_buffer1[255] = {0};
int idx;
for (idx = 0; idx < PROFILE_NUM; idx++) {
blah; // i.e. do something with big_buffer1
}
} // big_buffer1 is released from the stack automatically when execution exits this section
{
char big_buffer2[255] = {0};
int idx;
for (idx = 0; idx < PROFILE_NUM; idx++) {
blah; // i.e. do something with big_buffer2
}
} // big_buffer2 is released from the stack automatically when execution exits this section
}
Note you could have achieved the same by making each section into a function, but for style/readability this explicit inline may be preferred.
-
- Posts: 28
- Joined: Thu Jan 05, 2017 10:46 am
Re: BLE gatt_client ( do while(0) )
Agree with phatpaul.
I tend to keep the variables just in the section which use it. Not only related to stack use, but also make code looks more logical.
But there's another discussion about `{}` and `do {} while {0}`, which is better?
I tend to keep the variables just in the section which use it. Not only related to stack use, but also make code looks more logical.
But there's another discussion about `{}` and `do {} while {0}`, which is better?
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 415 guests