我试过这样的代码:
int *a;
*a = 10;
printf("%d",*a);
在 eclipse 中,它没有打印出任何东西。 是因为我没有给a赋初值吗?
谢谢,很有帮助。我知道这是有问题的我只是不确定确切的问题
比如,如果我执行 printf("%d",a);
我可以看到它确实包含一些东西,是 C 的规则吗
我必须给它一个指向的地方,然后我才能开始更改该地址中的值?
最佳答案
int *a;
这定义了一个变量,它是一个指向整数类型的指针。创建时指针类型变量a
包含垃圾值。- 当您执行
*a = 10;
时,它会使用存储在a
中的值(这是垃圾)作为地址并存储值10
那里。因为我们不知道a
包含什么并且它没有被分配所以a
指向一些未知的内存位置并且访问它是非法的,并且会给你一个分段故障(或类似的东西)。 - 与
printf ("%d", *a);
的情况相同。这也会尝试访问存储在您尚未分配的某些未定义内存位置的值。
.
this variable is this is the location
stored in some with the address
address on the 'garbage'. You do not
stack have permissions to
access this
+-------+---------+
| name | value |
+-------+---------+ +---------+
| a | garbage |---->| ????? |
+-------+---------+ +---------+
定义完指针类型变量后,需要向操作系统申请一些内存位置,并使用该内存位置的值存入a
,然后通过一个
。
为此,您需要执行以下操作:
int *a;
a = malloc (sizeof (int)); /* allocates a block of memory
* of size of one integer
*/
*a = 10;
printf ("%d", *a);
free (a); /* You need to free it after you have used the memory
* location back to the OS yourself.
*/
在这种情况下,它如下所示:
在 *a = 10;
之后。指针变量分配在堆栈中。此时 a
包含一个垃圾值。然后 a
指向具有该垃圾值的地址。
this variable is this is the location
stored in some with the address
address on the 'garbage'. You do not
stack have permissions to
access this
+-------+---------+
| name | value |
+-------+---------+ +---------+
| a | garbage |---->| ????? |
+-------+---------+ +---------+
在 a = (int *) malloc (sizeof (int));
之后。让我们假设 malloc
返回一些地址 0x1234abcd
,供您使用。此时 a
将包含 0x1234abcd
然后 a
指向一个有效的内存位置,该位置已分配并保留给您使用。但请注意,0x1234abcd
中的值可以是任何值,即。垃圾。您可以使用 calloc
将您分配的内存位置的内容设置为 0
。
this variable is this is the location
stored in some 0x1234abcd , allocated
address on the by malloc, and reserved
stack for your program. You have
access to this location.
+-------+------------+
| name | value |
+-------+------------+ +---------+
| a | 0x1234abcd |---->| garbage|
+-------+------------+ +---------+
在 *a = 10;
之后,通过 *a
访问内存位置 0x1234abcd
并存储 10
进入其中。
this variable is this is the location
stored in some 0x1234abcd , allocated
address on the by malloc, and reserved
stack for your program. You have
access to this location.
+-------+------------+
| name | value |
+-------+------------+ +---------+
| a | 0x1234abcd |---->| 10 |
+-------+------------+ +---------+
free (a)
之后,a
的内容即。内存地址0x1234abcd
将被释放,即返回给操作系统。请注意,在释放 0x1234abcd
后,a
的内容仍然 0x1234abcd
,但您无法再合法访问它,因为你刚刚释放了它。访问存储在 a
中的地址指向的内容将导致未定义的行为,很可能是段错误或堆损坏,因为它已被释放并且您没有访问权限。
this variable is this is the location
stored in some 0x1234abcd , allocated
address on the by malloc. You have freed it.
stack Now you CANNOT access it legally
+-------+------------+
| name | value |
+-------+------------+ +---------+
| a | 0x1234abcd | | 10 |
+-------+------------+ +---------+
the contents of a remains
the same.
编辑1
还要注意 printf ("%d", a);
和 printf ("%d", *a);
之间的区别。当您引用 a
时,它只是打印 a
的内容,即 0x1234abcd
。当你引用 *a
然后它使用 0x1234abcd
作为地址,然后打印地址的内容,在这种情况下是 10
.
this variable is this is the location
stored in some 0x1234abcd , allocated
address on the by malloc, and reserved
stack for your program. You have
access to this location.
+-------+------------+
| name | value |
+-------+------------+ +---------+
| a | 0x1234abcd |---->| 10 |
+-------+------------+ +---------+
^ ^
| |
| |
(contents of 'a') (contents of the )
| (location, pointed )
printf ("%d", a); ( by 'a' )
|
+----------------+
|
printf ("%d", *a);
EDIT2
另请注意,malloc
可能无法为您提供一些有效的内存位置。您应该始终检查 malloc
是否返回了有效的内存位置。如果 malloc
无法为您提供一些要使用的内存位置,那么它将返回给您 NULL
因此您应该检查返回值是否为 NULL
使用前。所以最后代码变成了:
int *a;
a = malloc (sizeof (int)); /* allocates a block of memory
* of size of one integer
*/
if (a == NULL)
{
printf ("\nCannot allocate memory. Terminating");
exit (1);
}
*a = 10;
printf ("%d", *a);
free (a); /* You need to free it after you have used the memory
* location back to the OS yourself.
*/
关于c - C中的指针(应该很简单),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274715/