what I miss?

Discussion in 'C Programming' started by Parahat Melayev, Jul 15, 2005.

  1. #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>

    int main()
    {
    unsigned char *c;
    c = malloc(sizeof(unsigned char));
    printf("size of unsigned char: %d\n", sizeof(unsigned char));
    printf("size of c: %d\n", sizeof(c));
    return 0;
    }

    When I execute this, it says that size of "unsigned char" is "1" & size
    of "c" is "4". isn't that strange?

    ( gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) )
     
    Parahat Melayev, Jul 15, 2005
    #1
    1. Advertising

  2. "Parahat Melayev" <> wrote in message
    news:...
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <limits.h>
    >
    > int main()
    > {
    > unsigned char *c;
    > c = malloc(sizeof(unsigned char));
    > printf("size of unsigned char: %d\n", sizeof(unsigned char));
    > printf("size of c: %d\n", sizeof(c));
    > return 0;
    > }
    >
    > When I execute this, it says that size of "unsigned char" is "1" & size
    > of "c" is "4". isn't that strange?


    Nope. C isn't a char, it's a pointer (to a char).

    Alex
     
    Alexei A. Frounze, Jul 15, 2005
    #2
    1. Advertising

  3. oh yeah that is right :) panic

    tnx
     
    Parahat Melayev, Jul 15, 2005
    #3
  4. Parahat  Melayev

    Eric Sosman Guest

    Parahat Melayev wrote:
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <limits.h>
    >
    > int main()
    > {
    > unsigned char *c;
    > c = malloc(sizeof(unsigned char));
    > printf("size of unsigned char: %d\n", sizeof(unsigned char));
    > printf("size of c: %d\n", sizeof(c));
    > return 0;
    > }
    >
    > When I execute this, it says that size of "unsigned char" is "1" & size
    > of "c" is "4". isn't that strange?


    Not very. I know of systems where this program
    would report both sizes as zero -- a good deal stranger,
    don't you think?

    (Hint: What is the type of the result of `sizeof',
    and what is the type expected by the "%d" conversion?)

    --
     
    Eric Sosman, Jul 15, 2005
    #4
  5. Parahat  Melayev

    Guest

    Parahat Melayev wrote:
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <limits.h>
    >
    > int main()
    > {
    > unsigned char *c;
    > c = malloc(sizeof(unsigned char));
    > printf("size of unsigned char: %d\n", sizeof(unsigned char));
    > printf("size of c: %d\n", sizeof(c));
    > return 0;
    > }
    >
    > When I execute this, it says that size of "unsigned char" is "1" & size
    > of "c" is "4". isn't that strange?


    what is c ????
    c is the pointer which is going to hold the address which is of the
    char type.

    malloc will return the address of the allocated memory.
    and so c holds the address, which is int.

    c = malloc(sizeof(unsigned char));

    HTH
    ranjeet

    >
    > ( gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) )
     
    , Jul 15, 2005
    #5
  6. thanks but problem is not at malloc.
    it must be,

    printf("size of *c: %d\n", sizeof(*c));
     
    Parahat Melayev, Jul 15, 2005
    #6
  7. In article <>,
    Parahat Melayev <> wrote:
    >thanks but problem is not at malloc.
    >it must be,
    >
    >printf("size of *c: %d\n", sizeof(*c));
    >


    Glad to see you've solved your own problem. Congratulations!
     
    Kenny McCormack, Jul 15, 2005
    #7
  8. Some minor nits...

    Alexei A. Frounze wrote:

    > "Parahat Melayev" <> wrote in message
    > news:...
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >> #include <limits.h>
    >>
    >> int main()
    >> {
    >> unsigned char *c;
    >> c = malloc(sizeof(unsigned char));
    >> printf("size of unsigned char: %d\n", sizeof(unsigned char));
    >> printf("size of c: %d\n", sizeof(c));
    >> return 0;
    >> }
    >>
    >> When I execute this, it says that size of "unsigned char" is "1" & size
    >> of "c" is "4". isn't that strange?

    >
    > Nope. C isn't a char, it's a pointer (to a char).


    No, C is a programming language (hint - case sensitivity!), and c is not a
    pointer to a char, but a pointer to an unsigned char.

    Usual stuff follows:

    A) many people prefer a full prototype for main(), as in:
    int main(void)

    B) using the template p = malloc(n * sizeof *p), we could improve the malloc
    to:
    c = malloc(sizeof *c);
    (ignoring n on this occasion, since we appear only to want one object).

    C) sizeof yields a size_t, which is an unsigned integer type of unknown
    size (in C90), so we will need to cast it. Unsigned longs are good
    for this, so make that:

    printf("size of unsigned char: %lu\n",
    (unsigned long)sizeof(unsigned char));

    (note that this is now required to write 1 on stdout), and

    printf("size of c: %lu\n", (unsigned long)sizeof c);

    Superfluous parentheses removed. Note the updated format specifiers.

    Did I miss anything?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    mail: rjh at above domain
     
    Richard Heathfield, Jul 15, 2005
    #8
  9. Parahat  Melayev

    pete Guest

    wrote:
    >
    > Parahat Melayev wrote:
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > > #include <limits.h>
    > >
    > > int main()
    > > {
    > > unsigned char *c;
    > > c = malloc(sizeof(unsigned char));
    > > printf("size of unsigned char: %d\n",
    > > sizeof(unsigned char));
    > > printf("size of c: %d\n", sizeof(c));
    > > return 0;
    > > }
    > >
    > > When I execute this,
    > > it says that size of "unsigned char" is "1" & size
    > > of "c" is "4". isn't that strange?

    >
    > what is c ????
    > c is the pointer which is going to hold the address which is of the
    > char type.
    >
    > malloc will return the address of the allocated memory.
    > and so c holds the address, which is int.


    The address is not int.
    A pointer type is different from an int type.

    --
    pete
     
    pete, Jul 15, 2005
    #9
  10. Parahat  Melayev

    Sensei Guest

    On 2005-07-15 09:24:30 -0500, "Parahat Melayev" <> said:

    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <limits.h>
    >
    > int main()
    > {
    > unsigned char *c;
    > c = malloc(sizeof(unsigned char));
    > printf("size of unsigned char: %d\n", sizeof(unsigned char));


    The size of an unsigned char is 1.


    > printf("size of c: %d\n", sizeof(c));


    The size of a pointer is 4.

    The size of the memory handled by the pointer is 1.


    > When I execute this, it says that size of "unsigned char" is "1" & size
    > of "c" is "4". isn't that strange?
    >
    > ( gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) )


    I suggest reading a basic C book. You must know the difference between
    a variable and a pointer.

    PS. Sizes of vars and ptrs may vary on different platforms.

    --
    Sensei <>

    cd /pub
    more beer
     
    Sensei, Jul 15, 2005
    #10
  11. Parahat Melayev wrote:
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <limits.h>
    >
    > int main()
    > {
    > unsigned char *c;
    > c = malloc(sizeof(unsigned char));
    > printf("size of unsigned char: %d\n", sizeof(unsigned char));

    ^^
    You have no reason to think that the size_t that sizeof() yields is a
    signed int, as '%d' requires. In fact, we know that it is not, since a
    size_t is by definition unsigned, and will often be of a wider type than
    int.

    > printf("size of c: %d\n", sizeof(c));
    > return 0;
    > }


    > When I execute this, it says that size of "unsigned char" is "1" & size
    > of "c" is "4". isn't that strange?


    No. c is a pointer-to-unsigned-char. Do you, for some reason, imagine
    that a pointer-to-T should have the same size as a T?
     
    Martin Ambuhl, Jul 15, 2005
    #11
  12. Parahat Melayev wrote on 15/07/05 :
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <limits.h>
    >
    > int main()
    > {
    > unsigned char *c;
    > c = malloc(sizeof(unsigned char));
    > printf("size of unsigned char: %d\n", sizeof(unsigned char));
    > printf("size of c: %d\n", sizeof(c));
    > return 0;
    > }
    >
    > When I execute this, it says that size of "unsigned char" is "1" & size
    > of "c" is "4". isn't that strange?
    >
    > ( gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) )


    It's fine. c is a pointer. The sizeof of a pointer can be 4 (actually,
    it's implementation-dependent).

    Note that sizeof returns a size_t. You should use "%zu" as a formatter.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    I once asked an expert COBOL programmer, how to
    declare local variables in COBOL, the reply was:
    "what is a local variable?"
     
    Emmanuel Delahaye, Jul 15, 2005
    #12
  13. "Emmanuel Delahaye" <> writes:
    [...]
    > Note that sizeof returns a size_t. You should use "%zu" as a formatter.


    You should use "%zu" only if you can depend on your runtime library to
    implement it. "%zu" is a new feature in C99. Even if your compiler
    supports C99 (or most of it), the runtime library may not.

    A reasonably safe way to print a size_t value, compatible with both
    C90 and C99, is:

    printf("%lu", (unsigned long)sizeof foo);

    The only case where this can fail is if sizeof foo exceeds ULONG_MAX,
    which can only happen if size_t is bigger than unsigned long, which
    can't happen in C99. Even then, it can only happen if foo is a huge
    object, 4 gigabytes or bigger.

    If you're worried about that possibility, you can use the preprocessor
    to check for __STDC_VERSION__ >= 199901L.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jul 15, 2005
    #13
  14. Keith Thompson wrote on 15/07/05 :
    > "Emmanuel Delahaye" <> writes:
    > [...]
    >> Note that sizeof returns a size_t. You should use "%zu" as a formatter.

    >
    > You should use "%zu" only if you can depend on your runtime library to
    > implement it. "%zu" is a new feature in C99. Even if your compiler
    > supports C99 (or most of it), the runtime library may not.


    From the OP : "gcc version 4.0.0"

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Clearly your code does not meet the original spec."
    "You are sentenced to 30 lashes with a wet noodle."
    -- Jerry Coffin in a.l.c.c++
     
    Emmanuel Delahaye, Jul 15, 2005
    #14
  15. Parahat  Melayev

    Michael Mair Guest

    Emmanuel Delahaye wrote:
    > Keith Thompson wrote on 15/07/05 :
    >
    >> "Emmanuel Delahaye" <> writes:
    >> [...]
    >>
    >>> Note that sizeof returns a size_t. You should use "%zu" as a formatter.

    >>
    >>
    >> You should use "%zu" only if you can depend on your runtime library to
    >> implement it. "%zu" is a new feature in C99. Even if your compiler
    >> supports C99 (or most of it), the runtime library may not.

    >
    > From the OP : "gcc version 4.0.0"


    So what? The OP uses gcc on a Linux system and thus probably has an
    appropriate glibc to do the trick.
    I can have gcc 4.0.0 on my cygwin under windows yet the runtime
    library still is upset about "%zu"...


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Jul 15, 2005
    #15
  16. "Emmanuel Delahaye" <> writes:
    > Keith Thompson wrote on 15/07/05 :
    >> "Emmanuel Delahaye" <> writes:
    >> [...]
    >>> Note that sizeof returns a size_t. You should use "%zu" as a formatter.

    >>
    >> You should use "%zu" only if you can depend on your runtime library to
    >> implement it. "%zu" is a new feature in C99. Even if your compiler
    >> supports C99 (or most of it), the runtime library may not.

    >
    > From the OP : "gcc version 4.0.0"


    That provides no clue about whether the runtime library supports "zu".

    I have gcc 4.0.0 on three different systems. On one of them, the
    following program:

    #include <stdio.h>
    int main(void)
    {
    size_t s = 42;
    printf("s = %zu\n", s);
    return 0;
    }

    prints "s = 42"; on the other two, it prints "s = zu". (I'd mention
    which systems they are if it were relevant.)

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jul 15, 2005
    #16
  17. "Keith Thompson" <> wrote in message
    news:...
    > "Emmanuel Delahaye" <> writes:
    > > Keith Thompson wrote on 15/07/05 :
    > >> "Emmanuel Delahaye" <> writes:
    > >> [...]
    > >>> Note that sizeof returns a size_t. You should use "%zu" as a

    formatter.

    Wow, there's a new type specifier for size_t in *printf()? Great. I used to
    use long for it and to make sure it's indeed long was casting to it.

    Btw, how about pointers to functions and pointers to data? The two can be
    different types (I mean not the C's idea about the types but rather the
    effective size in bits of them). Are there any special specifiers for each?

    Alex
     
    Alexei A. Frounze, Jul 15, 2005
    #17
  18. Parahat  Melayev

    Michael Mair Guest

    Alexei A. Frounze wrote:
    > "Keith Thompson" <> wrote in message
    > news:...
    >
    >>"Emmanuel Delahaye" <> writes:
    >>
    >>>Keith Thompson wrote on 15/07/05 :
    >>>
    >>>>"Emmanuel Delahaye" <> writes:
    >>>>[...]
    >>>>
    >>>>>Note that sizeof returns a size_t. You should use "%zu" as a

    >
    > formatter.
    >
    > Wow, there's a new type specifier for size_t in *printf()? Great. I used to
    > use long for it and to make sure it's indeed long was casting to it.


    ITYM "unsigned long"/"%lu" -- this is the best bet under C89.

    > Btw, how about pointers to functions and pointers to data? The two can be
    > different types (I mean not the C's idea about the types but rather the
    > effective size in bits of them). Are there any special specifiers for each?


    AFAIK, no.
    There are length modifiers for ptrdiff_t("t"), intmax_t("j"),
    char("hh"), long long("ll") and a conversion specifier for hex
    fractions/floating point representation ("a","A").

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Jul 15, 2005
    #18
  19. Parahat  Melayev

    Eric Sosman Guest

    Alexei A. Frounze wrote:
    > "Keith Thompson" <> wrote in message
    > news:...
    >
    >>"Emmanuel Delahaye" <> writes:
    >>
    >>>Keith Thompson wrote on 15/07/05 :
    >>>
    >>>>"Emmanuel Delahaye" <> writes:
    >>>>[...]
    >>>>
    >>>>>Note that sizeof returns a size_t. You should use "%zu" as a

    >
    > formatter.
    >
    > Wow, there's a new type specifier for size_t in *printf()? Great. I used to
    > use long for it and to make sure it's indeed long was casting to it.
    >
    > Btw, how about pointers to functions and pointers to data? The two can be
    > different types (I mean not the C's idea about the types but rather the
    > effective size in bits of them). Are there any special specifiers for each?


    Pointers to data can be converted to `void*' and printed
    with "%p", just as in C89. Pointers to functions cannot be
    printed except via subterfuges like printing the values of
    their constituent bytes.

    --
     
    Eric Sosman, Jul 15, 2005
    #19
    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. youngeagle

    I miss frames!

    youngeagle, Aug 24, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    670
    Vidar Petursson
    Oct 23, 2004
  2. Wayne Wengert

    What Did I Miss?

    Wayne Wengert, May 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    457
    Wayne Wengert
    May 25, 2005
  3. Paul Watt

    God I miss tables-help needed!

    Paul Watt, Apr 4, 2006, in forum: HTML
    Replies:
    53
    Views:
    1,225
    Paul Ding
    Apr 8, 2006
  4. Ndeye

    miss something about xslt

    Ndeye, Dec 18, 2003, in forum: XML
    Replies:
    3
    Views:
    405
    Dimitre Novatchev
    Dec 18, 2003
  5. JJ
    Replies:
    2
    Views:
    372
Loading...

Share This Page