In C ++, when a function is declared, the pointer and reference semantics are referenced. when defining a method prototype, refer.

Source: Internet
Author: User

A language, including syntax and semantics. Generally, computer books only talk about grammar rules. Over the past few years, I have found that there are too few books about semantics (maybe I haven't read it for a long time ). This article introduces some common semantics related to function declaration.

 

When a function is called, the parameter is pushed to the stack, and the return value is copied to the caller. If it is a simple type, things are simple, but it involves pointers or references, things will be more complicated, because in C/C ++, programmers are responsible for managing memory and resources, A good programmer must have a clear understanding of the time when each memory application and corresponding release occurs in his/her code.

 

For example, the following statement:

 

Struct mydate

{

Int year;

Int month;

Int Day;

};

 

Void getbirthday (mydate * pbirthday );

Who is responsible for applying for space and releasing space for pbirthday?

This problem is actually relatively simple, because the pointer itself is pressed to the stack when the parameter is pressed to the stack, and the address indicated by the pointer is not in the stack. It is clear that the caller is responsible for applying for memory, memory should be released by the caller.

 

If we change the function declaration, how can we understand if a pointer is returned?

Mydate * getbirthday (void );

 

In general, the function is responsible for applying for memory, and the caller is responsible for releasing.

 

But... But... What if getbirthday () is not implemented normally?

 

Static mydate mybirthday;

...

...

 

Mydate * getbirthday (void)

{

Return & mybirthday;

}

If the pointer returned by the caller Delete is used, the program behavior is uncertain. Hey hey, the so-called behavior is uncertain, that is, the program runs well on you, and the behavior will crash on the client.

I may ask why a pointer is returned? Why not directly return the mydate object?

When an object is returned, the compiler will generate an invisible temporary object for the caller to use.

Mydate getbirthday (void );

Compared with the declaration of the returned pointer, the structure will be copied once more in the C code, and the structure will be copied once more in the C ++ code. In some situations where efficiency is pursued, these redundant operations are hard to tolerate. In addition, if your object contains resource and memory management, resources and memory may be accidentally released during the analysis.

Some people say that it is the internal code of the company. Can the caller check whether the implementation is complete?

The problem is:

1. You ask others to view your implementation and increase the burden on others. You are digging holes for others;

2. What if your code is compiled into a library for others' use? What if your company does not want to disclose source code?

 

As a compromise, the function can be declared

Const mydate * getbirthday (void );

 

The const syntax is to tell the caller that this pointer is owned by me and you should not tamper with it.

 

 

Because the compiler automatically generates a copy statement when an object is passed, we usually pass object pointers or object references. The following lists some common semantics for your reference:

A Method accepts normal pointers, indicating that this method may modify the data pointed to by this pointer;
A Method accepts a constant pointer, indicating that this method promises not to modify the data pointed to by this pointer;
A method returns a common pointer, indicating that the caller is required to release the pointer;
A method returns a constant pointer, indicating that this method will handle the space indicated by this pointer by itself, and the caller does not need to worry about it;

A method that accepts common references indicates that this method may modify the referenced data;
A Method accepts constant references, indicating that this method promises not to modify the referenced data;

A normal reference is returned for a method, indicating that the method expects or allows the caller to modify the returned object;
A constant reference is returned for a method, indicating that the method does not allow the caller to modify the returned object;

 

 

These things are very trivial, and some people may say: with so many rules and regulations, coding is completely physical work.

 

But there is no way, we should note that the code is written to people, not to the compiler, remember to remember.

 

 

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.