How can I transform source range to destination range that is thesame as source?

Discussion in 'C++' started by Lambda, Jul 16, 2008.

  1. Lambda

    Lambda Guest

    I'd like to change all the character in a array to lower case.
    I find transform() is convenient.

    Because the array is large, I don't want to define another array. I
    tried:

    transform(buffer, buffer + size, buffer, tolower);

    From the output, I find it works.
    But when I compile it with Visual C++,
    I find a warning that says 'Function call with parameters that may be
    unsafe'.

    I shouldn't use transform that way?
    I must define another array to store the result or
    do it without transform?
     
    Lambda, Jul 16, 2008
    #1
    1. Advertising

  2. Re: How can I transform source range to destination range that isthe same as source?

    Lambda a écrit :
    > I'd like to change all the character in a array to lower case.
    > I find transform() is convenient.
    >
    > Because the array is large, I don't want to define another array. I
    > tried:
    >
    > transform(buffer, buffer + size, buffer, tolower);
    >
    > From the output, I find it works.
    > But when I compile it with Visual C++,
    > I find a warning that says 'Function call with parameters that may be
    > unsafe'.
    >
    > I shouldn't use transform that way?


    This is a MS feature called check iterator. The compiler warns you that
    you may write out of bounds. Are you sure the size is correct ?

    > I must define another array to store the result or
    > do it without transform?


    If you are sure you are right you can silence the compiler by defining:
    #define _SECURE_SCL 0

    Or using a Windows specific extension:
    transform(buffer,buffer+size,
    stdext::checked_array_iterator<char*>(buffer, size),
    tolower);



    --
    Michael
     
    Michael DOUBEZ, Jul 16, 2008
    #2
    1. Advertising

  3. Lambda

    James Kanze Guest

    On Jul 16, 12:43 pm, Lambda <> wrote:
    > I'd like to change all the character in a array to lower case.
    > I find transform() is convenient.


    > Because the array is large, I don't want to define another array. I
    > tried:


    > transform(buffer, buffer + size, buffer, tolower);


    > From the output, I find it works.


    Depending on the includes, it's likely to not even compile. And
    if it does, the results have undefined behavior.

    > But when I compile it with Visual C++,
    > I find a warning that says 'Function call with parameters that
    > may be unsafe'.


    Which one?

    > I shouldn't use transform that way?


    No.

    > I must define another array to store the result or
    > do it without transform?


    No, but you do have to provide a functional object that is
    unambiguous and that works correctly on the data type. You also
    should be using std::vector instead of whatever; it would make
    life a lot easier.

    And finally, of course, you should be aware that upper to lower
    case is not necessarily a one to one mapping, and that it is
    very locale dependent.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 16, 2008
    #3
    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. Replies:
    3
    Views:
    930
    Roedy Green
    Jan 28, 2008
  2. laredotornado
    Replies:
    1
    Views:
    81
    Rick DeNatale
    Jan 28, 2008
  3. Thomas Watson Steen
    Replies:
    4
    Views:
    341
    Sisyphus
    Oct 4, 2004
  4. Peng Yu
    Replies:
    23
    Views:
    283
    Peter J. Holzer
    Jun 12, 2010
  5. John Reye
    Replies:
    28
    Views:
    1,375
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page