Implicit string Conversion, and sizeof, strlen

Source: Internet
Author: User

Problem Source: http://cxwangyi.blogspot.com/2010/05/hadoop-pipes-is-incompatible-with.html

The key part is as follows:

In your mapper you have the line:
Context. emit ("", "apple/norange/0 banana/tpapaya ");
The signature for the emit method is:
TaskContext: emit (const std: string &, const std: string &);

Since you pass the value as a string literal, C ++ must convert it to an std: string. Following C ++'s implict conversion rules, it does so uSing std: string (const char *),Which as defined by the standard looks for the first occurence of a null character to determine the length of the string. so, when emit gets called, it is being invoked with an std: string whose last character is the 'E' in orange. not Hadoop's fault at all. you 'd get the same result if you passed that literal to any function that expects an std: string (or for that matter pretty much any function where all you passed was that literal ). try this:

   1: #include 

   2: #include 

   3: void print(const std::string& aString) {

4: std: cout
       
   5: }

   6: int main(int argc, char** argv) {

   7: print("apple/norange/0banana/tpapaya");

8: std: cout
        
   9: }

You'll get the same disappointing result. If you change your code as follows it'll work fine:

Void map (HadoopPipes: MapContext & context ){

Const char valueString [] = "apple/norange/0 banana/tpapaya ";

Context. emit ("", std: string (valueString, sizeof (valueString)-1 ));

}

All this does is make sure you give it a string which is properly length terminated.

Note: we need to change the default implicit conversion of string.

Http://www.cplusplus.com/reference/string/string/string/

String (const char * s, size_t n );

Ntent is initialized to a copy of the string formed by the firstNCharacters in the array of characters pointedS.

String (const char * s );
Content is initialized to a copy of the string formed by the null-terminated character sequence (C string) pointed S. The length of the caracter sequence is determined by the first occurrence of a null character (as determined by traits. length (s )). this version can be used to initialize a string object using String Literal constant.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.