Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How is the wild pointer produced in C language

Shulou Source: shulou.com Published: 2022-05-31 10:55:51 09月28日 Update

This article mainly explains how the wild pointer of c language is produced. Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how the wild pointer of c language is produced.

1, the pointer variable is not initialized, any pointer variable will not automatically become a NULL pointer when it is created, its default value is random.

Therefore, the pointer variable should be initialized at the same time as it is created, either setting the pointer to NULL or letting it point to legitimate memory.

# include void Swap (int * p1minint * p2) {int * tmp; * tmp=*p1; * p1 invalid address. The program warns that the value of the uninitialized local variable tmp,*tmp cannot be accessed because tmp is not initialized and its address is a random value (0xcccccccc)}.

If it is not initialized, the compiler will report an error 'point' may be uninitializedin the function.

2. After the pointer is released, it is not set empty.

Pointers do not assign NULL after free or delete, they just free the memory that the pointer refers to, but do not deal with the pointer itself. At this point, the pointer points to the unknown (random, incorrect, and undefined).

After the pointer is released, the pointer should be set to NULL immediately to prevent the generation of wild pointers.

Int main () {char* p; p = (char*) malloc (10); printf ("% s\ n", p); if (paired null) {free (p);} free (p); / / when releasing p again, because it has been released once before, it will report an error return 0;} so far, I believe you have a better understanding of "how the c language wild pointer is generated". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Pointers variables language pointing memory content again address or will report learning legal practical deeper interested just at the same time practical practical local Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Linux Xiaomi Shulou Technology