Explanation of sizeof(...) request

Discussion in 'C Programming' started by signuts, Jul 31, 2003.

  1. signuts

    signuts Guest

    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
    signuts, Jul 31, 2003
    #1
    1. Advertising

  2. signuts

    Allan Bruce Guest

    "signuts" <> wrote in message
    news:p...
    > 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
    Allan Bruce, Jul 31, 2003
    #2
    1. Advertising

  3. signuts

    Chris Dollin Guest

    Allan Bruce wrote:

    > "signuts" <> wrote in message
    > news:p...
    >> 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-newsgroup/comp/comp.lang.c.html
    C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
    Chris Dollin, Jul 31, 2003
    #3
  4. signuts

    Allan Bruce Guest

    "signuts" <> wrote in message
    news:p...
    > 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
    Allan Bruce, Jul 31, 2003
    #4
  5. signuts

    Allan Bruce Guest

    "Allan Bruce" <> wrote in message
    news:bgbd9g$s8u$2surf.net...
    >
    > "signuts" <> wrote in message
    > news:p...
    > > 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
    Allan Bruce, Jul 31, 2003
    #5
  6. signuts

    Allan Bruce Guest

    "Chris Dollin" <> wrote in message
    news:bgbd54$a97$...
    > Allan Bruce wrote:
    >
    > > "signuts" <> wrote in message
    > > news:p...
    > >> 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
    Allan Bruce, Jul 31, 2003
    #6
  7. signuts

    bd Guest

    On Thu, 31 Jul 2003 16:18:36 +0100, Allan Bruce wrote:

    >
    > "signuts" <> wrote in message
    > news:p...
    >> 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
    bd, Jul 31, 2003
    #7
  8. signuts

    bd Guest

    On Thu, 31 Jul 2003 16:42:15 +0100, Allan Bruce wrote:

    >
    > "signuts" <> wrote in message
    > news:p...
    >> 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
    bd, Jul 31, 2003
    #8
  9. In 'comp.lang.c', signuts <> 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- [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/
    Emmanuel Delahaye, Jul 31, 2003
    #9
  10. In 'comp.lang.c', signuts <> 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- [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/
    Emmanuel Delahaye, Jul 31, 2003
    #10
  11. On 31 Jul 2003, Emmanuel Delahaye wrote:

    > > The sizeof is calculated at run-time unless the compiler has suitable

    >
    > No. 'sizeof' is computed at compile-time. Always.


    int nit(int a) {
    int b[a];
    return sizeof b;
    }
    Jarno A Wuolijoki, Aug 1, 2003
    #11
  12. signuts

    Rishix Guest

    On 8/1/2003 5:14 AM, Jarno A Wuolijoki wrote:
    > On 31 Jul 2003, Emmanuel Delahaye wrote:
    >
    >
    >>>The sizeof is calculated at run-time unless the compiler has suitable

    >>
    >>No. 'sizeof' is computed at compile-time. Always.

    >
    >
    > int nit(int a) {
    > int b[a];
    > return sizeof b;
    > }
    >

    K&R says
    For array declaration if parameter is present it should be constant expression
    which is not the case here.
    Rishix, Aug 1, 2003
    #12
  13. signuts

    Chris Dollin Guest

    Rishix wrote:

    > On 8/1/2003 5:14 AM, Jarno A Wuolijoki wrote:
    >> On 31 Jul 2003, Emmanuel Delahaye wrote:
    >>
    >>>>The sizeof is calculated at run-time unless the compiler has suitable
    >>>
    >>>No. 'sizeof' is computed at compile-time. Always.

    >>
    >> int nit(int a) {
    >> int b[a];
    >> return sizeof b;
    >> }
    >>

    > K&R says
    > For array declaration if parameter is present it should be constant
    > expression which is not the case here.


    C99 says that a non-constant expression is allowed. So sizeof need
    not be computed at compile-time if you have a C99 compiler.

    --
    Chris "electric hedgehog" Dollin
    C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
    C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
    Chris Dollin, Aug 1, 2003
    #13
  14. In 'comp.lang.c', Jarno A Wuolijoki <> wrote:

    >> > The sizeof is calculated at run-time unless the compiler has suitable

    >>
    >> No. 'sizeof' is computed at compile-time. Always.

    >
    > int nit(int a) {
    > int b[a];
    > return sizeof b;
    > }


    Yeah, I was thinking of C90. In C99, sizeof is evaluated at run time on
    VLA's.

    --
    -ed- [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/
    Emmanuel Delahaye, Aug 1, 2003
    #14
  15. In 'comp.lang.c', (Dan Pop) wrote:

    >>No. 'sizeof' is computed at compile-time. Always.

    >
    > Chapter and verse, please.
    >
    > It would be a remarkable feat for a C99 compiler to compute the size
    > of a VLA at compile time ;-)


    Yeah, bloody C99! It came out of my mind.

    --
    -ed- [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/
    Emmanuel Delahaye, Aug 1, 2003
    #15
  16. signuts

    Dan Pop Guest

    In <Xns93CAB91B120F4hsnoservernet@130.133.1.4> Emmanuel Delahaye <> writes:

    >In 'comp.lang.c', (Dan Pop) wrote:
    >
    >>>No. 'sizeof' is computed at compile-time. Always.

    >>
    >> Chapter and verse, please.
    >>
    >> It would be a remarkable feat for a C99 compiler to compute the size
    >> of a VLA at compile time ;-)

    >
    >Yeah, bloody C99! It came out of my mind.


    Even C89 doesn't guarantee that sizeof is computed at compile time.
    It merely requires that it is computable at compile time, which is not
    the same thing!

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Aug 4, 2003
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,331
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    629
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,415
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    555
    Joe Smith
    May 22, 2006
  5. Replies:
    11
    Views:
    505
    Charlie Gordon
    Sep 15, 2007
Loading...

Share This Page