Code fails under vc8 - clearing a structure

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

    Code fails under vc8 - clearing a structure

    Hi, This code I inherited worked under VC7 now fails under VC8 with error:
    error C2664: 'memcpy' : cannot convert parameter 1 from
    'std::_Vector_i terator<_Ty,_Al loc>' to 'void *'



    vector <png_color> vPalette;
    ..
    ..
    ..
    memcpy(vPalette .begin(), palette, num_palette * sizeof png_color);

    png_color is part of the PNG graphics library so I can't change that.
    So, what is the best way to substitute the memcpy()?

    Thanks




  • Heinz Ozwirk

    #2
    Re: Code fails under vc8 - clearing a structure

    "Jack" <test@test.co m> schrieb im Newsbeitrag news:zMSdnYvrl5 h5uqzZRVnytA@pi pex.net...[color=blue]
    > Hi, This code I inherited worked under VC7 now fails under VC8 with error:
    > error C2664: 'memcpy' : cannot convert parameter 1 from
    > 'std::_Vector_i terator<_Ty,_Al loc>' to 'void *'
    >
    >
    >
    > vector <png_color> vPalette;
    > .
    > .
    > .
    > memcpy(vPalette .begin(), palette, num_palette * sizeof png_color);
    >
    > png_color is part of the PNG graphics library so I can't change that.
    > So, what is the best way to substitute the memcpy()?[/color]

    First of all you should be greatful that the new compiler has found such a nasty bug in that code.

    Basically you should replace that memcpy with a loop that assigns a new value to each element of the vector. Depending on what palette, num_palette etc. are std::copy or some other function from algorithm might be usefull.

    HTH
    Heinz

    Comment

    • Gavin Deane

      #3
      Re: Code fails under vc8 - clearing a structure


      Jack wrote:[color=blue]
      > Hi, This code I inherited worked under VC7 now fails under VC8 with error:
      > error C2664: 'memcpy' : cannot convert parameter 1 from
      > 'std::_Vector_i terator<_Ty,_Al loc>' to 'void *'
      >
      >
      >
      > vector <png_color> vPalette;
      > .
      > .
      > .
      > memcpy(vPalette .begin(), palette, num_palette * sizeof png_color);
      >
      > png_color is part of the PNG graphics library so I can't change that.
      > So, what is the best way to substitute the memcpy()?[/color]

      It looks like you've fallen foul of the fact that vector iterators
      *can* be implemented as pure pointers, but they need not be. To avoid
      falling into this trap again, never assume implementation details like
      that. begin() returns an iterator so use it where an iterator is
      expected. Pretend you don't know that internally that iterator *might*
      be a pointer.

      You haven't shown full code, so I'm going to assume the following:
      palette is an array of png_color objects
      the size of the array palette is num_palette
      the size of the vector vPalette is >= num_palette

      std::copy(palet te, palette + num_palette, vPalette.begin( ));

      should do what you want.

      Gavin Deane

      Comment

      • klaus hoffmann

        #4
        Re: Code fails under vc8 - clearing a structure

        Jack wrote:[color=blue]
        > Hi, This code I inherited worked under VC7 now fails under VC8 with error:
        > error C2664: 'memcpy' : cannot convert parameter 1 from
        > 'std::_Vector_i terator<_Ty,_Al loc>' to 'void *'
        >
        >
        >
        > vector <png_color> vPalette;
        > .
        > .
        > .
        > memcpy(vPalette .begin(), palette, num_palette * sizeof png_color);
        >
        > png_color is part of the PNG graphics library so I can't change that.
        > So, what is the best way to substitute the memcpy()?
        >
        > Thanks
        >[/color]

        as Heinz pointed out you might use std::assign

        hth
        Klaus

        Comment

        • Michiel.Salters@tomtom.com

          #5
          Re: Code fails under vc8 - clearing a structure

          Jack wrote:[color=blue]
          > Hi, This code I inherited worked under VC7 now fails under VC8 with error:
          > error C2664: 'memcpy' : cannot convert parameter 1 from
          > 'std::_Vector_i terator<_Ty,_Al loc>' to 'void *'
          >
          > vector <png_color> vPalette;
          > .
          > memcpy(vPalette .begin(), palette, num_palette * sizeof png_color);
          >
          > png_color is part of the PNG graphics library so I can't change that.
          > So, what is the best way to substitute the memcpy()?[/color]

          std::copy(palle te, pallete + num_palette, vPalette.begin( ));

          HTH,
          Michiel Salters.

          Comment

          • Jack

            #6
            Re: Code fails under vc8 - clearing a structure


            "Gavin Deane" <deane_gavin@ho tmail.com> wrote in message
            news:1144073448 .618399.73610@e 56g2000cwe.goog legroups.com...[color=blue]
            >
            > Jack wrote:[color=green]
            >> Hi, This code I inherited worked under VC7 now fails under VC8 with
            >> error:
            >> error C2664: 'memcpy' : cannot convert parameter 1 from
            >> 'std::_Vector_i terator<_Ty,_Al loc>' to 'void *'
            >>
            >>
            >>
            >> vector <png_color> vPalette;
            >> .
            >> .
            >> .
            >> memcpy(vPalette .begin(), palette, num_palette * sizeof png_color);
            >>
            >> png_color is part of the PNG graphics library so I can't change that.
            >> So, what is the best way to substitute the memcpy()?[/color]
            >
            > It looks like you've fallen foul of the fact that vector iterators
            > *can* be implemented as pure pointers, but they need not be. To avoid
            > falling into this trap again, never assume implementation details like
            > that. begin() returns an iterator so use it where an iterator is
            > expected. Pretend you don't know that internally that iterator *might*
            > be a pointer.
            >
            > You haven't shown full code, so I'm going to assume the following:
            > palette is an array of png_color objects
            > the size of the array palette is num_palette
            > the size of the vector vPalette is >= num_palette
            >
            > std::copy(palet te, palette + num_palette, vPalette.begin( ));
            >
            > should do what you want.[/color]

            Thanks, seems to work - compiles anyway :) .

            Is there a similar std substitution for memset or must I step through as
            Heinz suggested? ( I don' really know the std library)
            memset(pV, 0, nPal * sizeof png_color);

            fails with the same error.






            Comment

            • Jack

              #7
              Re: Code fails under vc8 - clearing a structure


              "Jack" <test@test.co m> wrote in message
              news:kc2dnT40wI CrrKzZRVnyqQ@pi pex.net...[color=blue]
              >
              > "Gavin Deane" <deane_gavin@ho tmail.com> wrote in message
              > news:1144073448 .618399.73610@e 56g2000cwe.goog legroups.com...[color=green]
              >>
              >> Jack wrote:[color=darkred]
              >>> Hi, This code I inherited worked under VC7 now fails under VC8 with
              >>> error:
              >>> error C2664: 'memcpy' : cannot convert parameter 1 from
              >>> 'std::_Vector_i terator<_Ty,_Al loc>' to 'void *'
              >>>
              >>>
              >>>
              >>> vector <png_color> vPalette;
              >>> .
              >>> .
              >>> .
              >>> memcpy(vPalette .begin(), palette, num_palette * sizeof png_color);
              >>>
              >>> png_color is part of the PNG graphics library so I can't change that.
              >>> So, what is the best way to substitute the memcpy()?[/color]
              >>
              >> It looks like you've fallen foul of the fact that vector iterators
              >> *can* be implemented as pure pointers, but they need not be. To avoid
              >> falling into this trap again, never assume implementation details like
              >> that. begin() returns an iterator so use it where an iterator is
              >> expected. Pretend you don't know that internally that iterator *might*
              >> be a pointer.
              >>
              >> You haven't shown full code, so I'm going to assume the following:
              >> palette is an array of png_color objects
              >> the size of the array palette is num_palette
              >> the size of the vector vPalette is >= num_palette
              >>
              >> std::copy(palet te, palette + num_palette, vPalette.begin( ));
              >>
              >> should do what you want.[/color]
              >
              > Thanks, seems to work - compiles anyway :) .
              >
              > Is there a similar std substitution for memset or must I step through as
              > Heinz suggested? ( I don' really know the std library)
              > memset(pV, 0, nPal * sizeof png_color);
              >[/color]

              Should read:
              memset(vPalette .begin(), 0, nPal * sizeof png_color);


              Comment

              • Jack

                #8
                Re: Code fails under vc8 - clearing a structure


                "Jack" <test@test.co m> wrote in message
                news:kc2dnT40wI CrrKzZRVnyqQ@pi pex.net...[color=blue]
                >
                > "Gavin Deane" <deane_gavin@ho tmail.com> wrote in message
                > news:1144073448 .618399.73610@e 56g2000cwe.goog legroups.com...[color=green]
                >>
                >> Jack wrote:[color=darkred]
                >>> Hi, This code I inherited worked under VC7 now fails under VC8 with
                >>> error:
                >>> error C2664: 'memcpy' : cannot convert parameter 1 from
                >>> 'std::_Vector_i terator<_Ty,_Al loc>' to 'void *'
                >>>
                >>>
                >>>
                >>> vector <png_color> vPalette;
                >>> .
                >>> .
                >>> .
                >>> memcpy(vPalette .begin(), palette, num_palette * sizeof png_color);
                >>>
                >>> png_color is part of the PNG graphics library so I can't change that.
                >>> So, what is the best way to substitute the memcpy()?[/color]
                >>
                >> It looks like you've fallen foul of the fact that vector iterators
                >> *can* be implemented as pure pointers, but they need not be. To avoid
                >> falling into this trap again, never assume implementation details like
                >> that. begin() returns an iterator so use it where an iterator is
                >> expected. Pretend you don't know that internally that iterator *might*
                >> be a pointer.
                >>
                >> You haven't shown full code, so I'm going to assume the following:
                >> palette is an array of png_color objects
                >> the size of the array palette is num_palette
                >> the size of the vector vPalette is >= num_palette
                >>
                >> std::copy(palet te, palette + num_palette, vPalette.begin( ));
                >>
                >> should do what you want.[/color]
                >
                > Thanks, seems to work - compiles anyway :) .
                >
                > Is there a similar std substitution for memset or must I step through as
                > Heinz suggested? ( I don' really know the std library)
                > memset(pV, 0, nPal * sizeof png_color);
                >[/color]

                Should read:
                memset(vPalette .begin(), 0, nPal * sizeof png_color);



                Comment

                • Gavin Deane

                  #9
                  Re: Code fails under vc8 - clearing a structure


                  Jack wrote:[color=blue]
                  > "Jack" <test@test.co m> wrote in message[color=green]
                  > > Is there a similar std substitution for memset or must I step through as
                  > > Heinz suggested? ( I don' really know the std library)[/color][/color]

                  Get yourself a copy of this



                  It widely recommended and worth it's weight in gold.
                  [color=blue][color=green]
                  > > memset(pV, 0, nPal * sizeof png_color);
                  > >[/color]
                  >
                  > Should read:
                  > memset(vPalette .begin(), 0, nPal * sizeof png_color);[/color]

                  Not quite, because memset directly sets the values of individual bytes
                  of memory without caring whether those bytes values make sense for the
                  type of data. But you should avoid that sort of low level operation
                  unless you absolutely need it. Try

                  std::fill_n(vPa lette.begin(), nPal, png_color());

                  to fill the first nPal elements of the vector with default-constructed
                  png_color objects.

                  Gavin Deane

                  Comment

                  Working...