Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Explanation of sizeof(...) request

Reply
Thread Tools

Explanation of sizeof(...) request

 
 
signuts
Guest
Posts: n/a
 
      07-31-2003
I'm aware of what sizeof(...) does, what I would like to know is if
sizeof(...) is compiled in or a function that's executed at run-time.

Like for example

{
int a;

printf("a is %d bytes\n", sizeof(a));
}
would this yield the same "machine code" as it's constant value:
{
int a;
printf("a is %d bytes\n", 4);
}

Another thing I just thought of while writing this up is how are
sizeof(...) and pointers computed? is a pointer architecture specific?
a pointer of type int* and char* both yield 4 bytes on my x86 32bit
machine.

Thank ya much
--
Sig
Thu Jul 31 11:03:12 EDT 2003

 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      07-31-2003

"signuts" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> I'm aware of what sizeof(...) does, what I would like to know is if
> sizeof(...) is compiled in or a function that's executed at run-time.
>
> Like for example
>
> {
> int a;
>
> printf("a is %d bytes\n", sizeof(a));
> }
> would this yield the same "machine code" as it's constant value:
> {
> int a;
> printf("a is %d bytes\n", 4);
> }
>
> Another thing I just thought of while writing this up is how are
> sizeof(...) and pointers computed? is a pointer architecture specific?
> a pointer of type int* and char* both yield 4 bytes on my x86 32bit
> machine.
>
> Thank ya much
> --
> Sig
> Thu Jul 31 11:03:12 EDT 2003
>


I dont know the answer but I could stab a guess.
The sizeof is calculated at run-time unless the compiler has suitable
optimisation. For example, you say that sizeof(int) is 4Bytes on your
machine, therefore, one would expect the compiler to use this rather than
calculating the sizeof at runtime. However, for strings etc. the sizeof is
not known at compile time therefore must be calculated at runtime.
I believe the pointers on your architecture are 4Bytes since you are using a
32-bit architecture, but I could be wrong.
Allan


 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      07-31-2003
Allan Bruce wrote:

> "signuts" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
>> I'm aware of what sizeof(...) does, what I would like to know is if
>> sizeof(...) is compiled in or a function that's executed at run-time.
>>
>> Like for example
>>
>> {
>> int a;
>>
>> printf("a is %d bytes\n", sizeof(a));
>> }
>> would this yield the same "machine code" as it's constant value:
>> {
>> int a;
>> printf("a is %d bytes\n", 4);
>> }
>>
>> Another thing I just thought of while writing this up is how are
>> sizeof(...) and pointers computed? is a pointer architecture specific?
>> a pointer of type int* and char* both yield 4 bytes on my x86 32bit
>> machine.
>>

>
> I dont know the answer but I could stab a guess.
> The sizeof is calculated at run-time unless the compiler has suitable
> optimisation.


sizeof is a compile-time constant [except for C99 variable-length
arrays]. The compiler is, of course, at liberty to recompute it
at run-time, or to insert arbitrary calculations for fun in your
code, but few of them do.

> For example, you say that sizeof(int) is 4Bytes on your
> machine, therefore, one would expect the compiler to use this rather than
> calculating the sizeof at runtime. However, for strings etc. the sizeof
> is not known at compile time


Wrong. The sizeof() is known, although the strlen() probably isn't.

> therefore must be calculated at runtime.
> I believe the pointers on your architecture are 4Bytes since you are using
> a 32-bit architecture, but I could be wrong.


A 32-bit machine might have 16-bit bytes. It almost certainly doesn't,
but it could. Also a 32bit machine might have bigger-than-32bit C pointers
(eg for byte offsets on a word-based machine); again, his machine almost
certainly doesn't, but one shouldn't make assumptions one doesn't need to.

--
Chris "pointy hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
 
Reply With Quote
 
Allan Bruce
Guest
Posts: n/a
 
      07-31-2003

"signuts" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> Thanks , it's starting to make sense.
>
> > However, for strings etc. the sizeof is
> > not known at compile time therefore must be calculated at runtime.

> I'm not sure if this is correct
>
> This example code prints 4 for each line
> struct my_s {
> int a,b,c;
> char buf[32];
> };
>
> int main(int argc, char **argv) {
> struct my_s *a;
> char *b;
>
> printf("size of *a is %d\n", sizeof(a));
> printf("size of *b is %d\n", sizeof(b));
>
> exit(EXIT_SUCCESS);
> }
>
> I'd be interested to see what a pointer returns on a 64-bit machine, would
> it be 8?
>
> --
> Sig
> Thu Jul 31 11:17:49 EDT 2003
>


Of course it will - all you are doing is evaluating the size of pointers
again, if you do sizeof(*a) then you will get a decent answer, btw dont try
sizeof(*b) as it will cause undefined behaviour as it stands, but if you
were to have:

char Name[] = "Allan Bruce";
char *pName;

pName = Name;

printf("%d %d\n", sizeof(pName), sizeof(*pName));

This will return

4 12

4 is because you are getting the size of the pointer, and 12 because the
string is 12 chars long (this needs a NULL terminator stop undefined
behaviour".
Allan


 
Reply With Quote
 
Allan Bruce
Guest
Posts: n/a
 
      07-31-2003

"Allan Bruce" <(E-Mail Removed)> wrote in message
news:bgbd9g$s8u$(E-Mail Removed)2surf.net...
>
> "signuts" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
> > Thanks , it's starting to make sense.
> >
> > > However, for strings etc. the sizeof is
> > > not known at compile time therefore must be calculated at runtime.

> > I'm not sure if this is correct
> >
> > This example code prints 4 for each line
> > struct my_s {
> > int a,b,c;
> > char buf[32];
> > };
> >
> > int main(int argc, char **argv) {
> > struct my_s *a;
> > char *b;
> >
> > printf("size of *a is %d\n", sizeof(a));
> > printf("size of *b is %d\n", sizeof(b));
> >
> > exit(EXIT_SUCCESS);
> > }
> >
> > I'd be interested to see what a pointer returns on a 64-bit machine,

would
> > it be 8?
> >
> > --
> > Sig
> > Thu Jul 31 11:17:49 EDT 2003
> >

>
> Of course it will - all you are doing is evaluating the size of pointers
> again, if you do sizeof(*a) then you will get a decent answer, btw dont

try
> sizeof(*b) as it will cause undefined behaviour as it stands, but if you
> were to have:
>
> char Name[] = "Allan Bruce";
> char *pName;
>
> pName = Name;
>
> printf("%d %d\n", sizeof(pName), sizeof(*pName));
>
> This will return
>
> 4 12
>
> 4 is because you are getting the size of the pointer, and 12 because the
> string is 12 chars long (this needs a NULL terminator stop undefined
> behaviour".
> Allan
>
>


actually void this! I forgot strlen(*pName) will return 12, and sizeof wont.
Sorry


 
Reply With Quote
 
Allan Bruce
Guest
Posts: n/a
 
      07-31-2003

"Chris Dollin" <(E-Mail Removed)> wrote in message
news:bgbd54$a97$(E-Mail Removed)...
> Allan Bruce wrote:
>
> > "signuts" <(E-Mail Removed)> wrote in message
> > news(E-Mail Removed)...
> >> I'm aware of what sizeof(...) does, what I would like to know is if
> >> sizeof(...) is compiled in or a function that's executed at run-time.
> >>
> >> Like for example
> >>
> >> {
> >> int a;
> >>
> >> printf("a is %d bytes\n", sizeof(a));
> >> }
> >> would this yield the same "machine code" as it's constant value:
> >> {
> >> int a;
> >> printf("a is %d bytes\n", 4);
> >> }
> >>
> >> Another thing I just thought of while writing this up is how are
> >> sizeof(...) and pointers computed? is a pointer architecture specific?
> >> a pointer of type int* and char* both yield 4 bytes on my x86 32bit
> >> machine.
> >>

> >
> > I dont know the answer but I could stab a guess.
> > The sizeof is calculated at run-time unless the compiler has suitable
> > optimisation.

>
> sizeof is a compile-time constant [except for C99 variable-length
> arrays]. The compiler is, of course, at liberty to recompute it
> at run-time, or to insert arbitrary calculations for fun in your
> code, but few of them do.
>
> > For example, you say that sizeof(int) is 4Bytes on your
> > machine, therefore, one would expect the compiler to use this rather

than
> > calculating the sizeof at runtime. However, for strings etc. the sizeof
> > is not known at compile time

>
> Wrong. The sizeof() is known, although the strlen() probably isn't.


You are completely correct - I was getting a bit muddled up
Allan


 
Reply With Quote
 
bd
Guest
Posts: n/a
 
      07-31-2003
On Thu, 31 Jul 2003 16:18:36 +0100, Allan Bruce wrote:

>
> "signuts" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
>> I'm aware of what sizeof(...) does, what I would like to know is if
>> sizeof(...) is compiled in or a function that's executed at run-time.
>>
>> Like for example
>>
>> {
>> int a;
>>
>> printf("a is %d bytes\n", sizeof(a));
>> }
>> would this yield the same "machine code" as it's constant value:
>> {
>> int a;
>> printf("a is %d bytes\n", 4);
>> }
>>
>> Another thing I just thought of while writing this up is how are
>> sizeof(...) and pointers computed? is a pointer architecture specific?
>> a pointer of type int* and char* both yield 4 bytes on my x86 32bit
>> machine.
>>
>> Thank ya much
>> --
>> Sig
>> Thu Jul 31 11:03:12 EDT 2003
>>

>
> I dont know the answer but I could stab a guess.
> The sizeof is calculated at run-time unless the compiler has suitable
> optimisation. For example, you say that sizeof(int) is 4Bytes on your
> machine, therefore, one would expect the compiler to use this rather than
> calculating the sizeof at runtime. However, for strings etc. the sizeof is
> not known at compile time therefore must be calculated at runtime.
> I believe the pointers on your architecture are 4Bytes since you are using a
> 32-bit architecture, but I could be wrong.
> Allan



No, strings are known at compile-time, or are a pointer. In either case,
it's a fixed length. The only time when run-time calculation is necessare
is with C99 variable-length arrays, e.g.:

void foo(int x){
int bar[x];
printf("sizeof bar = %zu\n", sizeof bar);
}

Many compilers do not yet support this, however.
--
Freenet distribution not available
Tart words make no friends; a spoonful of honey will catch more flies than
a gallon of vinegar.
-- B. Franklin

 
Reply With Quote
 
bd
Guest
Posts: n/a
 
      07-31-2003
On Thu, 31 Jul 2003 16:42:15 +0100, Allan Bruce wrote:

>
> "signuts" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
>> Thanks , it's starting to make sense.
>>
>> > However, for strings etc. the sizeof is
>> > not known at compile time therefore must be calculated at runtime.

>> I'm not sure if this is correct
>>
>> This example code prints 4 for each line
>> struct my_s {
>> int a,b,c;
>> char buf[32];
>> };
>>
>> int main(int argc, char **argv) {
>> struct my_s *a;
>> char *b;
>>
>> printf("size of *a is %d\n", sizeof(a));
>> printf("size of *b is %d\n", sizeof(b));
>>
>> exit(EXIT_SUCCESS);
>> }
>>
>> I'd be interested to see what a pointer returns on a 64-bit machine, would
>> it be 8?
>>
>> --
>> Sig
>> Thu Jul 31 11:17:49 EDT 2003
>>

>
> Of course it will - all you are doing is evaluating the size of pointers
> again, if you do sizeof(*a) then you will get a decent answer, btw dont try
> sizeof(*b) as it will cause undefined behaviour as it stands, but if you
> were to have:


No, sizeof(*b) is legal, and will be 1. I don't think you understand with
sizeof does.

> char Name[] = "Allan Bruce";
> char *pName;
>
> pName = Name;
>
> printf("%d %d\n", sizeof(pName), sizeof(*pName));
>
> This will return
>
> 4 12
>
> 4 is because you are getting the size of the pointer, and 12 because the
> string is 12 chars long (this needs a NULL terminator stop undefined
> behaviour".


Wrong. sizeof *pName will be 1. strlen(pName) will be 11, however.
sizeof Name, however, will be 12.
--
Freenet distribution not available
BOFH Excuse #14:

sounds like a Windows problem, try calling Microsoft support

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-31-2003
In 'comp.lang.c', signuts <(E-Mail Removed)> wrote:

> I'm aware of what sizeof(...) does, what I would like to know is if
> sizeof(...) is compiled in or a function that's executed at run-time.


'sizeof' returns a constant of type 'size_t' evaluated at compile time.

> Like for example
>
> {
> int a;
>
> printf("a is %d bytes\n", sizeof(a));


printf ("a is %lu bytes\n", (unsigned long) sizeof a);

or in C99:

printf ("a is %zu bytes\n", sizeof a);

> }
> would this yield the same "machine code" as it's constant value:
> {
> int a;
> printf("a is %d bytes\n", 4);
> }


Yes, assuming that on you implementation, sizeof (int) == 4.

> Another thing I just thought of while writing this up is how are
> sizeof(...) and pointers computed?


By the compiler in an implentation-dependent manner.

> is a pointer architecture specific?


Yes.

> a pointer of type int* and char* both yield 4 bytes on my x86 32bit
> machine.


And your question is?

--
-ed- http://www.velocityreviews.com/forums/(E-Mail Removed) [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-31-2003
In 'comp.lang.c', signuts <(E-Mail Removed)> wrote:

> I'd be interested to see what a pointer returns on a 64-bit machine, would
> it be 8?


The C language can't give an answer to implementation-specific questions.
Please try it yourself, or ask to a newsgroup dedicated to theses platforms.

--
-ed- (E-Mail Removed) [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Integer division with / - request explanation of behavior Wes Gamble Ruby 12 09-30-2006 06:36 PM
Simple Explanation to Networking Wirelessly?? Jaxim Wireless Networking 4 08-19-2005 05:04 AM
ASP .NET 2.0 special folders explanation request Bob Hanson ASP .Net 2 04-30-2005 06:23 PM
explanation Mariusz VHDL 1 01-13-2004 02:10 AM
Re: Accessing Request.InputStream / Request.BinaryRead *as the request is occuring*: How??? Brian Birtle ASP .Net 2 10-16-2003 02:11 PM



Advertisments