On the difference between pointer and array in C and C++
- 2020-09-28 09:04:28
- OfStack
Here is a brief introduction to memory partitioning.
The memory is divided into five areas according to the purpose:
1. Stack area: allocated and reclaimed by system control.
For example, define variables
int x = 0; int *p = NULL;
Variables occupy the memory allocated in the stack area.
2. Heap area: managed by programmer.
Memory requested by malloc in C, or by C++ in new, is requested in the heap area. It needs to be recycled by the programmer, otherwise it will cause memory leak.
3. Global: Stores global and static variables
4. Constant area: Stores constants.
5. Code area: Store the compiled code in base 2.
Arrays and Pointers have a lot in common. In fact, arrays are also a pointer, and a somewhat special pointer.
For example, you can apply for an array containing 10 int types of data
// way 1
int arr[10]; // The stack area
// way 2
int *ptr = new int[10]; // The heap area
We also often use Pointers when defining a function, and arrays when passing in arguments (argument) (even function declarations and definitions can be mixed with Pointers and arrays). Such as:
void func(int *ptr, int n) {
//statements
}
int main(void) {
int arr[10];
...
func(arr, 10);
return 0;
}
The array name represents 1 address, which is the first address of the memory unit it occupies. In the above example, arr and & arr[0] is the same.
The array name represents 1 address, which is the same as the pointer 1. The difference is that the array name is a fixed address, the array is stored on the stack, its address cannot be changed, that is, 1 const.
There are several ways to use a pointer to an array.
int arrInt[10];
/*
ptr1 and arrInt The value is 1 All things are the same 1 The first address of the block memory space.
This form specifies that ptr1 Points to the 1 containing 10 An array of 20 elements, which is cumbersome to write and also restricts Pointers, is rarely used.
*/
int (*ptr1)[10] = &arrInt;
/*
This is the form we are more familiar with and pleased to see.
In the previous 1 In block code, you pass in the array name in the argument, and you actually do that 1 Things:
int *ptr = arrInt; The formal parameter is 1 A point to arrInt The pointer.
The main points to explain are below 1 Block code ~~~
*/
int *ptr2 = arrInt;
In C++, there is one type of reference, which is equivalent to giving a variable an individual name. When passing parameters, they will not be copied, which improves efficiency and reduces memory overhead.
Obviously, you can use an array reference when passing an array parameter.
There are also different ways to reference arrays:
int arrInt[10];
// And a pointer to an array 1 It's defined in a similar way
int (&ref1)[10] = arrInt;
// How about writing it this way?
int *&ref2 = arrInt;
/*
The compiler will report an error:
invalid initialization of non-const reference of type 'int*&' from an rvalue of type 'int*'.
The reason is that the array is in the stack arrInt Controlled by the system, its address cannot be changed.
If the above code is passable, it means passable ref2 Point to another address and modify arrInt The memory address of which is not allowed, so the compile does not pass.
*/
/*
You can do that.
ref3 is 1 A quote, and is 1 Is a reference to a constant 1 a int* .
Due to the ref3 is 1 A constant reference whose value is not allowed to be modified, therefore ref3 I can refer to arrInt .
*/
int * const &ref3 = arrInt;
AD nauseam I saying this in one sentence
The array in the stack is a pointer that cannot change the address, or an const pointer.
o o (� �)
(One more sentence ~~~)
The array that dynamically requests memory in the heap area is the pointer that we usually use.
Above is to talk about C/C++ Pointers and arrays of different details, more about c/c++ Pointers and arrays of information please pay attention to this site other related articles!