我试过这样的代码:
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);
相同。这还会尝试访问存储在尚未分配的某些 undefined 存储位置中的值。 。
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
中,然后通过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
中存储的地址指向的内容将导致 undefined 的行为,很可能是分段错误或堆损坏,因为它已被释放并且您没有访问权限。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.
EDIT1
还要注意
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.
*/