ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
1). We dereference IR meaning we get the value which IR is pointing to
2). We then get the address of the value
3). We use the address to select the func element
If IR is of type pointer-to-some-struct and func is the name of a pointer to a function, that piece of code is not valid C and will not compile. Provide more context next time before asking any questions. If you're having trouble printing the address of a function pointer that's inside a struct, you should carefully inspect the follwing program and its output (it's somewhat difficult to understand what you want to do from just an invalid code snippet):
Code:
#include <stdio.h>
#include <stdlib.h>
typedef void (* fptr)(void);
static void
myfunc(void)
{
}
struct foo
{
char c; /* Just to make sizeof(struct foo) > sizeof(fptr) */
fptr func;
};
int
main(void)
{
struct foo *ptr = malloc(sizeof(struct foo));
ptr->func = myfunc;
printf("Printing the address of the struct: %#x\n", (unsigned int)ptr);
printf("Print the address of the pointer to the struct: %#x\n",
(unsigned int)&ptr);
printf("Printing the address of the function the function pointer "
"points to: %#x\n", (unsigned int)ptr->func);
printf("Printing the address of the stand-alone function myfunc "
"(will be same as above): %#x\n", (unsigned int)myfunc);
printf("Printing the address of the function pointer (will be the same "
"as the address of the struct if sizeof(struct foo) == "
"sizeof(fptr)): %#x\n", (unsigned int)&(ptr->func));
free(ptr);
return EXIT_SUCCESS;
}
Output:
Code:
$ ./what_this_means.exe
Printing the address of the struct: 0x460160
Print the address of the pointer to the struct: 0x22eec4
Printing the address of the function the function pointer points to: 0x401050
Printing the address of the stand-alone function myfunc (will be same as above): 0x401050
Printing the address of the function pointer (will be the same as the address
of the struct if sizeof(struct foo) == sizeof(fptr)): 0x460164
When a program is compiled the compiler will try and determine the amount of memory that is required for each function. The memory requirements that it can determine beforehand will be placed on the stack. Each time the function is called the required amount of memory is allocated and push'ed on the stack. When the program leaves a function the memory requirements are pop'ed from the stack. This makes the variables that were used in the function inaccessible once the function has finished. So parameters that are required will be copied into variables that are local to the calling function.
Memory that is dynamically allocated can't be predetermined before a function call and these variables may be passed from the function that allocated the memory and used in another function. Typically these are held in a different area of memory to help with the memory management of the program as a whole and hence of the computer. The heap is kept at a different location in memory because it is a much harder beast to manage than the stack.
The basic rule is, if the compiler can determine the amount of memory required to hold a variable then it will store it on the stack, otherwise it will need to resort to the heap.
For example the compiler will know how much memory to allocate when an integer is declared. However memory can be allocated dynamically using functions such as malloc. The malloc will receive an argument that states just how much memory is required. Now because this could be a variable, or it could be in a loop which is executed a variable number of times the compiler is unable to determine how much memory will be required and so it will ensure that at runtime the memory is allocated from the heap and not the stack.
As I understand it global variables are considered to be "local" to the main function and so they reside on the stack of the main function.
This is the case with any extern variable, it will reside on the stack of the function for where they have been declared. The compiler will then make a link from another function that defines the external variable to the offset with the function where the variable actually resides.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.