简体   繁体   English

指向指针空 C 的指针

[英]Pointer to Pointer null C

I have a question about a double pointer in C. For example:我有一个关于 C 中双指针的问题。例如:

int** point = malloc(sizeof(int**));
int* point2 = malloc(sizeof(int*));
*point2 = 5;
*point = point2;
free(point2);
printf("%d", *point);

My question is, if I have a double pointer pointing to another pointer (point2) and I make a free on (point2), the double pointer (point) is going to point to a memory address that has nothing.我的问题是,如果我有一个指向另一个指针 (point2) 的双指针,并且我在 (point2) 上释放了一个指针,那么双指针 (point) 将指向一个没有任何内容的内存地址。 So I should do * point = NULL?所以我应该做 * point = NULL?

Strictly speaking, you don't have to assign NULL to *point .严格来说,您不必将NULL分配给*point However, it is a very good idea to do it anyway, because it helps you distinguish valid pointers from invalid when you debug your program.但是,无论如何这样做是一个很好的主意,因为它可以帮助您在调试程序时区分有效指针和无效指针。

the double pointer ( point ) is going to point to a memory address that has nothing.双指针( point )将指向一个没有任何内容的内存地址。

Memory always has something in it.记忆里总有一些东西。 However, it's not always under your control, so it may not be legal to read its content.但是,它并不总是在您的控制之下,因此阅读其内容可能不合法。

For instance, when you free point2 , the value stored in point is not going to disappear.例如,当您释放point2 ,存储在point的值不会消失。 However, it would become illegal to use that value, as it becomes indeterminate.但是,使用该值将成为非法,因为它变得不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM