C ++ memory allocation and pointer

Source: Internet
Author: User

C ++ Memory Allocation

==========================================

Static allocation: Allocated by the compiler when processing program source code.

It is allocated before execution of the program, so the efficiency is relatively high.

However, it requires you to know the type and quantity of memory required before the program is executed, and therefore lacks flexibility.

For example:

Int ival = 1024; // indicates that the compiler allocates enough storage space to store an integer value,

// The bucket is associated with the name ival. Use the value 1024 to initialize the bucket.

Dynamic Allocation: The library function is called for allocation during program execution.

There are two values related to one object: one is the value it contains, and the other is the address of the storage area where the value is stored.

C ++ supports using pointer types to store the memory address values of objects.

For example: int * pint; // declare a pointer to the int type

Pint = & ival; // when the get address operator (&) is used before an object, the returned address value is the object's address value.

 

* Pint = * pint + 1; // when the unreferencing operator (*) is used before a pointer, the returned object is the object indicated by the pointer.

Equivalent to ival = ival + 1;

In C ++, pointers are mainly used to manage and manipulate dynamically allocated memory.

The two main differences between static and dynamic memory allocation are::

1. A static object is a variable with a name. We can operate on it directly. A dynamic object is a variable without a name. We operate it indirectly through a pointer.

2. the compiler automatically allocates and releases static objects. The distribution and release of dynamic objects must be explicitly managed by programmers. It is completed by two expressions: new and delete.

Int * pint = new int (1024); // assign an int type object. The initial value of the object is 1024.

Int * pia = new int [4]; // an array that is allocated with a specific type and dimension. pia has the address of the first element of the four-element array.

Delete pint; // delete a single object

Delete [] pia; // delete an array object

 

==========================================

Member operators in C ++:

==========================================

Point operator (.) For class objects (.)

IntArray myArray; // defines the object of an IntArray class.

Int min_val = myArray. min (); // use the object to enumerate the member functions of the class.

Arrow operators used for class object pointers (->)

IntArray * pArray = new IntArray; // defines the pointer of an IntArray class.

Int max_val = pArray-> max (); // use pointers to member functions of the sort class

 

======================================

Pointer type

======================================

A typical usage of pointers is to build a data structure of a link, such as a tree and a list, and manage objects dynamically allocated during program execution, as a function parameter type, it is mainly used to pass arrays or large class objects.

 

  • The pointer cannot hold non-address values.

Int ivals = 1024;

Int * pi = ival; // error: pi is assigned the int value ival.

  • The pointer cannot be initialized or assigned the address value of another type of object.

Double dval;

Double * pd = & dval;

Pi = pd; // error: Invalid Type assignment

Pi = & dval; // error: Invalid Type assignment

  • C ++ provides a special pointer type to support this requirement:Void *)It can be assigned a value by any data pointer type address value (the function pointer cannot be assigned to it ).

// OK: void * can hold any pointer type address value
Void * pv = pi;
Pv = pd;

Void * indicates that the value is an address, but the object type of the address is unknown. We cannot operate on the object pointed to by a null pointer. We can only transmit this address value or compare it with other address values.

========================================

Pointer arithmetic)

========================================

An integer that increases or decreases the address value of a pointer is called an arithmetic operation of a pointer.

Adding 2 to the pointer means that the address value held by the pointer shifts the length of the two objects of this type. For example, if a char is a byte, an int is 4 bytes, and a double is 8 bytes, adding 2 to the pointer will increase the address value to 2, 8, or 16, it depends entirely on whether the pointer type is char, int, or double.

In fact, only when the pointer points to an array element can we make good use of the pointer arithmetic operation. Because the General variables cannot guarantee that they are continuously stored in the memory, adding 2 may not generate a valid address. A typical usage of pointer arithmetic operations is to traverse an array. For example:

Int ia [10]; int * iter = & ia [0]; int * iter_end = & ia [10]; while (iter! = Iter_end) {do_something_with_value (* iter); ++ iter; // now iter points to the next 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.