473,322 Members | 1,566 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,322 software developers and data experts.

some pointer issues....

double * X[5]

size of X->??
size of X[0]->??

double (*X)[5]
size of X->??
size of X[0]->??
Also if i want to print sizeof(main) it gives me 1
and sizeof(main()) gives me 4 why?

Aug 12 '07 #1
34 1845
On 12 Aug., 17:24, "sumedh....." <sumedhsak...@gmail.comwrote:
double * X[5]

size of X->??
size of X[0]->??

double (*X)[5]
size of X->??
size of X[0]->??

Also if i want to print sizeof(main) it gives me 1
and sizeof(main()) gives me 4 why?
Try this test program:

/****************************/
#include <stdio.h>

double *X[5];
/* X is an array of 5 "pointers to double" */

double (*Y)[5];
/* Y is a pointer to an array of 5 doubles */

int main(void)
{
double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5};
X[0] = &array_of_5_double[0];
X[1] = &array_of_5_double[1];
X[2] = &array_of_5_double[2];
X[3] = &array_of_5_double[3];
X[4] = &array_of_5_double[4];
#if 0 // why does this not work?
X = {&array_of_5_double[0], /* array_of_5_double */ \
&array_of_5_double[1], \
&array_of_5_double[2], \
&array_of_5_double[3], \
&array_of_5_double[4]};
#endif
printf("sizeof(double) = %d // sizeof(double *) = %d //
sizeof(double (*)[5]) = %d\n", \
sizeof(double), sizeof(double *), sizeof(double (*)[5]));
/* 8 */

printf("sizeof(X) = %d // X = %p\n", sizeof(X), X,
&array_of_5_double);

printf("sizeof(X[0]) = %d // X[0] = %p // &array_of_5_double[0]
= %p\n", sizeof(X[0]), X[0], &array_of_5_double[0]);
// X[0] is the first element of our array: here it is a pointer to a
double with value 0.5

printf("sizeof(Y) = %d\n", sizeof(Y));

printf("sizeof(Y[0]) = %d\n", sizeof(Y[0])); // 40

printf("sizeof(main) = %d // sizeof(main()) = %d\n", sizeof(main),
sizeof(main()));

return 0;
}

/*
main is a function pointer!
main() is the invocation of the function "int main(void)" which will
return an integer of the following size:
sizeof(int) == sizeof(main())
*/

Explanations:

double *X[5];
/* X is an array of 5 "pointers to double" */
Thus sizeof(X) = 5*sizeof(double *)
"double *" is a pointer to a double (tip: move from right to left)
double (*Y)[5];
/* Y is a pointer to an array of 5 doubles */
Thus sizeof(Y) = sizeof(double (*)[5])
"double (*)[5]" is a pointer to an array of 5 doubles (tip: move from
inside to outside)
/*
/*
main is a function pointer!
main() is the invocation of the function "int main(void)" which will
return an integer of the following size:
sizeof(int)
This sizeof(main()) is the same size, i.e. the size of the returned
integer
*/

-anon.asdf

Aug 12 '07 #2
On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
On 12 Aug., 17:24, "sumedh....." <sumedhsak...@gmail.comwrote:
double * X[5]
size of X->??
size of X[0]->??
double (*X)[5]
size of X->??
size of X[0]->??
Also if i want to print sizeof(main) it gives me 1
and sizeof(main()) gives me 4 why?

Try this test program:

/****************************/
#include <stdio.h>

double *X[5];
/* X is an array of 5 "pointers to double" */

double (*Y)[5];
/* Y is a pointer to an array of 5 doubles */

int main(void)
{
double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5};
X[0] = &array_of_5_double[0];
X[1] = &array_of_5_double[1];
X[2] = &array_of_5_double[2];
X[3] = &array_of_5_double[3];
X[4] = &array_of_5_double[4];
#if 0 // why does this not work?
X = {&array_of_5_double[0], /* array_of_5_double */ \
&array_of_5_double[1], \
&array_of_5_double[2], \
&array_of_5_double[3], \
&array_of_5_double[4]};
#endif
printf("sizeof(double) = %d // sizeof(double *) = %d //
sizeof(double (*)[5]) = %d\n", \
sizeof(double), sizeof(double *), sizeof(double (*)[5]));
/* 8 */

printf("sizeof(X) = %d // X = %p\n", sizeof(X), X,
&array_of_5_double);

printf("sizeof(X[0]) = %d // X[0] = %p // &array_of_5_double[0]
= %p\n", sizeof(X[0]), X[0], &array_of_5_double[0]);
// X[0] is the first element of our array: here it is a pointer to a
double with value 0.5

printf("sizeof(Y) = %d\n", sizeof(Y));

printf("sizeof(Y[0]) = %d\n", sizeof(Y[0])); // 40

printf("sizeof(main) = %d // sizeof(main()) = %d\n", sizeof(main),
sizeof(main()));

return 0;

}

/*
main is a function pointer!
main() is the invocation of the function "int main(void)" which will
return an integer of the following size:
sizeof(int) == sizeof(main())
*/

Explanations:

double *X[5];
/* X is an array of 5 "pointers to double" */
Thus sizeof(X) = 5*sizeof(double *)
"double *" is a pointer to a double (tip: move from right to left)

double (*Y)[5];
/* Y is a pointer to an array of 5 doubles */
Thus sizeof(Y) = sizeof(double (*)[5])
"double (*)[5]" is a pointer to an array of 5 doubles (tip: move from
inside to outside)
/*

/*
main is a function pointer!
main() is the invocation of the function "int main(void)" which will
return an integer of the following size:
sizeof(int)
This sizeof(main()) is the same size, i.e. the size of the returned
integer
*/

-anon.asdf
gr8 explanation:
just wanted to know why does
sizeof(main) -1
sizeof(main()) -sizeof(int)?????

Aug 12 '07 #3

"sumedh....." <su**********@gmail.comwrote in message
news:11*********************@i13g2000prf.googlegro ups.com...
double * X[5]
Array of five pointers to type double
>
size of X->??
size of X is reported by

sizeof X

or

sizeof(double *[5])

I recommend the first method.
size of X[0]->??
size of X[0] is reported by

sizeof X[0]

or

sizeof(double)

Again, I recommend the first method.
>
double (*X)[5]
Pointer to an array of five doubles
size of X->??
size of X is reported by

sizeof X

or

sizeof(double(*)[5]

Again, I recommend the first method.
size of X[0]->??
size of X[0] is reported by

sizeof X[0]

or

sizeof(double[5])

Again, I recommend the first method.
>

Also if i want to print sizeof(main) it gives me 1
and sizeof(main()) gives me 4 why?
'main' (without parentheses) yields the address of
the function 'main()'. So sizeof(main) will give
the size of a pointer to a function. 'main()' invokes
the function main(), and returns its return value. This
type is 'int'. So 'sizeof(main())' gives the size of
type 'int'.

Note that these type sizes will vary among platforms (and
could conceivably vary among implementations for the same
platform).

-Mike
Aug 12 '07 #4

"sumedh....." <su**********@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
sizeof(main) -1
sizeof(main()) -sizeof(int)?????
The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike

Aug 12 '07 #5
On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"sumedh....." <sumedhsak...@gmail.comwrote in message

news:11**********************@i38g2000prf.googlegr oups.com...
On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
sizeof(main) -1
sizeof(main()) -sizeof(int)?????

The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike
i have read that size of pointer is 2bytes,
its 4bytes on my compiler... not an issue...
now...
If its a pointer type? why an integer/float/void/double pointer
declared inside main yields there size as 4 and main as 1?
so is this pointer diff than others?

Aug 12 '07 #6
Mike Wahler wrote, On 12/08/07 18:42:
"sumedh....." <su**********@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
>On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
sizeof(main) -1
sizeof(main()) -sizeof(int)?????

The type of 'main' is a pointer type.
Wrong. The time of main is a function, not a pointer. sizeof(main) is
actually a constraint violation and a diagnostic is required. Of course,
with some C compilers (e.g. gcc) you have to poke the compiler hard
enough to get all the required diagnostics. Personally I think this gcc
extension is pointless.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.
Indeed.
--
Flash Gordon
Aug 12 '07 #7
sumedh..... wrote, On 12/08/07 19:17:
On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"sumedh....." <sumedhsak...@gmail.comwrote in message

news:11**********************@i38g2000prf.googleg roups.com...
>>On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
sizeof(main) -1
sizeof(main()) -sizeof(int)?????
The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike

i have read that size of pointer is 2bytes,
Whatever you read is implementation specific, not part of C. Also
irrelevant.
its 4bytes on my compiler... not an issue...
As you see. I've used implementations where the sizeof a pointer is 1.
now...
If its a pointer type? why an integer/float/void/double pointer
declared inside main yields there size as 4 and main as 1?
so is this pointer diff than others?
Because main is not a pointer and Mike was wrong.
--
Flash Gordon
Aug 12 '07 #8

"sumedh....." <su**********@gmail.comwrote in message
news:11*********************@i13g2000prf.googlegro ups.com...
On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"sumedh....." <sumedhsak...@gmail.comwrote in message

news:11**********************@i38g2000prf.googleg roups.com...
On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
sizeof(main) -1
sizeof(main()) -sizeof(int)?????

The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike

i have read that size of pointer is 2bytes,
its 4bytes on my compiler... not an issue...
The C language does not specify particular
sizes for pointer types. This depends upon your
implementation.
now...
If its a pointer type? why an integer/float/void/double pointer
declared inside main yields there size as 4 and main as 1?
so is this pointer diff than others?
There are several different pointer types in C. E.g.
'pointer to int', 'pointer to double', 'pointer to function',
etc. There's no requirement that the sizes of these pointers
are the same?

BTW why are you so concerned about these sizes? If some
part(s) of your program needs to know, just use the sizeof
operator. The specific sizes should not concern you.

-Mike
Aug 12 '07 #9

"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
news:0r************@news.flash-gordon.me.uk...
sumedh..... wrote, On 12/08/07 19:17:
>On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>>"sumedh....." <sumedhsak...@gmail.comwrote in message

news:11**********************@i38g2000prf.google groups.com...

On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
sizeof(main) -1
sizeof(main()) -sizeof(int)?????
The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike

i have read that size of pointer is 2bytes,

Whatever you read is implementation specific, not part of C. Also
irrelevant.
>its 4bytes on my compiler... not an issue...

As you see. I've used implementations where the sizeof a pointer is 1.
>now...
If its a pointer type? why an integer/float/void/double pointer
declared inside main yields there size as 4 and main as 1?
so is this pointer diff than others?

Because main is not a pointer and Mike was wrong.
Huh? Are you saying that the name of a function (without parentheses)
does not yeild a pointer value?

-Mike
Aug 12 '07 #10

"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
news:qh************@news.flash-gordon.me.uk...
Mike Wahler wrote, On 12/08/07 18:42:
>"sumedh....." <su**********@gmail.comwrote in message
news:11**********************@i38g2000prf.googleg roups.com...
>>On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
sizeof(main) -1
sizeof(main()) -sizeof(int)?????

The type of 'main' is a pointer type.

Wrong. The time of main is a function,
Yes, its type is 'function'. But in a program,
the name (without parens) will yield an address.

-Mike
Aug 12 '07 #11
Mike Wahler wrote, On 12/08/07 20:34:
"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
news:qh************@news.flash-gordon.me.uk...
>Mike Wahler wrote, On 12/08/07 18:42:
>>"sumedh....." <su**********@gmail.comwrote in message
news:11**********************@i38g2000prf.google groups.com...
On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
sizeof(main) -1
sizeof(main()) -sizeof(int)?????
The type of 'main' is a pointer type.
Wrong. The time of main is a function,

Yes, its type is 'function'. But in a program,
the name (without parens) will yield an address.
N1124 section 6.3.2.1 para 4 it says
| A function designator is an expression that has function type.
| Except when it is the operand of the sizeof operator54) 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’’.

Further, the constraints section of the definition of the sizeof
operator says:
| The sizeof operator shall not be applied to an expression that
| has function type or ...

So as I said the type is function, not pointer to funtion, and a
diagnostic (warning or error) is *required*.
--
Flash Gordon
Aug 12 '07 #12
Mike Wahler wrote:
>
"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
Because main is not a pointer and Mike was wrong.

Huh? Are you saying that the name of a function (without parentheses)
does not yeild a pointer value?
The only two times when an expression of function type is not
automatically converted to a pointer, are:
1 as an operand of &
2 as an operand of sizeof

Since sizeof is not defined for expressions of function type,
the only thing that you can do with an expression of function type
in a correct C program, is to derive a pointer from it.

--
pete
Aug 12 '07 #13
"Mike Wahler" <mk******@mkwahler.netwrites:
[...]
'main' (without parentheses) yields the address of
the function 'main()'.
In most contexts, yes.
So sizeof(main) will give
the size of a pointer to a function.
No, the operand of a sizeof operator is one of the contexts in which a
function name (more generally an expression of function type) is not
converted to a pointer. 'sizeof(main)' (or 'sizeof main'; the
parentheses are not necessary) is a constraint violation. <OT>gcc's
"-pedantic" option causes it to emit the required diagnostic.</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"
Aug 12 '07 #14
On Aug 13, 2:35 am, pete <pfil...@mindspring.comwrote:
Mike Wahler wrote:
"Flash Gordon" <s...@flash-gordon.me.ukwrote in message
Because main is not a pointer and Mike was wrong.
Huh? Are you saying that the name of a function (without parentheses)
does not yeild a pointer value?

The only two times when an expression of function type is not
automatically converted to a pointer, are:
1 as an operand of &
2 as an operand of sizeof

Since sizeof is not defined for expressions of function type,
the only thing that you can do with an expression of function type
in a correct C program, is to derive a pointer from it.

--
pete
pete would you like to explain by giving an example?
if i derive a pointer from it what would be the type of that pointer?

Aug 13 '07 #15
sumedh..... wrote:
>
On Aug 13, 2:35 am, pete <pfil...@mindspring.comwrote:
Mike Wahler wrote:
"Flash Gordon" <s...@flash-gordon.me.ukwrote in message
Because main is not a pointer and Mike was wrong.
Huh? Are you saying that the name of a function (without parentheses)
does not yeild a pointer value?
The only two times when an expression of function type is not
automatically converted to a pointer, are:
1 as an operand of &
2 as an operand of sizeof

Since sizeof is not defined for expressions of function type,
the only thing that you can do with an expression of function type
in a correct C program, is to derive a pointer from it.

--
pete

pete would you like to explain by giving an example?
if i derive a pointer from it what would be the type of that pointer?
The type of (&main) is a pointer to a function
returning type int and taking no arguments.

--
pete
Aug 13 '07 #16
pete wrote:
>
sumedh..... wrote:
Since sizeof is not defined for expressions of function type,
the only thing that you can do with an expression of function type
in a correct C program, is to derive a pointer from it.
>
--
pete
pete would you like to explain by giving an example?
if i derive a pointer from it what would
be the type of that pointer?

The type of (&main) is a pointer to a function
returning type int and taking no arguments.
Well actually, how many arguments depends on how main is defined.

--
pete
Aug 13 '07 #17
sumedh..... wrote:
On Aug 13, 2:35 am, pete <pfil...@mindspring.comwrote:
>Mike Wahler wrote:
>>"Flash Gordon" <s...@flash-gordon.me.ukwrote in message
Because main is not a pointer and Mike was wrong.
Huh? Are you saying that the name of a function (without parentheses)
does not yeild a pointer value?
The only two times when an expression of function type is not
automatically converted to a pointer, are:
1 as an operand of &
2 as an operand of sizeof

Since sizeof is not defined for expressions of function type,
the only thing that you can do with an expression of function type
in a correct C program, is to derive a pointer from it.
pete would you like to explain by giving an example?
if i derive a pointer from it what would be the type of that pointer?
Does this make it clearer?

typedef int (*Fn)(void);

int main( void ) {
Fn fn = main;
}

--
Ian Collins.
Aug 13 '07 #18
Does this make it clearer?

typedef int (*Fn)(void);

int main( void ) {
Fn fn = main;

}

--
Ian Collins.
typedef int (*Fn)(void);
int main( void ) {
Fn fn = main;
printf("%d\n",sizeof(fn)); // o/ps 4 (that is size of pointer to
function returning int.)
printf("%d\n",sizeof(&fn)); // o/ps 4 (that is size of pointer to
function returning int.)
printf("%d\n",sizeof(&main)); // o/ps 4 (that is size of pointer to
function returning int.)
printf("%d\n",sizeof(main)); //o/ps 1 inspite of main & (&main) are
both the same thing.. pointer //to function returning int
printf("%d %d \n",fn,&fn); //o/ps addresses A & B
printf("%d %d \n",main,&main); //o/ps addresses A & A
system("PAUSE");
return 0;
}

Aug 13 '07 #19
sumedh..... wrote:
>Does this make it clearer?

typedef int (*Fn)(void);

int main( void ) {
Fn fn = main;

}
*Please don't quote signatures.*
>
typedef int (*Fn)(void);
int main( void ) {
Fn fn = main;
printf("%d\n",sizeof(main)); //o/ps 1 inspite of main & (&main) are
both the same thing.. pointer //to function returning int
Constraint violation, see 6.5.3.4.1

"The sizeof operator shall not be applied to an expression that has
function type..."

Did this even compile?

--
Ian Collins.
Aug 13 '07 #20
On Aug 13, 1:19 pm, Ian Collins <ian-n...@hotmail.comwrote:
sumedh..... wrote:
Does this make it clearer?
typedef int (*Fn)(void);
int main( void ) {
Fn fn = main;
}

*Please don't quote signatures.*
typedef int (*Fn)(void);
int main( void ) {
Fn fn = main;
printf("%d\n",sizeof(main)); //o/ps 1 inspite of main & (&main) are
both the same thing.. pointer //to function returning int

Constraint violation, see 6.5.3.4.1

"The sizeof operator shall not be applied to an expression that has
function type..."

Did this even compile?

--
Ian Collins.
it compiles and runs properly.

Aug 13 '07 #21
sumedh..... wrote:
On Aug 13, 1:19 pm, Ian Collins <ian-n...@hotmail.comwrote:
>sumedh..... wrote:
>>>Does this make it clearer?
typedef int (*Fn)(void);
int main( void ) {
Fn fn = main;
}
typedef int (*Fn)(void);
int main( void ) {
Fn fn = main;
printf("%d\n",sizeof(main)); //o/ps 1 inspite of main & (&main) are
both the same thing.. pointer //to function returning int
Constraint violation, see 6.5.3.4.1

"The sizeof operator shall not be applied to an expression that has
function type..."

Did this even compile?
*Please don't quote signatures.*
>
it compiles and runs properly.
Then either get a better compiler, or turn up the warnings.

--
Ian Collins.
Aug 13 '07 #22
Ian Collins <ia******@hotmail.comwrites:
sumedh..... wrote:
>>Does this make it clearer?

typedef int (*Fn)(void);

int main( void ) {
Fn fn = main;

}

*Please don't quote signatures.*
>>
typedef int (*Fn)(void);
int main( void ) {
Fn fn = main;
> printf("%d\n",sizeof(main)); //o/ps 1 inspite of main & (&main) are
both the same thing.. pointer //to function returning int

Constraint violation, see 6.5.3.4.1

"The sizeof operator shall not be applied to an expression that has
function type..."

Did this even compile?
Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension)
by default.

To the original poster: if you invoke gc with "-std=c99 -pedantic", it
will complain about several problems in your program. If you replace
"-std=99" with "-ansi", it will also complain about the "//" comments
(actually it will completely fail to recognize them and complain about
syntax errors).

Using "//" comments in Usenet postings is usually a bad idea; wrapping
of long lines can easily introduce syntax errors. And you've
neglected the #include directives for <stdio.h(needed for printf)
and <stdlib.h(needed for system). And you attempt to use the "%d"
format for arguments of type size_t and of pointer types.

Here's a *nearly* correct version of your program:

#include <stdio.h>
#include <stdlib.h>
typedef int (*Fn)(void);
int main( void ) {
Fn fn = main;
printf("%d\n", (int)sizeof(fn));
printf("%d\n", (int)sizeof(&fn));
printf("%d\n", (int)sizeof(&main));
/* printf("%d\n", (int)sizeof(main)); */
printf("%p %p\n", (void*)fn, (void*)&fn);
printf("%p %p\n", (void*)main, (void*)&main);
return 0;
}

I wrote "nearly" because the casts on the last two printf statements
are actually illegal. There is no portable way to print the value of
a pointer-to-function (other than by decomposing it into a sequence of
bytes). Allowing conversion to void* is a common extension -- more
common, I think, than allowing sizeof to be applied to functions.

--
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"
Aug 13 '07 #23
Ian Collins wrote:
>
sumedh..... wrote:
On Aug 13, 1:19 pm, Ian Collins <ian-n...@hotmail.comwrote:
sumedh..... wrote:
> printf("%d\n",sizeof(main));
Constraint violation, see 6.5.3.4.1

"The sizeof operator shall not be applied to an expression that has
function type..."

Did this even compile?
*Please don't quote signatures.*

it compiles and runs properly.
Then either get a better compiler, or turn up the warnings.
.... and or make sure that the compiler
is configured as an ANSI C compiler.

--
pete
Aug 13 '07 #24
Keith Thompson wrote:
>
Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension)
by default.
That's one thing that has puzzled me for a long time, why would anyone
want to apply sizeof to a function? Even more puzzling considering gcc
evaluates it as 1 when they do.

--
Ian Collins.
Aug 13 '07 #25
Ian Collins said:
Keith Thompson wrote:
>>
Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension)
by default.
That's one thing that has puzzled me for a long time, why would anyone
want to apply sizeof to a function?
Well, if we stop thinking C and start thinking raw machine for a minute,
it's not difficult to imagine why it might be useful to know how big a
function is - given that information, we could replace some or all of
the function with inline machine code at runtime, presumably only under
certain runtime conditions (because otherwise, why bother?).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 13 '07 #26
Richard Heathfield wrote:
Ian Collins said:
>Keith Thompson wrote:
>>Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension)
by default.
That's one thing that has puzzled me for a long time, why would anyone
want to apply sizeof to a function?

Well, if we stop thinking C and start thinking raw machine for a minute,
it's not difficult to imagine why it might be useful to know how big a
function is - given that information, we could replace some or all of
the function with inline machine code at runtime, presumably only under
certain runtime conditions (because otherwise, why bother?).
Fair enough, but what's the point if the result of the expression is
always 1?

--
Ian Collins.
Aug 13 '07 #27
Ian Collins wrote:
Richard Heathfield wrote:
>Ian Collins said:
>>Keith Thompson wrote:
Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension)
by default.

That's one thing that has puzzled me for a long time, why would anyone
want to apply sizeof to a function?
Well, if we stop thinking C and start thinking raw machine for a minute,
it's not difficult to imagine why it might be useful to know how big a
function is - given that information, we could replace some or all of
the function with inline machine code at runtime, presumably only under
certain runtime conditions (because otherwise, why bother?).
Fair enough, but what's the point if the result of the expression is
always 1?
Even for an extern function!

--
Ian Collins.
Aug 13 '07 #28
Ian Collins wrote:
Keith Thompson wrote:
>>
Quite possibly it does; gcc accepts 'sizeof(main)' (as an extension)
by default.
That's one thing that has puzzled me for a long time, why would anyone
want to apply sizeof to a function? Even more puzzling considering gcc
evaluates it as 1 when they do.
gcc makes sizeof(void) and sizeof(func) 1 so that you can do pointer
arithmetic on void and function pointers as if they were char pointers. In
standard C, a conversion to char * is required for this:

/* non-standard */
void *p = malloc(12);
/* NULL check omitted */
int *i = p + 4;
long *l = p + 8;

versus

/* standard */
void *p = malloc(12);
/* NULL check omitted */
int *i = (int *) ((char *) p + 4);
long *l = (long *) ((char *) p + 8);

I don't particularly see the need for this, especially for function
pointers, but apparently others did.
Aug 13 '07 #29
Ian Collins said:
Richard Heathfield wrote:
>Ian Collins said:
>>Keith Thompson wrote:
Quite possibly it does; gcc accepts 'sizeof(main)' (as an
extension) by default.

That's one thing that has puzzled me for a long time, why would
anyone want to apply sizeof to a function?
<my explan snipped>
>>
Fair enough, but what's the point if the result of the expression is
always 1?
Perhaps gcc is just very, very, very good at optimisation? :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 13 '07 #30
On Mon, 13 Aug 2007 13:31:10 +0000, Richard Heathfield wrote:
Ian Collins said:
>Richard Heathfield wrote:
>>Ian Collins said:

Keith Thompson wrote:
Quite possibly it does; gcc accepts 'sizeof(main)' (as an
extension) by default.
>
That's one thing that has puzzled me for a long time, why would
anyone want to apply sizeof to a function?
<my explan snipped>
>>>
Fair enough, but what's the point if the result of the expression is
always 1?

Perhaps gcc is just very, very, very good at optimisation? :-)
The problem is when it has to optimize more than 256 functions
that well. :-)
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 13 '07 #31
Richard Heathfield wrote:
Well, if we stop thinking C and start thinking raw machine
for a minute,
it's not difficult to imagine why it might be useful to know how big a
function is - given that information, we could replace some or all of
the function with inline machine code at runtime,
presumably only under
certain runtime conditions (because otherwise, why bother?).
I don't think it is useful.
The function with the size in question,
might work by calling other functions.
If the sizeof the function doesn't include those helper functions
then it doesn't have the information that you need.
If the sizeof the function does include those helper functions,
you don't know how many of those helper functions
are being used by other functions,
which means that if you delete one function that calls them,
there may still be other functions
that call some of the same helper functions and
your calculations of how much space can be saved
by deleting the function in question, will be wrong.

--
pete
Aug 13 '07 #32
pete said:
Richard Heathfield wrote:
>Well, if we stop thinking C and start thinking raw machine
for a minute,
it's not difficult to imagine why it might be useful to know how big
a function is - given that information, we could replace some or all
of the function with inline machine code at runtime,
presumably only under
certain runtime conditions (because otherwise, why bother?).

I don't think it is useful.
Well, it isn't all /that/ useful.
The function with the size in question,
might work by calling other functions.
Wouldn't matter, if you were replacing it at runtime with inline
assembly language.
If the sizeof the function doesn't include those helper functions
then it doesn't have the information that you need.
You lost me. Why would you need that info? All you're doing is an
overlay, not a complete runtime code optimisation.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 14 '07 #33
Richard Heathfield wrote:
Ian Collins said:
>Richard Heathfield wrote:
>>Ian Collins said:

Keith Thompson wrote:
Quite possibly it does; gcc accepts 'sizeof(main)' (as an
extension) by default.
>
That's one thing that has puzzled me for a long time, why would
anyone want to apply sizeof to a function?
<my explan snipped>
>Fair enough, but what's the point if the result of the expression is
always 1?

Perhaps gcc is just very, very, very good at optimisation? :-)
And psychic, considering the sizeof an extern function is also 1!

--
Ian Collins.
Aug 14 '07 #34
Richard Heathfield wrote:
>
pete said:
Richard Heathfield wrote:
Well, if we stop thinking C and start thinking raw machine
for a minute, it's not difficult to imagine
why it might be useful to know how big
a function is - given that information,
we could replace some or all
of the function with inline machine code at runtime,
presumably only under
certain runtime conditions (because otherwise, why bother?).
If the sizeof the function doesn't include those helper functions
then it doesn't have the information that you need.

You lost me. Why would you need that info? All you're doing is an
overlay, not a complete runtime code optimisation.
I guess I don't understand the usage.
I look at compiler listing files
when I want to see the memory usage for the program instructions.

--
pete
Aug 15 '07 #35

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

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
2
by: Robbie Hatley | last post by:
About 20 days ago, "John Harrison" <john_andronicus@hotmail.com> replied to a message which I had written: > > > Q1 . What is the use of pointer to an array? > > > > It points to it. (The name...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
40
by: Steve Rencontre | last post by:
I can't for the life of me see how to do pointer-to-member when the member is actually part of an embedded structure. That is, if I have: struct S1 { int a; }; struct S2 { S1 s; int b; }; ...
7
by: Mohan | last post by:
Hi, What are the advantages/disadvantages of using a pointer instead of Reference in the Copy Constructor ? For Example, Writing the Copy constructor for the Class "Temp" as below, ...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
9
by: Dave Stallard | last post by:
Pardon if this is the wrong newsgroup for this question, and/or if this question is naive. I have a multi-threaded Windows application in which certain variables/object fields are shared: one...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.