Click to See Complete Forum and Search --> : static declaration for Callback Functions in C ?
kkgurpreet
August 10th, 2004, 02:17 AM
[Moderator]: This question was asked in regard to the following FAQ (http://www.codeguru.com/forum/showthread.php?t=231047)...
Hi,
What is the use of a static declaration for Callback Functions in C ?
(:-) Gp.
Andreas Masur
August 10th, 2004, 05:41 PM
Well..the FAQ explicitly deals with member functions used as callbacks functions which need to be declared a 'static' ones for the reason pointed out in the FAQ.
In C however, you do not have member functions (since there are no classes), thus, you simply use global functions instead...and thus....there is no need to add the 'static' keyword...
kkgurpreet
August 12th, 2004, 08:18 AM
I understand. But if we do have a "static" declaration for a callback function implemented in C, is the solution really portable ?
I mean, a "static" function declaration in C would primarily be for data-hiding. So, is it really Ok to give a pointer to such a function ?
Andreas Masur
August 12th, 2004, 08:32 AM
I mean, a "static" function declaration in C would primarily be for data-hiding. So, is it really Ok to give a pointer to such a function ?
Well...the question is rather...why wouldn't I use a global one instead....
usman999_1
August 12th, 2004, 08:54 AM
You don't need to declare it with static keyword for C. It's only needed for C++ member functions so that the compiler doesn't change the function signature (compiler automatically adds the hidden this pointer to the function singature for non-static member functions).
Hope this helps,
Regards,
Usman.
kkgurpreet
August 13th, 2004, 10:43 AM
I gathered as much from the previous discussion. But I have a problem here. We have callback functions declared with a "static" in our project. What I'm wondering is whether they could cause portability issues with different RTOS's.
Our RTOS allows a common heap. So we could refer to the static functions/variables with pointers.
But do all RTOS's allow this ? Or some Memory Managers are strict about such declarations ?
Kheun
August 16th, 2004, 09:05 PM
Declaring a function static in C limits the the function to the file scope. In other word, static function cannot be called from a function in another file. This is almost equivalent to C++'s private member function if you treat your file as a class.
CJ1
August 17th, 2004, 12:00 PM
In C however, you do not have member functions (since there are no classes), thus, you simply use global functions instead...and thus....there is no need to add the 'static' keyword...
In C the "static" modifier will limit the scope of a global function and/or variable to the file in which it appears. When static is used for local variables within a function, the variable is NOT stored on the stack, this means the values are saved from one call to the next (and the function is NOT re-entrent).
For your case, with a static function, you can't link to this function from a function appearing in another file, however you CAN pass a pointer to this function and call it from a function outside the file using this pointer (how you will get this pointer, it must be from a function within your source file or a global variable within the source file).
Think of a static function as being C's version of a private method. I always use static for a function that does not need to be visable outside the source file. As for protability, static is part of the ANSI C standard, so as long as you are using an ANSI C compiler this is not an issue.
Andreas Masur
August 17th, 2004, 12:48 PM
Well...thanks...I actually know what 'static' means...my sentence was simply in regard to the callback function in C being static or not...I usually do not see that much of a reason for it...however, that is not denying the purpose of the 'static' keyword itself...
KevinHall
August 17th, 2004, 12:51 PM
When static is used for local variables within a function, the variable is NOT stored on the stack, this means the values are saved from one call to the next (and the function is NOT re-entrent).
Really? So the following is not valid C code?
#include <stdio.h>
void count_to(int z)
{
static int x;
if (x <= z)
{
printf("%d\n", x++);
count_to(z);
}
}
int main()
{
count_to(5);
count_to(11);
}
It compiles and runs fine -- though I recongnize that not every compiler is standard confomant.
Kheun
August 17th, 2004, 10:17 PM
I believe that CJ1 doesn't means that using static local variable is invaild. CJ1 is stating the a static local variable is actually created somewhere much like a global variable except that it cannot be accessible outside the function. Beside, function that uses static variable, like strtok, is not re-entrant and therefore not thread-safe.
CJ1
August 18th, 2004, 03:38 PM
I understand. But if we do have a "static" declaration for a callback function implemented in C, is the solution really portable ?
I mean, a "static" function declaration in C would primarily be for data-hiding. So, is it really Ok to give a pointer to such a function ?Yes In C think of a static function as only being a non-public (i.e. private) function, which means that you wish to fully control who calls this function. You are only hiding the function from others and are deciding who may use it. Passing a pointer to this function (as in a call back) is in keeping with this idea and is perfectly legal.
I gathered as much from the previous discussion. But I have a problem here. We have callback functions declared with a "static" in our project. What I'm wondering is whether they could cause portability issues with different RTOS's.
Our RTOS allows a common heap. So we could refer to the static functions/variables with pointers.
But do all RTOS's allow this ? Or some Memory Managers are strict about such declarations ?If the compiler meets the ANSI standard, there is no problem, C does not care where the function is located as long as the pointer can access the memory, this not an issue for flat memory, but it could be an issue for segmented memory such as the old 8086 DOS applications where you may need a modifier (such as FAR) for the pointer declaration.
The point I was trying to make with the static qualifier for global variables/functions is that it only changes the scope where the variable/function can be seen. This could be critical if you have two or more global functions/variables that have the same name but are in different files.
The key word static has a different meaning for local variables, and that is a discussion for another day.
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.