The pointers and references in malloc and new,c++, the difference between c++struct and class

Source: Internet
Author: User
Tags define null

I. The difference between struct and class

1. struct and class are not very different

By default, the default permissions for class are private, and the struct defaults to public (common).

2. However, in C + +, the struct has been extended, and now it is not just a data structure that contains a different datatype, it includes more functionality.

Two. The difference between malloc and new,delete and free

1.malloc and free are functions in the library function, when the call is to add a header file, delete and new are operators in C + +

The 2.malloc return value is a void* pointer, and new returns a pointer of type

The constructor is not called when 3.malloc is created, and free does not call destructors, but new and delete automatically call

4.delete cannot be released repeatedly, but a null pointer can be released.

Three. Pointers and references

"A. Citation"

1. In layman's parlance, the reference is to take an alias

2. References must be initialized, cannot define null references, "Wild references", "dangling references" are present (data referencing heap memory should not be used when heap memory is freed)

3. The reference can only be one-time and cannot be changed (once the reference succeeds, he is a variable)

4. A reference can also be used as an argument to a function, which refers to an object that is the argument of the function, and the reference can achieve the effect of the pointer

A. Sharing variables between functions (pointers can also)

B. Improve the efficiency of the transfer of parameters (higher than the pointer)

C. When parameters are used,

Recommendation: Do not use pointers if you can use references, but references can not replace pointers

5. A reference can also be used as the return value of a function, but it must never return a reference to a local variable

"B. Hands"

1. The pointer can be used in these three cases: 1. The function pointer shares the variable 2. Use heap memory 3. Optimization of the parameter 4. can access hardware memory

2. What to look for when using pointers

1. Class when a pointer variable is defined

2. Address that does not return a local variable

3. When the space pointed to by the pointer is released, even if the empty

3. Const-related pointers

Const INT* P: Cannot modify the memory he points to by *p

int const* p cannot modify the memory it points to by *p

int * Const P: Cannot change P

   const int * Const P:P cannot be changed, *p cannot change memory

The pointers and references in malloc and new,c++, the difference between c++struct and class

Related Article

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.