far pointers

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

    #31
    Re: far pointers

    "Arthur J. O'Dwyer" <ajo@nospam.and rew.cmu.edu> wrote in message news:<Pine.LNX. 4.60-041.04081209355 90.1583@unix48. andrew.cmu.edu> ...[color=blue]
    > On Thu, 12 Aug 2004, CBFalconer wrote:[color=green]
    > >
    > > Jens.Toerring@p hysik.fu-berlin.de wrote:[color=darkred]
    > >> Needing an array of ints (or long ints) with all elements
    > >> initialized to 0 is more or less the only time I use calloc().[/color]
    > >
    > > While that may work for you, it is not guaranteed by the standard.[/color]
    >
    > Amplification (sorry!;) : memsetting an 'int' to zero is not
    > guaranteed by the Standard to set the actual /value/ of that 'int'
    > to the integer zero. (But I think there's some debate about that;
    > I forget the details.) So what Jens is suggesting doesn't work.
    >
    > However, unsigned types are guaranteed to have pure binary
    > representations , which means that memsetting an 'unsigned int' to
    > zero /will/ set its value to the unsigned integer zero! Ditto
    > 'unsigned char', 'unsigned long', et cetera. (And I think ditto
    > the new C99 fixed-width types: 'int32_t' and 'int_least8_t' and
    > friends.)
    >
    > I often use 'calloc' in image processing; for example, to get
    > a grayscale image of size w*h initialized to black, I'll write
    >
    > unsigned char *im = calloc(w*h, 1);
    >
    > So 'calloc' does have its uses; they're just rare.
    >
    > -Arthur[/color]


    I had gone through three books (just basics ones) but didnt find
    memset memmove ect can any one plz explain it .

    Comment

    • Emmanuel Delahaye

      #32
      Re: far pointers

      Harsimran wrote on 13/08/04 :
      [color=blue]
      > I had gone through three books (just basics ones) but didnt find
      > memset memmove ect can any one plz explain it .[/color]

      A good reference for C functions:



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

      "C is a sharp tool"

      Comment

      • Joona I Palaste

        #33
        Re: far pointers

        Harsimran <sainiharsimran @yahoo.co.in> scribbled the following:[color=blue]
        > "Arthur J. O'Dwyer" <ajo@nospam.and rew.cmu.edu> wrote in message news:<Pine.LNX. 4.60-041.04081209355 90.1583@unix48. andrew.cmu.edu> ...[color=green]
        >> On Thu, 12 Aug 2004, CBFalconer wrote:[color=darkred]
        >> >
        >> > Jens.Toerring@p hysik.fu-berlin.de wrote:
        >> >> Needing an array of ints (or long ints) with all elements
        >> >> initialized to 0 is more or less the only time I use calloc().
        >> >
        >> > While that may work for you, it is not guaranteed by the standard.[/color]
        >>
        >> Amplification (sorry!;) : memsetting an 'int' to zero is not
        >> guaranteed by the Standard to set the actual /value/ of that 'int'
        >> to the integer zero. (But I think there's some debate about that;
        >> I forget the details.) So what Jens is suggesting doesn't work.
        >>
        >> However, unsigned types are guaranteed to have pure binary
        >> representations , which means that memsetting an 'unsigned int' to
        >> zero /will/ set its value to the unsigned integer zero! Ditto
        >> 'unsigned char', 'unsigned long', et cetera. (And I think ditto
        >> the new C99 fixed-width types: 'int32_t' and 'int_least8_t' and
        >> friends.)
        >>
        >> I often use 'calloc' in image processing; for example, to get
        >> a grayscale image of size w*h initialized to black, I'll write
        >>
        >> unsigned char *im = calloc(w*h, 1);
        >>
        >> So 'calloc' does have its uses; they're just rare.[/color][/color]
        [color=blue]
        > I had gone through three books (just basics ones) but didnt find
        > memset memmove ect can any one plz explain it .[/color]

        They must be pretty crappy books then. memset is a function for setting
        every byte in a range of bytes to a specific value. memmove is for
        moving a range of bytes into another location in memory. Does this
        answer your question?

        --
        /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
        \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
        "Show me a good mouser and I'll show you a cat with bad breath."
        - Garfield

        Comment

        • Emmanuel Delahaye

          #34
          Re: far pointers

          Joona I Palaste wrote on 13/08/04 :[color=blue]
          > memmove is for
          > moving a range of bytes into another location in memory.[/color]

          Actually, no. memmove() is used to copy bytes in overlapping memory.

          char s[] = "hello world";

          memmove (s, s + 6, 5);

          produces "world world"

          Due to overlapping,

          /* Don't do that */
          memcpy (s, s + 6, 5);

          would have produced an Undefined Behaviour.

          (I'm quite sure you was aware of that...)

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

          "C is a sharp tool"

          Comment

          • Flash Gordon

            #35
            Re: far pointers

            On 13 Aug 2004 04:57:51 -0700
            sainiharsimran@ yahoo.co.in (Harsimran) wrote:

            <snip>
            [color=blue]
            > I had gone through three books (just basics ones) but didnt find
            > memset memmove ect can any one plz explain it .[/color]

            I can offer three possible explanations.

            1) The books were bad.
            2) The books were aimed only at getting you just about started and
            deliberately left out those functions.
            3) You didn't read the books very carefully.

            I would recommend that you get The C Programming Language 2nd edition by
            Kernighan & Ritchie.
            --
            Flash Gordon
            Sometimes I think shooting would be far too good for some people.
            Although my email address says spam, it is real and I read it.

            Comment

            • Default User

              #36
              Re: far pointers

              Edd wrote:[color=blue]
              >
              > Harsimran wrote:[color=green]
              > > Can any one explain what are far pointers and what is the difference
              > > between malloc and calloc .Which is better ?[/color]
              >
              > I believe others have answered your question perfectly well, but while we're on
              > the subject I'd like to ask about something that's been niggling me for a while now.
              >
              > One of my C programming books (A Book On C 4th edition, Kelley & Pohl) has the
              > following to say about malloc and calloc (page 663):
              >
              > void *calloc(size_t n, size_t el_size);
              > Allocates contiguous space in memory for an array of n elements, with each
              > element requiring el_size bytes. The space is initialized with all bits set to
              > zero. A successful call returns the base address of the allocated space;
              > otherwise, NULL is returned.
              >
              > void *malloc(size_t size);
              > Allocates a block in memory consisting of size bytes. The space is not
              > initialized. A successful call returns the base address of the allocated space;
              > otherwise, NULL is returned.
              >
              > The niggle of which I speak is with respect to the "contiguous " part of the
              > calloc description; is the space allocated by a successful malloc call not
              > necessarily required to be contiguous?[/color]


              Note that the description of malloc() states it returns a "block in
              memory". I can't think of any definition of "block" that would allow it
              to be non-contiguous. I believe the author just used slightly different
              phrasing.


              Brian Rodenborn

              Comment

              • CBFalconer

                #37
                Re: far pointers

                Joona I Palaste wrote:[color=blue]
                > Harsimran <sainiharsimran @yahoo.co.in> scribbled the following:[color=green]
                >> "Arthur J. O'Dwyer" <ajo@nospam.and rew.cmu.edu> wrote:[color=darkred]
                >>> On Thu, 12 Aug 2004, CBFalconer wrote:
                >>>> Jens.Toerring@p hysik.fu-berlin.de wrote:
                >>>>
                >>>>> Needing an array of ints (or long ints) with all elements
                >>>>> initialized to 0 is more or less the only time I use calloc().
                >>>>
                >>>> While that may work for you, it is not guaranteed by the standard.
                >>>
                >>> Amplification (sorry!;) : memsetting an 'int' to zero is not
                >>> guaranteed by the Standard to set the actual /value/ of that 'int'
                >>> to the integer zero. (But I think there's some debate about that;
                >>> I forget the details.) So what Jens is suggesting doesn't work.
                >>>
                >>> However, unsigned types are guaranteed to have pure binary
                >>> representations , which means that memsetting an 'unsigned int' to
                >>> zero /will/ set its value to the unsigned integer zero! Ditto
                >>> 'unsigned char', 'unsigned long', et cetera. (And I think ditto
                >>> the new C99 fixed-width types: 'int32_t' and 'int_least8_t' and
                >>> friends.)
                >>>
                >>> I often use 'calloc' in image processing; for example, to get
                >>> a grayscale image of size w*h initialized to black, I'll write
                >>>
                >>> unsigned char *im = calloc(w*h, 1);
                >>>
                >>> So 'calloc' does have its uses; they're just rare.[/color][/color]
                >[color=green]
                >> I had gone through three books (just basics ones) but didnt find
                >> memset memmove ect can any one plz explain it .[/color]
                >
                > They must be pretty crappy books then. memset is a function for setting
                > every byte in a range of bytes to a specific value. memmove is for
                > moving a range of bytes into another location in memory. Does this
                > answer your question?[/color]

                and there is also memcpy() in the same group. Read all their
                specifications.

                --
                Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                Available for consulting/temporary embedded and systems.
                <http://cbfalconer.home .att.net> USE worldnet address!


                Comment

                • Stan Milam

                  #38
                  Re: far pointers

                  Harsimran wrote:
                  [color=blue]
                  > Can any one explain what are far pointers and what is the difference
                  > between malloc and calloc .Which is better ?[/color]

                  Others have answered very well about the difference between malloc and
                  calloc, but like most C programmers who are anal rententive (myself
                  included) did not answer your question about far pointers. The only
                  reference they gave you was that far pointers were off-topic since they
                  are not ANSI C. Here is the long and short of it.

                  In the bad old MS-DOS days the X86 processors used 16 bit registers and
                  pointers which only allowed addressing 64K of memory. Intel included a
                  cludge where two registers (pointers) could be used to address 1 meg of
                  memory. The two pointers were called segment and offset respectively.
                  To address 1 meg the segment address was internally expanded to 20 bits
                  and shifed left four bits. The offset address was then added to render
                  the final 20 bit address. Ultimately it was a real mess and fostered a
                  lot of unportable code. I was so much happier when I started
                  programming in UNIX and did not have to mess with memory models and far
                  pointers.

                  --
                  Regards,
                  Stan Milam.
                  -----------------------------------------------------------------------------
                  My life is a song. I live to be sung. I sing with all my heart - John
                  Denver.
                  -----------------------------------------------------------------------------

                  Comment

                  Working...