Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the cause of the wild pointer in C language?

Shulou Source: shulou.com Published: 2022-06-03 17:22:38 11月13日 Update

This article introduces the relevant knowledge of "what is the cause of wild pointer in c language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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;} "what is the cause of c language wild pointer"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Pointer variable point reason language memory content again address more knowledge or will report legal practical successful learning and then just at the same time dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn Xiaomi Huawei Microsoft