typecasting - explain
I'm not able to understand what exactly happens when I typecast a
data type from one to other. What I had in mind was, that by typecasting you are making it clear to the compiler how to handle the data. For example, when you get a "void *" from malloc, the reason you typecast it to the required data type, is to make sure later the compiler doesn't complain or throw an error, when doing some pointer arithmetic on it. Am I wrong completely?
If my conception is correct, then I'm having a serious problem in
understanding typecasting of function pointers. First of all, I thought it is meaningless and the compiler won't allow it. However, to my shock I came across a posting today morning on a different newsgroup, claiming that it is in fact supported by the ANSI standard. Is it? If so, what does such a typecasting mean?
_z33
Signature · 1 line
I love TUX; well... that's an understatement :)
hi _z33,
Someone correct me if Iam wrong.the idea behind typecasting is
to tell the compiler how u want to access a particular region in memory.Whether the region has to considered in terms of 4bytes at a time or interms of 1 byte at a time or 2bytes so on.ie.. when you use a variable to access a memory region the datatype of the variable would tell the size of the memory region which has meaningful data for us. When u typecast a variable it means u r trying to increase or decrease the memory region size.Compiler simply helps you do that.It will not bother if you are trying to use the data at that memory location is used as an address or data.Its how we use it.
cheers-
kaushal.
Quoted 15 lines · On Fri, 2005-09-09 at 12:46 -0700, _z33 wrote:
I'm not able to understand what exactly happens when I typecast a
data type from one to other. What I had in mind was, that by typecasting you are making it clear to the compiler how to handle the data. For example, when you get a "void *" from malloc, the reason you typecast it to the required data type, is to make sure later the compiler doesn't complain or throw an error, when doing some pointer arithmetic on it. Am I wrong completely?
If my conception is correct, then I'm having a serious problem in
understanding typecasting of function pointers. First of all, I thought it is meaningless and the compiler won't allow it. However, to my shock I came across a posting today morning on a different newsgroup, claiming that it is in fact supported by the ANSI standard. Is it? If so, what does such a typecasting mean?
_z33
Quoted 9 lines · kaushal
hi
_z33,
Someone correct me if Iam wrong.the idea behind typecasting is
to tell the compiler how u want to access a particular region in memory.Whether the region has to considered in terms of 4bytes at a time or interms of 1 byte at a time or 2bytes so on.ie.. when you use a variable to access a memory region the datatype of the variable would tell the size of the memory region which has meaningful data for us. When u typecast a variable it means u r trying to increase or decrease the memory region size.
I'm sorry my wording of the explanation of the typecasting is, I
agree, flawed. But, do tell me more on typecasting function pointers. Does it mean anything different or still the same as typecasting of normal data types?
_z33
Signature · 1 line
I love TUX; well... that's an understatement :)
...
Quoted 2 lines
I'm sorry my wording of the explanation of the typecasting is, I
agree, flawed. But, do tell me more on typecasting function pointers. Does it mean anything different or still the same as typecasting of normal data types?
Functions are only "jump" labels. Thou you cant expect a program to work if you cross typecast functions with different argument (or return) values. This because the function expects arguments to be in "right" register or stack position. And caller expects return values to be in right register.
// Jarmo
Quoted 4 lines · Jarmo
Functions are only "jump" labels. Thou you cant expect a program to work if you cross typecast functions with different argument (or return) values. This because the function expects arguments to be in "right" register or stack position. And caller expects return values to be in right register.
// Jarmo
excerpt from a discussion here @
...
ANSI Classic section 3.3.4, page 47 lines 4 to 5: "A pointer to a
function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer."
...
Seems to confuse me a lot. I didn't understand the ABC's of the
quoted discussion; nevertheless, this line there in the discussion, still stings me :(
What does it mean?
First of all, why should be function pointers be typecasted to
something else?
_z33
Signature · 1 line
I love TUX; well... that's an understatement :)
Quoted 18 lines · _z33 wrote:
Quoted 7 lines
Functions are only "jump" labels. Thou you cant expect a program to work if you cross typecast functions with different argument (or return) values. This because the function expects arguments to be in "right" register or stack position. And caller expects return values to be in right register.
// Jarmo
excerpt from a discussion here @
...
ANSI Classic section 3.3.4, page 47 lines 4 to 5: "A pointer to a
function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer."
...
Quoted 4 lines
Seems to confuse me a lot. I didn't understand the ABC's of the
quoted discussion; nevertheless, this line there in the discussion, still stings me :(
What does it mean?
All it really means is that all function pointers are the same size regardless of their type.
Quoted 2 lines
First of all, why should be function pointers be typecasted to
something else?
Storage. You might want to store pointers to different types of functions in a common variable or structure field.
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 7 lines · On 9/9/05, _z33 <[email protected]> wrote:
I'm not able to understand what exactly happens when I typecast a
data type from one to other. What I had in mind was, that by typecasting you are making it clear to the compiler how to handle the data. For example, when you get a "void *" from malloc, the reason you typecast it to the required data type, is to make sure later the compiler doesn't complain or throw an error, when doing some pointer arithmetic on it. Am I wrong completely?
Casting in C means to change the interpretation of the bit pattern found at the memory location denoted by variable x, which can also be a pointer variable of course. Therefore it is possible to cast a pointer to a struct to another struct pointer of a different type and access its fields. Returned is what is found at the fields offset of the structure:
(struct s1 *)s2)->myint = 1;
When casting the pointer returned from malloc to the desired type, you specify what this particular space in memory is used for. If you make some room for strings
char *s = (char *)malloc(128 * sizeof(char));
you indicate that the bytes found at memory location s are interpreted as characters and not as ints. But it's fairly leagal to write
int *i = (int *)malloc(128 * sizeof(int));
then assigning i a string is OK as shown in this short example:
char *s;
int *i;
s = (int *)malloc(128 * sizeof(char));
i = (char *)malloc(128 * sizeof(int));
s = "malloc is cool!";
printf("s: %s\n", s);
printf("s: %d\n", s);
i = "malloc is cool!";
printf("i: %s\n", i);
printf("i: %d\n", i);This piece of code prints:
s: malloc is cool! s: 4350328 i: malloc is cool! i: 4350328
You see, casting changes the interpretation of bits when they are read. The assigment of a particular type to a variable of another type destroys the original type information (i.e. loss of precision).
Quoted 6 lines
If my conception is correct, then I'm having a serious problem in
understanding typecasting of function pointers. First of all, I thought it is meaningless and the compiler won't allow it. However, to my shock I came across a posting today morning on a different newsgroup, claiming that it is in fact supported by the ANSI standard. Is it? If so, what does such a typecasting mean?
Yes function pointers are legal. ANSI C99 says:
"J.5.7 Function pointer casts
1 A pointer to an object or to void may be cast to a pointer to a function, allowing data to be invoked as a function (6.5.4).
2 A pointer to a function may be cast to a pointer to an object or to void, allowing a
function to be inspected or modified (for example, by a debugger) (6.5.4)."
A function name is just a pointer to the memory location where the function is found at runtime. It can be queried, modified and cast to other types. It behaves like a variable. Take a look at the sigaction structure:
struct sigaction {
/* SIG_DFL, SIG_IGN or pointer to function */
void (*sa_handler)(int);
... /* some other fields */
};You can define sigaction as follows:
void handler(int signo) {
doneflag = 1;
}
struct sigaction sa;
sa.sa_handler = handler;
...Here, sa_handler is registered as a function to be called when the specified signal occurrs.
Just handle function pointers as simple pointer variables.
Regards
\Steve
--
Steve Graegert <[email protected]>
Software Consultancy {C/C++ && Java && .NET}
Mobile: +49 (176) 21248869
Office: +49 (9131) 7126409
Quoted 15 lines · Steve Graegert
char *s;
int *i;
s = (int *)malloc(128 * sizeof(char));
i = (char *)malloc(128 * sizeof(int));
s = "malloc is cool!";
printf("s: %s\n", s);
printf("s: %d\n", s);
i = "malloc is cool!";
printf("i: %s\n", i);
printf("i: %d\n", i);Quoted 7 lines
This piece of code prints:
s: malloc is cool! s: 4350328 i: malloc is cool! i: 4350328
The illustration was too simple and good, for me to understand. Thanks :)
Quoted 4 lines
You see, casting changes the interpretation of bits when they are read. The assigment of a particular type to a variable of another type destroys the original type information (i.e. loss of precision).
destroys the original type information? Couldn't understand this part.
You are still able to recover the string, regardless of what kind of
datatype you store in. The only disadvantage I that, this obscures the logic of the program.
Quoted 30 lines
Yes function pointers are legal. ANSI C99 says:
"J.5.7 Function pointer casts
1 A pointer to an object or to void may be cast to a pointer to a function, allowing data to be invoked as a function (6.5.4).
2 A pointer to a function may be cast to a pointer to an object or to void, allowing a
function to be inspected or modified (for example, by a debugger) (6.5.4)."A function name is just a pointer to the memory location where the function is found at runtime. It can be queried, modified and cast to other types. It behaves like a variable. Take a look at the sigaction structure:
struct sigaction { /* SIG_DFL, SIG_IGN or pointer to function */ void (*sa_handler)(int); ... /* some other fields */ };You can define sigaction as follows:
void handler(int signo) { doneflag = 1; } struct sigaction sa; sa.sa_handler = handler; ...
Quoted 4 lines
Here,
sa_handleris registered as a function to be called when the specified signal occurrs.Just handle function pointers as simple pointer variables.
Have still one silly question ---
since you say function pointers are similar to simple pointer
variables, and that's why the typecast works, what would the following code mean?
void handler1 (int a) { /* body of handler - 1 */
printf ("HANDLER - 1 \n"); }
void handler2 (int a) { /* body of handler - 2 */
printf ("HANDLER - 2 \n");} struct sigaction sa; sa.sa_handler = (handler2 *) handler1;
Is this possible? if so, what does it mean?
_z33
Signature · 1 line
I love TUX; well... that's an understatement :)
Quoted 32 lines · On 9/9/05, _z33 <[email protected]> wrote:
Quoted 15 lines · Steve Graegert
char *s; int *i; s = (int *)malloc(128 * sizeof(char)); i = (char *)malloc(128 * sizeof(int)); s = "malloc is cool!"; printf("s: %s\n", s); printf("s: %d\n", s); i = "malloc is cool!"; printf("i: %s\n", i); printf("i: %d\n", i);Quoted 7 lines
This piece of code prints:
s: malloc is cool!
s: 4350328
i: malloc is cool!
i: 4350328The illustration was too simple and good, for me to understand. Thanks :)
Quoted 4 lines
You see, casting changes the interpretation of bits when they are read. The assigment of a particular type to a variable of another type destroys the original type information (i.e. loss of precision).
Quoted 4 lines
destroys the original type information? Couldn't understand this part. You are still able to recover the string, regardless of what kind of
datatype you store in. The only disadvantage I that, this obscures the logic of the program.
Didn't mean this specific example, but when casting a float or double to int then you loose some information which can not be restored afterwards.
Quoted 52 lines
Quoted 30 lines
Yes function pointers are legal. ANSI C99 says:
"J.5.7 Function pointer casts
1 A pointer to an object or to void may be cast to a pointer to a function, allowing data to be invoked as a function (6.5.4).
2 A pointer to a function may be cast to a pointer to an object or to void, allowing a
function to be inspected or modified (for example, by a debugger) (6.5.4)."A function name is just a pointer to the memory location where the function is found at runtime. It can be queried, modified and cast to other types. It behaves like a variable. Take a look at the sigaction structure:
struct sigaction { /* SIG_DFL, SIG_IGN or pointer to function */ void (*sa_handler)(int); ... /* some other fields */ };You can define sigaction as follows:
void handler(int signo) { doneflag = 1; } struct sigaction sa; sa.sa_handler = handler; ...Quoted 4 lines
Here,
sa_handleris registered as a function to be called when the specified signal occurrs.Just handle function pointers as simple pointer variables.
Have still one silly question ---
since you say function pointers are similar to simple pointer
variables, and that's why the typecast works, what would the following code mean?void handler1 (int a) {/* body of handler - 1 */ printf ("HANDLER - 1 \n");} void handler2 (int a) {/* body of handler - 2 */ printf ("HANDLER - 2 \n");
Quoted 6 lines
} struct sigaction sa; sa.sa_handler = (handler2 *) handler1;Is this possible? if so, what does it mean?
In this case you are trying to cast to a __name__ rather than a type. If you write
sa.sa_handler = (void *)handler1;
it would be OK, but nothing would happen. Why? Because no typecasting takes place (handler1 is of type void already) and therefore nothing changes. handler1 would be associated to sa_handler.
A final example:
#include <stdio.h>
void func1(int);
void func2(int);
void func1(int i) {
printf("func1: %d\n", i);
}
void func2(int i) {
printf("func2: %d\n", i);
}
int main (void) {
void (*f1)(int) = (void *)func1;
/* the same as above */
void (*f1)(int) = func1;
void (*f2)(int) = func2;
f1(1);
f2(2);
getc(stdin);
return (0);
}Everything fine now?
Regards
\Steve
--
Steve Graegert <[email protected]>
Software Consultancy {C/C++ && Java && .NET}
Mobile: +49 (176) 21248869
Office: +49 (9131) 7126409
Quoted 110 lines · Steve Graegert
Quoted 33 lines · On 9/9/05, _z33 <[email protected]> wrote:
Quoted 14 lines · Steve Graegert
char *s; int *i; s = (int *)malloc(128 * sizeof(char)); i = (char *)malloc(128 * sizeof(int)); s = "malloc is cool!"; printf("s: %s\n", s); printf("s: %d\n", s); i = "malloc is cool!"; printf("i: %s\n", i); printf("i: %d\n", i);Quoted 7 lines
This piece of code prints:
s: malloc is cool!
s: 4350328
i: malloc is cool!
i: 4350328The illustration was too simple and good, for me to understand. Thanks :)
Quoted 4 lines
You see, casting changes the interpretation of bits when they are read. The assigment of a particular type to a variable of another type destroys the original type information (i.e. loss of precision).
Quoted 4 lines
destroys the original type information? Couldn't understand this part. You are still able to recover the string, regardless of what kind of
datatype you store in. The only disadvantage I that, this obscures the logic of the program.Didn't mean this specific example, but when casting a float or double to int then you loose some information which can not be restored afterwards.
Quoted 52 lines
Quoted 30 lines
Yes function pointers are legal. ANSI C99 says:
"J.5.7 Function pointer casts
1 A pointer to an object or to void may be cast to a pointer to a function, allowing data to be invoked as a function (6.5.4).
2 A pointer to a function may be cast to a pointer to an object or to void, allowing a
function to be inspected or modified (for example, by a debugger) (6.5.4)."A function name is just a pointer to the memory location where the function is found at runtime. It can be queried, modified and cast to other types. It behaves like a variable. Take a look at the sigaction structure:
struct sigaction { /* SIG_DFL, SIG_IGN or pointer to function */ void (*sa_handler)(int); ... /* some other fields */ };You can define sigaction as follows:
void handler(int signo) { doneflag = 1; } struct sigaction sa; sa.sa_handler = handler; ...Quoted 4 lines
Here,
sa_handleris registered as a function to be called when the specified signal occurrs.Just handle function pointers as simple pointer variables.
Have still one silly question ---
since you say function pointers are similar to simple pointer
variables, and that's why the typecast works, what would the following code mean?void handler1 (int a) {
/* body of handler - 1 */ printf ("HANDLER - 1 \n");}
void handler2 (int a) {
/* body of handler - 2 */ printf ("HANDLER - 2 \n");Quoted 6 lines
}
struct sigaction sa;
sa.sa_handler = (handler2 *) handler1;Is this possible? if so, what does it mean?
In this case you are trying to cast to a
__name__rather than a type.
Quoted 9 lines
If you write
sa.sa_handler = (void *)handler1;it would be OK, but nothing would happen. Why? Because no typecasting takes place (handler1 is of type void already) and therefore nothing changes. handler1 would be associated to
sa_handler.
hmm.. fine
Quoted 1 line
A final example:
.."final"..
Got scared seeing this :-O
my silly questions are annoying you???
Quoted 27 lines
#include <stdio.h>
void func1(int);
void func2(int);
void func1(int i) {
printf("func1: %d\n", i);
}
void func2(int i) {
printf("func2: %d\n", i);
}
int main (void) {
void (*f1)(int) = (void *)func1;
/* the same as above */
void (*f1)(int) = func1;
void (*f2)(int) = func2;
f1(1);
f2(2);
getc(stdin);
return (0);
}Quoted 1 line
Everything fine now?
yes :)
_z33
Signature · 1 line
I love TUX; well... that's an understatement :)
Quoted 121 lines · On 9/9/05, _z33 <[email protected]> wrote:
Quoted 110 lines · Steve Graegert
Quoted 33 lines · On 9/9/05, _z33 <[email protected]> wrote:
Quoted 14 lines · Steve Graegert
char *s; int *i; s = (int *)malloc(128 * sizeof(char)); i = (char *)malloc(128 * sizeof(int)); s = "malloc is cool!"; printf("s: %s\n", s); printf("s: %d\n", s); i = "malloc is cool!"; printf("i: %s\n", i); printf("i: %d\n", i);Quoted 7 lines
This piece of code prints:
s: malloc is cool!
s: 4350328
i: malloc is cool!
i: 4350328The illustration was too simple and good, for me to understand. Thanks :)
Quoted 4 lines
You see, casting changes the interpretation of bits when they are read. The assigment of a particular type to a variable of another type destroys the original type information (i.e. loss of precision).
Quoted 4 lines
destroys the original type information? Couldn't understand this part. You are still able to recover the string, regardless of what kind of
datatype you store in. The only disadvantage I that, this obscures the logic of the program.Didn't mean this specific example, but when casting a float or double to int then you loose some information which can not be restored afterwards.
Quoted 52 lines
Quoted 30 lines
Yes function pointers are legal. ANSI C99 says:
"J.5.7 Function pointer casts
1 A pointer to an object or to void may be cast to a pointer to a function, allowing data to be invoked as a function (6.5.4).
2 A pointer to a function may be cast to a pointer to an object or to void, allowing a
function to be inspected or modified (for example, by a debugger) (6.5.4)."A function name is just a pointer to the memory location where the function is found at runtime. It can be queried, modified and cast to other types. It behaves like a variable. Take a look at the sigaction structure:
struct sigaction { /* SIG_DFL, SIG_IGN or pointer to function */ void (*sa_handler)(int); ... /* some other fields */ };You can define sigaction as follows:
void handler(int signo) { doneflag = 1; } struct sigaction sa; sa.sa_handler = handler; ...Quoted 4 lines
Here,
sa_handleris registered as a function to be called when the specified signal occurrs.Just handle function pointers as simple pointer variables.
Have still one silly question ---
since you say function pointers are similar to simple pointer
variables, and that's why the typecast works, what would the following code mean?void handler1 (int a) {
/* body of handler - 1 */ printf ("HANDLER - 1 \n");}
void handler2 (int a) {
/* body of handler - 2 */ printf ("HANDLER - 2 \n");Quoted 6 lines
}
struct sigaction sa;
sa.sa_handler = (handler2 *) handler1;Is this possible? if so, what does it mean?
In this case you are trying to cast to a
__name__rather than a type.Quoted 9 lines
If you write
sa.sa_handler = (void *)handler1;it would be OK, but nothing would happen. Why? Because no typecasting takes place (handler1 is of type void already) and therefore nothing changes. handler1 would be associated to
sa_handler.
Quoted 7 lines
hmm.. fine
Quoted 1 line
A final example:
.."final"..
Got scared seeing this :-O
my silly questions are annoying you???
No, of course not. Just hoped that I now can make myself as clear as prossible to answer all your questions with a "final" example. No reason to be scared. No stupid questions to be asked.
Quoted 22 lines
Quoted 22 lines
#include <stdio.h> void func1(int); void func2(int); void func1(int i) { printf("func1: %d\n", i); } void func2(int i) { printf("func2: %d\n", i); } int main (void) { void (*f1)(int) = (void *)func1; /* the same as above */ void (*f1)(int) = func1; void (*f2)(int) = func2; f1(1); f2(2);
Quoted 9 lines
Quoted 6 lines
getc(stdin); return (0); }Everything fine now?
yes :)
Cool then. Don't forget: never stop asking!
Regards
\Steve
--
Steve Graegert <[email protected]>
Software Consultancy {C/C++ && Java && .NET}
Mobile: +49 (176) 21248869
Office: +49 (9131) 7126409
Quoted 8 lines · _z33 wrote:
I'm not able to understand what exactly happens when I typecast a
data type from one to other. What I had in mind was, that by typecasting you are making it clear to the compiler how to handle the data. For example, when you get a "void *" from malloc, the reason you typecast it to the required data type, is to make sure later the compiler doesn't complain or throw an error, when doing some pointer arithmetic on it. Am I wrong completely?
Quoted 6 lines
If my conception is correct, then I'm having a serious problem in
understanding typecasting of function pointers. First of all, I thought it is meaningless and the compiler won't allow it. However, to my shock I came across a posting today morning on a different newsgroup, claiming that it is in fact supported by the ANSI standard. Is it? If so, what does such a typecasting mean?
If you cast numeric values (char, short, int, long, float, double, and similar), the compiler may insert code to actually convert the value.
OTOH, casting a pointer doesn't in itself add any code; it just changes the compiler's interpretation of the pointer. It changes the multiplier used for pointer arithmetic, and the effect of dereferencing a pointer; it also determines the type used for type checking.
For a function pointer, only the last one of the above makes sense. IOW, typecasting a function pointer changes the type used in type checking, and may change what data is passed if you call the function through the pointer.
In general, typecasting function pointers is a bad idea. Or rather, you shouldn't call a function through a pointer which doesn't represent the exact type of the function (casting to a different type for storage is fine, so long as you cast back to the correct type before calling). The reason is that you are making certain assumptions about the format in memory of the function's argument list.
A classic example of this is the last argument to qsort, which is a pointer to a function of type:
int compar(const void *, const void *);
If you want to sort an array of e.g. int, you need to write:
int compare_int(const void *a, const void *b)
{
const int *aa = a;
const int *bb = b;
return a - b;
}If you were to use:
int compare_int(const int *a, const int *b)
then pass compare_int to qsort() (either casting it explicitly or allowing it to be cast implicitly), it will only work so long as void* and int* are passed in exactly the same way. In particular, they have to be the same size, which isn't guaranteed (a void* can be larger than an int* on some architectures).
Signature · 1 line
Glynn Clements <[email protected]>