473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

explain the result

Hi

Help me to understand the program:
func()
{
}

main()
{
fprintf(stderr, "sizeof(func):%d\n", sizeof(func));
}

output: sizeof(func):1

I thought that it would return 4 bytes. since func is a symbol which
is pointing to the relative address of the function. But it returns 1
byte. Please help me to understand the program.

Thanks
Param

Mar 25 '07 #1
16 1357
param wrote:
Hi

Help me to understand the program:
func()
{
}

main()
{
fprintf(stderr, "sizeof(func):%d\n", sizeof(func));
}

output: sizeof(func):1

I thought that it would return 4 bytes. since func is a symbol which
is pointing to the relative address of the function. But it returns 1
byte. Please help me to understand the program.
Well, I'm surprised you managed to get this compiled at all. Is stderr
automatically defined on your implementation (in which case it's
broken), or did you just not copy the exact code as you compiled it?

That said, you can't apply sizeof to a function type. If you do
anyway, and the compiler accepts it, there's no guarantee whatsoever
about what'll happen.

Mar 25 '07 #2

"Harald van D?k" <tr*****@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
>I thought that it would return 4 bytes. since func is a symbol which
is pointing to the relative address of the function. But it returns 1
byte. Please help me to understand the program.

Well, I'm surprised you managed to get this compiled at all. Is stderr
automatically defined on your implementation (in which case it's
broken), or did you just not copy the exact code as you compiled it?
broken is a bit strong word imo, it doesnt seems so bad if a compiler has
knowledge of standard library names, although i do understand that it does
make it possible to write code that wont work on other complaiant compilers.
Mar 25 '07 #3
On Mar 25, 3:02 pm, "param" <sparamesw...@gmail.comwrote:
Help me to understand the program:
func()
{

}

main()
{
fprintf(stderr, "sizeof(func):%d\n", sizeof(func));

}

output: sizeof(func):1

I thought that it would return 4 bytes. since func is a symbol which
is pointing to the relative address of the function. But it returns 1
byte. Please help me to understand the program.
func is not a function pointer. Consider:

[tmp]$ gcc -Wall -pedantic a.c
a.c: In function `main':
a.c:14: warning: invalid application of `sizeof' to a function type
a.c:15: warning: invalid application of `sizeof' to a void type
[tmp]$ cat a.c
#include <stdio.h>

void
func(void)
{
}

int
main( void )
{
void *x;
void (*f)(void);
f = func;
fprintf(stderr, "sizeof func :%d\n", sizeof func);
fprintf(stderr, "sizeof *x :%d\n", sizeof *x);
fprintf(stderr, "sizeof f :%d\n", sizeof f);
return 0;
}
[tmp]$ ./a.out
sizeof func :1
sizeof *x :1
sizeof f :4

Note that the compiler warns about applying sizeof
to both func and *x. Applying sizeof to a function
type is a constraint violation (6.5.3.4). My question:
where in the standard is the behavior of sizeof( void )
described? (ie, is it a constraint violation?)

--
Bill Pursell
Mar 25 '07 #4
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= wrote:
That said, you can't apply sizeof to a function type.
The definition of sizeof would need a substantial
rewrite to be able to apply to function types.

How much memory a function occupies, isn't related to its type.

strcpy is the same type as strtok,
but they're very different functions.

--
pete
Mar 25 '07 #5
Bill Pursell wrote:
My question:
where in the standard is the behavior of sizeof( void )
described? (ie, is it a constraint violation?)
Yes, it is a constraint violation.
void is an incomplete type.
sizeof applies to object types.

N869
6.2.5 Types

[#18] The void type comprises an empty set of values; it is
an incomplete type that cannot be completed.

6.5.3.4 The sizeof operator
Constraints
[#1] The sizeof operator shall not be applied to an
expression that has function type or an incomplete type,

--
pete
Mar 25 '07 #6
In article <11*********************@p15g2000hsd.googlegroups. com>,
>func()
{
}

main()
{
fprintf(stderr, "sizeof(func):%d\n", sizeof(func));
}

output: sizeof(func):1

I thought that it would return 4 bytes. since func is a symbol which
is pointing to the relative address of the function.
No, func is *not* a pointer to the function, it is the function.
If you had printed sizeof(&func) you would have got the answer
you expected.

Why does sizeof(func) print 1? No good reason. You're not allowed to
do it, and if you had told your compiler to give more warnings it
would probably have told you that.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 25 '07 #7
Servé Laurijssen wrote:
"Harald van D?k" <tr*****@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
I thought that it would return 4 bytes. since func is a symbol which
is pointing to the relative address of the function. But it returns 1
byte. Please help me to understand the program.
Well, I'm surprised you managed to get this compiled at all. Is stderr
automatically defined on your implementation (in which case it's
broken), or did you just not copy the exact code as you compiled it?

broken is a bit strong word imo, it doesnt seems so bad if a compiler has
knowledge of standard library names, although i do understand that it does
make it possible to write code that wont work on other complaiant compilers.
If the compiler automatically defines stderr, it means valid C code
(which does not include <stdio.hand uses its own stderr for other
purposes) won't be accepted by the compiler. If valid C code is not
accepted by the compiler, the compiler is broken.

Mar 25 '07 #8
On 25 Mar 2007 07:11:43 -0700, "Harald van D?k" <tr*****@gmail.com>
wrote:
>param wrote:
>Hi

Help me to understand the program:
func()
{
}

main()
{
fprintf(stderr, "sizeof(func):%d\n", sizeof(func));
}

output: sizeof(func):1

I thought that it would return 4 bytes. since func is a symbol which
is pointing to the relative address of the function. But it returns 1
byte. Please help me to understand the program.

Well, I'm surprised you managed to get this compiled at all. Is stderr
automatically defined on your implementation (in which case it's
broken), or did you just not copy the exact code as you compiled it?

That said, you can't apply sizeof to a function type. If you do
anyway, and the compiler accepts it, there's no guarantee whatsoever
about what'll happen.
Doesn't the expression
func
without parentheses evaluate to the address of the function with type
pointer to function? If so, then sizeof is operating on a pointer and
not on a function.
Remove del for email
Mar 25 '07 #9
On 25 Mar 2007 07:02:34 -0700, "param" <sp**********@gmail.comwrote:
>Hi

Help me to understand the program:
func()
{
}

main()
{
fprintf(stderr, "sizeof(func):%d\n", sizeof(func));
In addition to all the other problems reported, sizeof evaluates to a
size_t but %d expects an argument of type int. Just another instance
of undefined behavior.
>}

output: sizeof(func):1

I thought that it would return 4 bytes. since func is a symbol which
is pointing to the relative address of the function. But it returns 1
byte. Please help me to understand the program.

Thanks
Param

Remove del for email
Mar 25 '07 #10
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <11*********************@p15g2000hsd.googlegroups. com>,
>>func()
{
}

main()
{
fprintf(stderr, "sizeof(func):%d\n", sizeof(func));
}

output: sizeof(func):1

I thought that it would return 4 bytes. since func is a symbol which
is pointing to the relative address of the function.

No, func is *not* a pointer to the function, it is the function.
If you had printed sizeof(&func) you would have got the answer
you expected.
In this context, it's neither. More generally, the expression
func
is implicitly converted to a pointer to the function in most contexts
(even in a function call expression; the function call "operator"
requires a function pointer).

C99 6.3.2.1p4:

A _function designator_ is an expression that has function type.
Except when it is the operand of the sizeof operator[*] or the
unary & operator, a function designator with type "function
returning _type_" is converted to an expression that has type
"pointer to function returning _type_".

A footnote clarifies:

Because this conversion does not occur, the operand of the sizeof
operator remains a function designator and violates the constraint
in 6.5.3.4.
Why does sizeof(func) print 1? No good reason. You're not allowed to
do it, and if you had told your compiler to give more warnings it
would probably have told you that.
<OT>I think gcc allows sizeof and pointer arithmetic to be applied to
function pointer types. See the gcc documentation for details.</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 25 '07 #11
On Mar 26, 10:18 am, Barry Schwarz <schwa...@doezl.netwrote:
>
Doesn't the expression
func
without parentheses evaluate to the address of the function with type
pointer to function?
No. It evaluates to the function. However, it can decay to a
pointer in appropriate contexts, much the same as how the name
of an array can decay to a pointer to its first element.

Mar 25 '07 #12
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>No, func is *not* a pointer to the function, it is the function.
If you had printed sizeof(&func) you would have got the answer
you expected.
>In this context, it's neither.
Given the text you quote, it is a function designator and has function
type. So I think my comment was justified. But of course since it
is a constraint violation this has no standardly useful consequences.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 26 '07 #13
Barry Schwarz wrote:
Doesn't the expression
func
without parentheses evaluate to the address of the function with type
pointer to function?
The two exceptions are when
an expression of function type is an operand of the sizeof operator
and when
an expression of function type is an operand of the address operator.

--
pete
Mar 26 '07 #14

"Harald van D?k" <tr*****@gmail.comha scritto nel messaggio
news:11**********************@p15g2000hsd.googlegr oups.com...
Well, I'm surprised you managed to get this compiled at all. Is stderr
automatically defined on your implementation (in which case it's
broken), or did you just not copy the exact code as you compiled it?

broken is a bit strong word imo, it doesnt seems so bad if a compiler has
knowledge of standard library names, although i do understand that it
does
make it possible to write code that wont work on other complaiant
compilers.

If the compiler automatically defines stderr, it means valid C code
(which does not include <stdio.hand uses its own stderr for other
purposes) won't be accepted by the compiler. If valid C code is not
accepted by the compiler, the compiler is broken.
As long as it has some option to disable this behaviour, it's not broken.
gcc doesn't allow me to call a variable random if I include stdlib.h,
because *its* stdlib.h contains a function random() (essentially the same as
rand()). But "random" doesn't begin with an underscore nor is the name of
any standard C function, macro or type, so I have the right to use that
name. Indeed, using gcc -ansi solves the problem.

(BTW, I've seen gcc's stdio.h, and its definitions of stdin and stdout are
#define stdin stdin
#define stdout stdout
/* C89-C99 say they're macros. Make them happy. */
, or something like that. LOL)
Mar 26 '07 #15
"Army1987" <pl********@for.itwrites:
[...]
(BTW, I've seen gcc's stdio.h, and its definitions of stdin and stdout are
#define stdin stdin
#define stdout stdout
/* C89-C99 say they're macros. Make them happy. */
, or something like that. LOL)
No you haven't seen gcc's stdio.h, since gcc has no stdio.h. You've
probably seen glibc's stdio.h.

gcc is a compiler, not a complete implementation. The gcc compiler
can be combined with a runtime library (either glibc or something
else), along with an operating system, to create a complete C
implementation.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 26 '07 #16
Army1987 wrote:
"Harald van D?k" <tr*****@gmail.comha scritto nel messaggio
news:11**********************@p15g2000hsd.googlegr oups.com...
Well, I'm surprised you managed to get this compiled at all. Is stderr
automatically defined on your implementation (in which case it's
broken), or did you just not copy the exact code as you compiled it?

broken is a bit strong word imo, it doesnt seems so bad if a compiler has
knowledge of standard library names, although i do understand that it
does
make it possible to write code that wont work on other complaiant
compilers.
If the compiler automatically defines stderr, it means valid C code
(which does not include <stdio.hand uses its own stderr for other
purposes) won't be accepted by the compiler. If valid C code is not
accepted by the compiler, the compiler is broken.

As long as it has some option to disable this behaviour, it's not broken.
Right, thanks for that. As long as it has some option to disable this
behaviour (and this option is hopefully documented as a requirement
for conformance to either of the C standards) the compiler can define
stderr or any other ordinary identifier as whatever it wants.

Mar 26 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yash | last post by:
Hi, Can someone please explain to me what the StreamReader.DiscardBufferedData method does? The documentation says "Use DiscardBufferedData to seek to a known location in the underlying stream...
0
by: Bruce D | last post by:
I'm trying to understand MySQL better. I can't seem to figure out why the second query takes 2 minutes and the first query takes 12 seconds. It should be noted that the table, KBM, has 250...
14
by: Ina Schmitz | last post by:
Hello all, I don't succeed in displaying the explain plan. I use IBM DB2 Universal Database 8.2. I tried to do the example given in the online help for "Visual Explain". The tables...
3
by: Lenn | last post by:
Hello, I have the following example of AsyncCallback from a C# book which I wanted to implement in my project: //Class with AsyncDelegate public class AsyncProcess { public AsyncProcess() {
12
by: Andrew Ducker | last post by:
And no, this isn't a complaint about break - I'm very happy to make things explicit. However, why isn't the format something like: switch(myVariable) { case 1: { //Do Something
0
by: JAW | last post by:
This plan seems like it should perform well. Does anyone see anything. SQL Statement Text: DECLARE MTR - RDG - EST - CSR CURSOR FOR
2
by: heming_g | last post by:
two tables with the same table structure : tb_xxx and tb_xxx_tmp in table tb_xxx , column "listno" is the primary key of itself and foreign key of dozen of tables . here is my sql .. "...
1
by: td0g03 | last post by:
Hello, I am new to C and I am new to English. I not sure what palindromes mean. I don't know exactly what my teacher wants me to do. If someone could explain it to me in a different way that would be...
4
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
1
by: w.l.fischer | last post by:
Hi, the following sequence: set current explain mode yes; set current explain snapshot yes; update ...; set current explain mode no;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.