Hi,

Where does a static function reside? We know that static variables reside in BSS. So does a static function also reside in any particular place? Again is there any classification of functions like there is for data like heap, stack, BSS.

Moreover, is function pointer similar to data pointer? I mean it is difficult to imagine the existence of a pointer to code fragment. What does a function pointer really points to? What kinds of operations are permitted on function pointers?

Thanks.

Recommended Answers

All 2 Replies

Hi,

Where does a static function reside? We know that static variables reside in BSS. So does a static function also reside in any particular place? Again is there any classification of functions like there is for data like heap, stack, BSS.

Moreover, is function pointer similar to data pointer? I mean it is difficult to imagine the existence of a pointer to code fragment. What does a function pointer really points to? What kinds of operations are permitted on function pointers?

Thanks.

Not sure what you mean by BSS, do you mean the bss section in assembler - block started by symbol? Because if you do, then your above statement isn't true.

Where does a static function reside?
The same place a non-static function resides its just not visible outside of the file its created in.

Moreover, is function pointer similar to data pointer?
Yes in the very low-level way they are the same, just an address but when you look at pointer in a high level language then you have differences...example:

In C its very reasonable to do this:

int *x = (unsigned int*)malloc(sizeof(int));

which allocates memory for an integer...

but if we were to try

typedef void(*pfunc)(void);
pfunc tfunc = (pfunc)malloc(sizeof(pfunc));

it would be ridiculous because you can't allocate a function using malloc

So yes we do have differences in function pointers and data pointers in high level languages.

Here is a simple example to show static functions. The qualifier "static" is an instruction to the linker - meaning its only visible to other functions in the same file.

getaddress.c

#include <stdio.h>

typedef void (*pfunc)(void);

static void myfunc(void)
{
	fputs("Hello, World!\n", stdout);
}

pfunc getaddr(void)
{
	return myfunc;
}

getaddress.h

typedef void (*pfunc)(void);

pfunc getaddr();

mainprog.c

#include <stdio.h>
#include <stdlib.h>
#include "getaddress.h"

int main(int argc, char**argv)
{
	pfunc tfunc = getaddr();
	tfunc();
	/*myfunc();*//*won't compile if we remove the comments*/
	exit(EXIT_SUCCESS);
}

gcc -Wall -ansi -pedantic -c getaddress.c
gcc mainprog.c getaddress.o -Wall -ansi -pedantic -o mainprog

Program output:
Hello, World!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.