Return Value -- difference between CString and pointer

Source: Internet
Author: User

Char * ReadLob (CString filename)

{

Char msgbody [1024];

Return msgbody

}

//////////////////////////////////////// //////////////////////////////////////// /// // <Difference

Int ReadLob (CString filename, char * node702_msgbody, int iSz)

{

Std: ifstream file; file. open (filename );

If (! File)

{Printf ("Unable to open file \ n"); return-1 ;}

Char pBuf [1024*4]; int iRead = 0, iCur = 0;

While (! File. eof ())

{

File. read (pBuf, sizeof (pBuf ));

IRead = file. gcount ();

Int iCpy = iRead + iCur> = iSz? ISz-iCur-1: iRead;

Strncpy (node702_msgbody + iCur, pBuf, iCpy); iCur + = iCpy;

If (iCur = sizeof (node702_msgbody)-1)

Break;

}

Node702_msgbody [iCur] = 0;

Return 0;

}

///////////////////////////

First, the pointer must be new before it can be used as the return value. If the array header in the function is not returned as a pointer, a problem may occur,

The reason why I did not encounter a problem is that there is no other code in it to destroy the previous stack space.

The CString type can be returned, and the CString can be protected.

The second method is recommended.

///////////////////////////

It is better to add iCur to read fields in segments.

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.