A simple problem solved in three days

Source: Internet
Author: User

There are always problems when debugging a function over the past few days;

 

To enable the computer to send data through the serial port;

 

The data format is defined; PELCOD protocol;

 

/* BYTE messagesend [] = {0xff, 0x01, 0x00, 0x08, 0x00, 0xff, 0x08 }; <br/> BYTE * test = messagesend; // Question 1: Can a pointer be used? messagesend ends when the messagesend pointer encounters '/0; (0x00 in ANSI is equivalent to '/0' Unicode is not) <br/> WriteToPort (messagesend, 7 );BYTE messagesend [] = {0xff, 0x01, 0x00, 0x08, 0x00, 0xff, 0x08 };
WriteToPort (messagesend, 7); // function prototype: WriteToPort (BYTE * pData, UINT len) messagesend is a BYTE pointer;
No warning is reported;
Custom functions:
BYTE * CPelcoD: GetMasseg ()
{
BYTE message [] = {0xff, 0x01, 0x00, 0x08, 0x00, 0xff, 0x08 };
// BYTE * object = message;
Return message;
} Appears: warning c00002: returning address of local variable or temporary

 

Shenzhen-why did it appear in the above call?
Beijing you return the address of the local variable
Beijing is still in scope.
The message [] in Beijing getmasseg () is destroyed when this function exists...
Add one in Beijing:
Byte * object = message;
Why not?
Why does Beijing report an error?
Shenzhen
Beijing um
Guangdong-the returned pointer is correct. Returning the address pointing to the local variable may be faulty (the local variable will be released )~~
Guangdong-the returned pointer is correct. Returning the "Address of the local variable" may cause a problem (the local variable will be released )~~
Although the Guangdong compiler has no warning, it is still wrong ~~ You cheated the compiler and yourself ~~
Shenzhen-side dish ?? It is not clear about the dishes in this place. Can you elaborate?
Every variable in Beijing has a scope. do you understand this?
Shenzhen-will the function be destroyed if the pointer of the dish goes out?
The scope of Beijing local variables is only within the scope of this function.
Shenzhen-cakeen
Beijing * The message [] function will be invalid when the getmessage function is generated ..
Shenzhen-this is a problem if it is not "return ".
Beijing * although you have returned a pointer pointing to message [], it is possible that the message [] address has been used by someone else. In this case, the caller may have problems.
Shenzhen-Why is there no warning when the pointer pointing to him is returned?
Guangdong-there is no problem with the returned pointer. The address of the variable you returned (although equivalent to the pointer), the compiler knows that its memory will be released because the variable is partial, so it can warn you that there is a problem, and the returned pointer does not care that the content pointed to by the pointer has been allocated or released...

Returns a pointer. The Compiler does not check whether the memory pointed to by the pointer has been released, because the pointer can be changed dynamically at any time ~~ Therefore, during compilation, it is impossible to know whether the content pointed to by the pointer has been released... // Member function <br/> byte * cmymessage: mygetmessage (byte address, byte command1, byte command2, byte data1, byte data2) <br/>{< br/>:: afxmessagebox ("3"); <br/> byte STX = 0xff; <br/> //: afxmessagebox ("1 "); <br/> If (address <1 & Address> 256) <br/> afxmessagebox ("protocol pelco D support 256 devices only! "); <Br/> //: afxmessagebox (" 1 "); <br/> address = address; <br/> //:: afxmessagebox ("1"); <br/> data1 = data1; <br/> data2 = data2; <br/> command1 = command1; <br/> command2 = command2; <br/> //: afxmessagebox ("1"); <br/> checksum = STX ^ address ^ command1 ^ command2 ^ data1 ^ data2; </P> <p> // byte * message; <br/> //: afxmessagebox ("1 "); <br/> message = new byte [7]; <br/> message [0] = STX; <br/> message [1] = address; <br/> message [2] = data1; <br/> message [3] = data2; </P> <p> message [4] = command1; <br/> message [5] = command2; <br/> message [6] = checksum; </P> <p> return message; <br/>}Original call

BYTE * CPelcoD: MyCameraScan (BYTE deviceAddress) <br/>{< br/> //: AfxMessageBox (AutoManualScan); <br/> // BYTE m_byte = AutoManualScan; <br/> // if (scan = Auto) <br/> // m_byte = AutoManualScan + Sense; </p> <p>/* test <br/> BYTE m_byte = AutoManualScan; <br/> // if (scan = Auto) <br/> m_byte = AutoManualScan + Sense; </p> <p> */</p> <p>: AfxMessageBox ("2 "); <br/> BYTE * ObjectMessge = m_message-> MyGetMessage (0xff, 0xf0, 0xf1, 0xf2, 0xf3); </p> <p>: AfxMessageBox ("2 "); <br/> return ObjectMessge; <br/> // test <br/>/* BYTE message [] = {0xff, 0x01, 0x00, 0x08, 0x00, 0xff, 0x08 }; <br/> BYTE * object = message; <br/> return object; */<br/> // return message. getMessage (deviceAddress, m_byte, 0 x, 0 x); <br/>}Second-level call

 

Void cyuntaidlg: onconfig2 () <br/>{</P> <p> cstring temp; <br/> // afxmessagebox ("1 "); <br/> // m_pelcod.mycamerascan (byte) 0xff); problem point; </P> <p> // writetoport (m_pelcod.mycamerascan (byte) 0xff), 7 ); </P> <p> // Test2 <br/> writetoport (m_pelcod.getmasseg (), 7 ); </P> <p> // how to obtain an unsigned char array in test1 VC; <br/>/* byte messagesend [] = {0xff, 0x01, 0x00, 0x08, 0x00, 0xff, 0x08}; <br/> byte * test = messagesend; // Question 1: Can I use a pointer; messagesend pointer ends when '/0' is encountered; (0x00 in ANSI is equivalent to'/0' Unicode is not) <br/> writetoport (messagesend, 7 ); // send data to the serial port; */</P> <p>}Is the outer call point;

 

The problem is the return value;

Returns a pointer to a local variable. After the function is called, the local variable does not exist. The pointer to the pointer may be changed elsewhere in the program.

Continue to practice...

Function return value: vc cannot be an array and can be replaced by a pointer;

Appendix:

The compiler does not check (if the return value of the function is a pointer), the pointer may point somewhere else after the return value;

Int a [] = {1, 2, 3 };

& A: The first address of the array. a is the first address of the array's first element.

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.