C + + Learning Resources

Source: Internet
Author: User

1. Structure Size

int *pi = new INT[12]; pi is purely a pointer variable, it is a pointer to 4 bytes in a 32-bit environment, while the IA in int ia[]={0, 1, 2}, is a number group, although its value is the address of the first element, but it represents the entire array. There are three elements in the array, each int type element occupies 4 byte, altogether is 12 bytes, pi requests the memory, is the entire memory block's first address, *PI represents is the first element, each element is the int type, certainly is 4, if is char *pi = new CHAR[12];

cout<< "*PI:" <<sizeof (*PI)//The result should be 1

2. pointers and references

A reference is a pointer without a pointer syntax. As with pointers, references provide indirect access to objects.

When passing the pointer, we can modify what it points to outside by the pointer. However, it is not possible to modify the object that the external pointer points to. For example, passing an external pointer to a function to allocate space, you must pass a pointer or a reference to the pointer.

int x = 0;

int &a (int &i)

{

i =-1;

return x;

}

Main ()

{

int j=10;

A (j) = 100;

This time J = -1,x = 100.

Strange macro Definition

(1) #define for if (0); else for

If you add the title of the sentence, then it can be, that is, I scoped to the else.

for (int i=0;i<90;i++)

{

...;

}

for (int i=0;i<90;i++)

{

...;

}//VC wouldn't have an error.

3. Reference vs. pointer comparison:

(1) A reference must be initialized at the time it is created, referring to a valid object, and the pointer does not have to be initialized when it is defined, and can be assignable anywhere after the definition.

(2) There is no null reference, the reference must be associated with a legitimate storage unit, and the pointer can be null.

(3) Once the reference is initialized to point to an object, it cannot be changed to a reference to another object, and the pointer can be changed to point to another object at any time. Assigning a value to a reference does not change its binding relationship with the original object.

(4) The creation and destruction of references do not invoke the copy constructor of the class

(5) At the language level, the use of references is the same as that of objects; at the binary level, references are generally implemented through pointers, except that the compiler has helped us to complete the conversion.

In general: References have both the efficiency of pointers and the convenience and intuition of variable use.

1 Value Transfer

void Func (int x)

{

x=x+10;//modifies the n copy x in the stack

}

int n=0;

Func (n);

cout<< "n=" <<n<<endl;//n=0

2 pointer delivery

void Func2 (int *x)

{

(*x) = (*x) +10;//Modify the value of the memory cell pointed to by the pointer x

}

int n=0;

Func (&n);

cout<< "n=" <<n<<endl;//n=10

3. Reference delivery

void Func3 (int &x)

{

x=x+10;//modifies the object n that X refers to

}

int n=0;

FUNC3 (n);

cout<< "n=" <<n<<endl;//n=10

4. function Pointer

When registering a callback function, we often use the function pointer. The C++/C connector must put the function's invocation statement on the connector, so the function address must be determined at compile time. That is, when the compiler generates code for the function.

the typedef int (* funcptr) (const char*) defines the type of a function pointer

There are 4 types of member functions for a class: The Inline,virtual,static,normal.inline function expands at run, although the language allows its address, but it does not make much sense. The address of the virtual member function refers to its position in the vtable; the address of the static member function and the address of the normal global function are indistinguishable; The address of the ordinary member function and the address of the general function are no different, which is the real address of the function code in memory. But since its invocation is bound to an object that is really real, it is particularly true whether its function pointer is declared or its address is obtained.

#include <iostream.h>
Class CTest
{
Public
void f (void)//normal member function
{
cout<< "Ctest::f ()" <<endl;
}
static void G (void)//static member functions
{
cout<< "Ctest::g ()" <<endl;

}
virtual void H (void)//virtual member function
{
cout<< "Ctest::h ()" <<endl;
}
Private
};

void Main ()
{
typedef void (*GFPTR) (void);//define a global function pointer type
Gfptr fp=ctest::g;//to take a static member function address is similar to the address of a global function
FP ();//Call class static member function via function pointer
typedef void (CTest::* memfuncptr) (void);//Declare class member function pointer type
Memfuncptr mfp_1=&ctest::f;//declare member function pointer variable and initialize
Memfuncptr mfp_2=&ctest::h;//attention to the method of obtaining member function address
CTest Theobj;
(theobj.*mfp_1) ()//Use object and member function pointers to call member functions
(theobj.*mfp_2) ();
CTest *ptest=&theobj;//Use object pointers and member function pointers to invoke member functions
(ptest->*mfp_1) ();
(ptest->*mfp_2) ();

}
In fact, the code body of any member function exists independently of the object of the class, except that the Non-static member function needs to establish a binding relationship with the specific object (that is, the this pointer) at the time of invocation, c++/ The C compiler eventually converts all the member functions to the global function after name-mangling, and adds an incoming parameter this as the first parameter for all objects of the owning class. So the address of the member function is actually the address of these global functions.

5. Memory Management

How memory is allocated:

(1) Distribution from the static storage area. The memory is already allocated at the time of the program (that is, it has been addressed), which exists throughout the running of the program. such as global variables, static variable degrees, and so on.

(2) created on the stack. During function execution, the storage units of local variables (including formal parameters) within a function are created on the stack, and the storage units are automatically released when the function ends (stack clearing stacks). Stack memory allocation operations are placed within the processor's instruction set, are highly efficient, and do not have the risk of failure, but the allocated memory capacity is limited.

(3) Allocation from the heap, also known as dynamic memory allocation. The program uses malloc or new to request any amount of memory during the run, and the programmer has the right time to release the memory (using free or delete). The lifetime of dynamic memory is determined by the programmer, is very flexible to use, but also the most prone to problems.

Why not new/delete with Malloc/free?

malloc and free are standard library functions for c++/c languages, and New/delete are the C + + operators, both applying and releasing dynamic memory.

For objects that are not internal data types (such as Adt/udt), optical malloc/free cannot satisfy the requirements of dynamic objects: When an object is created, the constructor is called automatically, and the destructor is automatically invoked when the object is destroyed. Because Malloc/free is a library function, it is not within the compiler's control.

Main points of use of Malloc/free:

The prototype of the function malloc is as follows:

void *malloc (size_t size);

Use malloc to request the memory of an integer array of length, the program is as follows:

int *p= (int*) malloc (length);

The prototype of the function free is like the next step:

void free (void* memblock);

If P==null,free to P, no matter how many times it will not occur.

Main points of use of New/delete:

int *p1= (int*) malloc (sizeof (int) *lengyh);

int* P2=new Int[length];

Note: If you create an array of objects with new, you can only use the object's default constructor and you cannot use a constructor with parameters.

When using no delete to release an array of objects, be careful not to lose the symbol "[]"

For the dynamic array p of the intrinsic data type (int,float,double), delete P is equivalent to delete[]p.

Full writing cache (Write through)

Writes memory at the same time every time the cache is written, keeping the memory and cache consistent.

Writeback cache (write back)

When each write, only cache writes, if the already written cache needs to be written, it will be written into memory

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.