what I miss?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Parahat  Melayev

    #1

    what I miss?

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

    int main()
    {
    unsigned char *c;
    c = malloc(sizeof(u nsigned 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) )

  • Alexei A. Frounze

    #2
    Re: what I miss?

    "Parahat Melayev" <parahat@gmail. com> wrote in message
    news:1121437470 .167407.137680@ g47g2000cwa.goo glegroups.com.. .[color=blue]
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <limits.h>
    >
    > int main()
    > {
    > unsigned char *c;
    > c = malloc(sizeof(u nsigned 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?[/color]

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

    Alex


    Comment

    • Parahat  Melayev

      #3
      Re: what I miss?

      oh yeah that is right :) panic

      tnx

      Comment

      • ranjeet.gupta@gmail.com

        #4
        Re: what I miss?



        Parahat Melayev wrote:[color=blue]
        > #include <stdio.h>
        > #include <stdlib.h>
        > #include <limits.h>
        >
        > int main()
        > {
        > unsigned char *c;
        > c = malloc(sizeof(u nsigned 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?[/color]

        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(u nsigned char));

        HTH
        ranjeet
        [color=blue]
        >
        > ( gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) )[/color]

        Comment

        • Eric Sosman

          #5
          Re: what I miss?



          Parahat Melayev wrote:[color=blue]
          > #include <stdio.h>
          > #include <stdlib.h>
          > #include <limits.h>
          >
          > int main()
          > {
          > unsigned char *c;
          > c = malloc(sizeof(u nsigned 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?[/color]

          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@sun .com

          Comment

          • Parahat  Melayev

            #6
            Re: what I miss?

            thanks but problem is not at malloc.
            it must be,

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

            Comment

            • Kenny McCormack

              #7
              Re: what I miss?

              In article <1121438369.882 630.279940@f14g 2000cwb.googleg roups.com>,
              Parahat Melayev <parahat@gmail. com> wrote:[color=blue]
              >thanks but problem is not at malloc.
              >it must be,
              >
              >printf("size of *c: %d\n", sizeof(*c));
              >[/color]

              Glad to see you've solved your own problem. Congratulations !

              Comment

              • Richard Heathfield

                #8
                Re: what I miss?

                Some minor nits...

                Alexei A. Frounze wrote:
                [color=blue]
                > "Parahat Melayev" <parahat@gmail. com> wrote in message
                > news:1121437470 .167407.137680@ g47g2000cwa.goo glegroups.com.. .[color=green]
                >> #include <stdio.h>
                >> #include <stdlib.h>
                >> #include <limits.h>
                >>
                >> int main()
                >> {
                >> unsigned char *c;
                >> c = malloc(sizeof(u nsigned 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?[/color]
                >
                > Nope. C isn't a char, it's a pointer (to a char).[/color]

                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(uns igned 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

                mail: rjh at above domain

                Comment

                • pete

                  #9
                  Re: what I miss?

                  ranjeet.gupta@g mail.com wrote:[color=blue]
                  >
                  > Parahat Melayev wrote:[color=green]
                  > > #include <stdio.h>
                  > > #include <stdlib.h>
                  > > #include <limits.h>
                  > >
                  > > int main()
                  > > {
                  > > unsigned char *c;
                  > > c = malloc(sizeof(u nsigned 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?[/color]
                  >
                  > 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.[/color]

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

                  --
                  pete

                  Comment

                  • Sensei

                    #10
                    Re: what I miss?

                    On 2005-07-15 09:24:30 -0500, "Parahat Melayev" <parahat@gmail. com> said:
                    [color=blue]
                    > #include <stdio.h>
                    > #include <stdlib.h>
                    > #include <limits.h>
                    >
                    > int main()
                    > {
                    > unsigned char *c;
                    > c = malloc(sizeof(u nsigned char));
                    > printf("size of unsigned char: %d\n", sizeof(unsigned char));[/color]

                    The size of an unsigned char is 1.

                    [color=blue]
                    > printf("size of c: %d\n", sizeof(c));[/color]

                    The size of a pointer is 4.

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

                    [color=blue]
                    > 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) )[/color]

                    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 <senseiwa@tin.i t>

                    cd /pub
                    more beer

                    Comment

                    • Martin Ambuhl

                      #11
                      Re: what I miss?

                      Parahat Melayev wrote:[color=blue]
                      > #include <stdio.h>
                      > #include <stdlib.h>
                      > #include <limits.h>
                      >
                      > int main()
                      > {
                      > unsigned char *c;
                      > c = malloc(sizeof(u nsigned char));
                      > printf("size of unsigned char: %d\n", sizeof(unsigned char));[/color]
                      ^^
                      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.
                      [color=blue]
                      > printf("size of c: %d\n", sizeof(c));
                      > return 0;
                      > }[/color]
                      [color=blue]
                      > When I execute this, it says that size of "unsigned char" is "1" & size
                      > of "c" is "4". isn't that strange?[/color]

                      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?

                      Comment

                      • Emmanuel Delahaye

                        #12
                        Re: what I miss?

                        Parahat Melayev wrote on 15/07/05 :[color=blue]
                        > #include <stdio.h>
                        > #include <stdlib.h>
                        > #include <limits.h>
                        >
                        > int main()
                        > {
                        > unsigned char *c;
                        > c = malloc(sizeof(u nsigned 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) )[/color]

                        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?"

                        Comment

                        • Keith Thompson

                          #13
                          Re: what I miss?

                          "Emmanuel Delahaye" <emdel@YOURBRAn oos.fr> writes:
                          [...][color=blue]
                          > Note that sizeof returns a size_t. You should use "%zu" as a formatter.[/color]

                          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_Keit h) kst-u@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.

                          Comment

                          • Emmanuel Delahaye

                            #14
                            Re: what I miss?

                            Keith Thompson wrote on 15/07/05 :[color=blue]
                            > "Emmanuel Delahaye" <emdel@YOURBRAn oos.fr> writes:
                            > [...][color=green]
                            >> Note that sizeof returns a size_t. You should use "%zu" as a formatter.[/color]
                            >
                            > 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.[/color]

                            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++

                            Comment

                            • Michael Mair

                              #15
                              Re: what I miss?

                              Emmanuel Delahaye wrote:[color=blue]
                              > Keith Thompson wrote on 15/07/05 :
                              >[color=green]
                              >> "Emmanuel Delahaye" <emdel@YOURBRAn oos.fr> writes:
                              >> [...]
                              >>[color=darkred]
                              >>> Note that sizeof returns a size_t. You should use "%zu" as a formatter.[/color]
                              >>
                              >>
                              >> 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.[/color]
                              >
                              > From the OP : "gcc version 4.0.0"[/color]

                              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.

                              Comment

                              Working...