c - C中的指针(应该很简单)

标签 c pointers

我试过这样的代码:

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/

相关文章:

c - 如何从头文件中的struct Hold打印char值?

c - pthread_cancel 不起作用

C++指针语法错误

c++ - 在 C++ 中相互引用 bool 指针的正确方法

c - 在 C 中使用 qsort() 并跳过特定的字符集

c - 如何在 Linux 中使用 ioctl(原始分区)正确刷新磁盘缓存

c - 用户选择他想要进行的操作

C++ 通用指针函数或模板

c++ - 好的编码 : Pointers rather than references?

c - 指向结构的指针是指向其第一个成员的指针吗?