Magic C++-pointer

Magic C++-pointer

Preface

Students who have studied C++ or C may know that the most different thing between them and java and python is that they have pointers-advanced and powerful.
When I was reading the book today, I saw the content of the pointer again, and I remembered to record it, and more or less keep a record. I'll add the incomplete ones later

concept

Pointer is a special variable, the value stored in it is interpreted as an address in memory . The address points to a piece of memory that can be used to store data. What data? To be reasonable, as long as it is data, it can be stored, int, long, char, double, structure, class...
So you can treat pointers as an indicative thing, and refer to the storage address itself , just like a key, the address points to Important data is like a granary.
But it cannot be said that the pointer is not important, how can the key be not important, so it is important .

First acquaintance

Let’s use a simple example to get to know the pointer

#include<iostream>
using namespace std;
int main()
{
    
    
	int donuts=6;
	int* dp=&donuts;
	cout<<"dp="<<dp<<"   *dp="<<*dp<<endl;
return 0;
}

As a result, we
Insert picture description here
can see that the pointer dp points to the variable donuts, more essentially, it points to the address of the block where 6 is stored, which is the long string of addresses after dp=.
Here & is the value symbol, take out the address of donuts and assign it to dp; * dp here * is dereference, read out the data stored in the memory pointed to by the pointer.
From this small example, you can basically see the usage of pointers, how to declare and how to use them.
Note: here int* means an int type pointer. Although pointers store 64-bit addresses (on 64-bit operating systems), in order to distinguish the data types they point to, they must be distinguished

statement

I already knew a thing or two when I first met. Now let's talk about it formally.
Since the computer needs to track the type of the value pointed to by the pointer , such as some point to int and some point to char, it needs to be distinguished according to the internal value.
For example, I need several keys, several granaries, the keys are the same, but I need to distinguish, this one is for opening the barn, this one is for opening the bean silo, etc.
So named:
type * name

Writing rules

What I want to explain here is that the spaces on both sides of the * operator are optional. Traditionally, C programmers use:

int *ptr;

It is emphasized that *ptr is an int type value
and C++ programmers use this format more:

int* ptr;

It is emphasized that int* is a type-pointer to int.
Where to add spaces, it makes no difference to the compiler, even

int*ptr;

But one thing to note:

int* p1,p2;

Named like this, p1 is a pointer, but p2 is an int.
That is to say, each pointer variable name needs to use a *

initialization

It can be initialized at the time of declaration, but one thing to remember is: the pointer is initialized, not the value it points to

int higgens=5;
int* pt=&higgens;

Here is to set the value of pt (not *pt) to &higgens

example:

#include<iostream>
using namespace std;
int main()
{
    
    
	int higgens=5;
	int* pt=&higgens;

	cout<<"higgens的值为:"<<higgens
		<<";	higgens的地址为:"<<&higgens<<endl;
	cout<<"*pt的值为:"<<*pt
		<<";	pt的值为:"<<pt<<endl;
}

Output: It
Insert picture description here
can be seen that pt is initialized to the address of higgens

The danger of pointers

We need to remember the extremely important point: when C++ creates a pointer, the computer will allocate the memory used to store the address, but it will not allocate the memory used to store the data pointed to by the pointer . Providing space for data is an independent step.
such as:

long* fellow;
*fellow=23333;

At first glance, there is no problem, but if you think about it, you will see that
Fellow is indeed a pointer, but where does it point? Since it is not initialized, the system will randomly assign an address to it , which may be the address of the program code, or the address of the data, so once it is tampered with, it is likely to cause a big problem.
This is a very serious problem, so be careful!

That's it for today, and I'll add it later~

Guess you like

Origin blog.csdn.net/rjszz1314/article/details/104255071