are arrays contiguous in memory?

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

    are arrays contiguous in memory?

    I looked at the addresses in an 'array<>' during debug and noticed that the
    addresses were contiguous. Is this guaranteed, or just something it does if
    it can?

    [==P==]

    PS = VS C++.NET 2005 Express using clr:/pure syntax


  • Laurent Lequenne

    #2
    Re: are arrays contiguous in memory?

    I guess that if you lock the array, the array will be contiguous to use a
    pointer as it is the only safe way you can access the array through a
    pointer :-)


    "Peteroid" <peter_oliphant @msn.com> a écrit dans le message de
    news:%23oGh3h$A GHA.608@TK2MSFT NGP12.phx.gbl.. .[color=blue]
    > I looked at the addresses in an 'array<>' during debug and noticed that[/color]
    the[color=blue]
    > addresses were contiguous. Is this guaranteed, or just something it does[/color]
    if[color=blue]
    > it can?
    >
    > [==P==]
    >
    > PS = VS C++.NET 2005 Express using clr:/pure syntax
    >
    >[/color]


    Comment

    • Carl Daniel [VC++ MVP]

      #3
      Re: are arrays contiguous in memory?

      Peteroid wrote:[color=blue]
      > I looked at the addresses in an 'array<>' during debug and noticed
      > that the addresses were contiguous. Is this guaranteed, or just
      > something it does if it can?[/color]

      Managed arrays are always contiguous, but you can only observe/take
      advantange of that fact by getting a pinning pointer into the array.
      Naturally, if it's an array of reference type, then the contiguous array
      will contain only pointers to the contained objects, which in all likelihood
      will not be contiguous.

      -cd


      Comment

      • Willy Denoyette [MVP]

        #4
        Re: are arrays contiguous in memory?


        "Peteroid" <peter_oliphant @msn.com> wrote in message
        news:%23oGh3h$A GHA.608@TK2MSFT NGP12.phx.gbl.. .[color=blue]
        >I looked at the addresses in an 'array<>' during debug and noticed that the
        >addresses were contiguous. Is this guaranteed, or just something it does if
        >it can?
        >
        > [==P==]
        >
        > PS = VS C++.NET 2005 Express using clr:/pure syntax
        >[/color]

        Yes.

        Willy.


        Comment

        • Willy Denoyette [MVP]

          #5
          Re: are arrays contiguous in memory?


          "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
          wrote in message news:OgQAIoABGH A.2788@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Peteroid wrote:[color=green]
          >> I looked at the addresses in an 'array<>' during debug and noticed
          >> that the addresses were contiguous. Is this guaranteed, or just
          >> something it does if it can?[/color]
          >
          > Managed arrays are always contiguous, but you can only observe/take
          > advantange of that fact by getting a pinning pointer into the array.
          > Naturally, if it's an array of reference type, then the contiguous array
          > will contain only pointers to the contained objects, which in all
          > likelihood will not be contiguous.
          >
          > -cd
          >
          >[/color]

          Not sure what you mean with "take/advantage", in managed code there is no
          need to pin pointers unless you need to pass the array or one of its
          elements to unmanaged code. But pinned or not, managed array's are always
          contigious, just like non managed array's.

          Willy.


          Comment

          • Carl Daniel [VC++ MVP]

            #6
            Re: are arrays contiguous in memory?

            Willy Denoyette [MVP] wrote:[color=blue]
            > Not sure what you mean with "take/advantage", in managed code there
            > is no need to pin pointers unless you need to pass the array or one
            > of its elements to unmanaged code. But pinned or not, managed array's
            > are always contigious, just like non managed array's.
            >[/color]

            take-advantage-of as in use pointer arithmetic to access array elements.
            Not of much (any?) value unless you're writing unmanaged code.

            --cd


            Comment

            • Rodrigo Corral [MVP]

              #7
              Re: are arrays contiguous in memory?

              It could be very useful to use pointer arithmetic, for performance reasons,
              in managed code, at least in C#. I guest that is the same in managed
              C++/CLI.
              For example:



              --
              Un saludo
              Rodrigo Corral González [MVP]

              FAQ de microsoft.publi c.es.vc++





              Comment

              • Nishant Sivakumar

                #8
                Re: are arrays contiguous in memory?

                Yes, arrays are ref types and the GC always allocates memory in contiguous
                blocks.

                --
                Regards,
                Nish [VC++ MVP]


                "Peteroid" <peter_oliphant @msn.com> wrote in message
                news:%23oGh3h$A GHA.608@TK2MSFT NGP12.phx.gbl.. .[color=blue]
                >I looked at the addresses in an 'array<>' during debug and noticed that the
                >addresses were contiguous. Is this guaranteed, or just something it does if
                >it can?
                >
                > [==P==]
                >
                > PS = VS C++.NET 2005 Express using clr:/pure syntax
                >[/color]


                Comment

                • Nishant Sivakumar

                  #9
                  Re: are arrays contiguous in memory?

                  >>Managed arrays are always contiguous, but you can only observe/take
                  advantange of that fact by getting a pinning pointer into the array. <<

                  An interior pointer would suffice too, Carl.

                  --
                  Regards,
                  Nish [VC++ MVP]


                  "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
                  wrote in message news:OgQAIoABGH A.2788@TK2MSFTN GP14.phx.gbl...[color=blue]
                  > Peteroid wrote:[color=green]
                  >> I looked at the addresses in an 'array<>' during debug and noticed
                  >> that the addresses were contiguous. Is this guaranteed, or just
                  >> something it does if it can?[/color]
                  >
                  > Managed arrays are always contiguous, but you can only observe/take
                  > advantange of that fact by getting a pinning pointer into the array.
                  > Naturally, if it's an array of reference type, then the contiguous array
                  > will contain only pointers to the contained objects, which in all
                  > likelihood will not be contiguous.
                  >
                  > -cd
                  >
                  >[/color]


                  Comment

                  • Willy Denoyette [MVP]

                    #10
                    Re: are arrays contiguous in memory?


                    "Rodrigo Corral [MVP]" <corral_glez@ho tmail.com> wrote in message
                    news:eiqYLFLBGH A.976@TK2MSFTNG P15.phx.gbl...[color=blue]
                    > It could be very useful to use pointer arithmetic, for performance
                    > reasons, in managed code, at least in C#. I guest that is the same in
                    > managed C++/CLI.
                    > For example:
                    > http://msdn.microsoft.com/library/de...rp11152001.asp
                    >
                    >
                    > --
                    > Un saludo
                    > Rodrigo Corral González [MVP]
                    >
                    > FAQ de microsoft.publi c.es.vc++
                    > http://rcorral.mvps.org
                    >[/color]


                    Beware that the sample in the article uses pointer arithmetic (C# unsafe
                    code) to directly access an unmanaged array (a bitmap is unmanaged data!!),
                    there is no need to pin here.
                    I see no reason whatsoever to use this to access managed arrays.

                    Willy.



                    Comment

                    • Willy Denoyette [MVP]

                      #11
                      Re: are arrays contiguous in memory?

                      Do you guy's know any "arrays" that aren't contigious?

                      Willy.

                      "Nishant Sivakumar" <nish@nospam.as ianetindia.com> wrote in message
                      news:%2339m%23M LBGHA.516@TK2MS FTNGP15.phx.gbl ...[color=blue]
                      > Yes, arrays are ref types and the GC always allocates memory in contiguous
                      > blocks.
                      >
                      > --
                      > Regards,
                      > Nish [VC++ MVP]
                      >
                      >
                      > "Peteroid" <peter_oliphant @msn.com> wrote in message
                      > news:%23oGh3h$A GHA.608@TK2MSFT NGP12.phx.gbl.. .[color=green]
                      >>I looked at the addresses in an 'array<>' during debug and noticed that
                      >>the addresses were contiguous. Is this guaranteed, or just something it
                      >>does if it can?
                      >>
                      >> [==P==]
                      >>
                      >> PS = VS C++.NET 2005 Express using clr:/pure syntax
                      >>[/color]
                      >
                      >[/color]


                      Comment

                      • Willy Denoyette [MVP]

                        #12
                        Re: are arrays contiguous in memory?


                        "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
                        wrote in message news:e9cR9SDBGH A.4004@TK2MSFTN GP15.phx.gbl...[color=blue]
                        > Willy Denoyette [MVP] wrote:[color=green]
                        >> Not sure what you mean with "take/advantage", in managed code there
                        >> is no need to pin pointers unless you need to pass the array or one
                        >> of its elements to unmanaged code. But pinned or not, managed array's
                        >> are always contigious, just like non managed array's.
                        >>[/color]
                        >
                        > take-advantage-of as in use pointer arithmetic to access array elements.
                        > Not of much (any?) value unless you're writing unmanaged code.
                        >
                        > --cd
                        >
                        >[/color]

                        Carl,

                        Sorry to be dense, but when talking about advantage, you mean advantage in
                        terms of what and compared to what? I don't see any advantage to use pointer
                        arithmetic to access "managed" array elements over let's say indexed access,
                        I'm I missing something?

                        Willy.



                        Comment

                        • Carl Daniel [VC++ MVP]

                          #13
                          Re: are arrays contiguous in memory?

                          Willy Denoyette [MVP] wrote:[color=blue]
                          > Carl,
                          >
                          > Sorry to be dense, but when talking about advantage, you mean
                          > advantage in terms of what and compared to what? I don't see any
                          > advantage to use pointer arithmetic to access "managed" array
                          > elements over let's say indexed access, I'm I missing something?[/color]

                          It's a figure of speach - don't put too much weight in it!

                          The only advantage would be to use it with existing code that does pointer
                          arithmetic when accessing an array.

                          In real terms, there is no advantage, since in C, a[n] is equivalent to
                          *(a+n) by definition.

                          -cd


                          Comment

                          • Carl Daniel [VC++ MVP]

                            #14
                            Re: are arrays contiguous in memory?

                            Willy Denoyette [MVP] wrote:[color=blue]
                            > Do you guy's know any "arrays" that aren't contigious?[/color]

                            Matrix arithmetic libraries typically supply some kind of sparse array
                            that's not contiguous, but those are definitely special purpose things. It
                            would seem very silly to make a general purpose array non-contiguous.

                            -cd


                            Comment

                            • Willy Denoyette [MVP]

                              #15
                              Re: are arrays contiguous in memory?


                              "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
                              wrote in message news:%23ujQmmLB GHA.4076@TK2MSF TNGP14.phx.gbl. ..[color=blue]
                              > Willy Denoyette [MVP] wrote:[color=green]
                              >> Carl,
                              >>
                              >> Sorry to be dense, but when talking about advantage, you mean
                              >> advantage in terms of what and compared to what? I don't see any
                              >> advantage to use pointer arithmetic to access "managed" array
                              >> elements over let's say indexed access, I'm I missing something?[/color]
                              >
                              > It's a figure of speach - don't put too much weight in it!
                              >
                              > The only advantage would be to use it with existing code that does pointer
                              > arithmetic when accessing an array.
                              >
                              > In real terms, there is no advantage, since in C, a[n] is equivalent to
                              > *(a+n) by definition.
                              >
                              > -cd
                              >
                              >[/color]

                              Carl,

                              Agreed, all I want is to make sure people don't start to believe that using
                              pointer arithmetic has performance advantages when accessing arrays
                              (elements). I know a lot of C# (and some C++) developers believe that there
                              is such advantage when using unsafe constructs, but it's most often not the
                              case, worse, they start to pin arrays for a long period of time, effectively
                              disturbing the GC activity which results in reduced overall performance.

                              Willy.


                              Comment

                              Working...