LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2006, 11:10 PM   #1
MeUmesh
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Rep: Reputation: 0
What this means


Hello
Can any one tell about the meaning of this statement &(*IR)->func;

where IR is the structure pointer & func is the member funtion in the structure in C.
 
Old 01-26-2006, 11:38 PM   #2
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
&(*IR)->func;

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
 
Old 01-27-2006, 07:49 AM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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
Makefile
Code:
CC = gcc
CFLAGS = -Wall -W -ansi -pedantic -g -O0 -c
LDFLAGS = -o $(EXEC)
EXEC = what_this_means.exe
OBJECTS = what_this_means.o

all: $(OBJECTS)
	$(CC) $^ $(LDFLAGS)

%.o: %.c
	$(CC) $(CFLAGS) $<

clean:
	rm -f $(OBJECTS) $(EXEC) *~ *.stackdump
 
Old 01-29-2006, 10:43 PM   #4
MeUmesh
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Original Poster
Rep: Reputation: 0
B/w Heap & Stack

Can any one tell about the difference b/w Heap & stack in c programs.
 
Old 01-29-2006, 11:05 PM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
declaring variables like so goes on the stack
int foo=1;
char x = 'x';
char* str = "hello";
etc....

when you use malloc to allocate memory that goes on the heap..
 
Old 01-30-2006, 07:40 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.

Hope that helps a little.

graeme.
 
Old 01-30-2006, 09:22 AM   #7
MeUmesh
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Original Poster
Rep: Reputation: 0
B/w Stack & heap

Mr.graeme

U mean to say that the variable or location which are to needed untill excution of program are stored in area(heap).

will global variables stored in a heap.
 
Old 01-30-2006, 11:28 AM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.

graeme.
 
Old 01-31-2006, 12:44 AM   #9
MeUmesh
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Mr.graeme.

Thank for your suggestion

Plz can u tell about type-safe Language & type-unsafe Language.
C is called as weak type-safe language.why ?.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can someone please tell me what this means... :) xbill311x Linux - Newbie 7 05-11-2005 06:37 AM
Does anyone know what this means? mrchaos Slackware 8 04-23-2005 01:32 PM
Help what does it means.. jhar Linux - Newbie 1 02-16-2005 01:44 AM
does anyone know what this means ? Infinite_Pizza Linux - General 8 10-14-2003 05:15 AM
Could someone please tell me what this means!!!!! brianm Programming 1 03-27-2001 10:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration