c - c中不使用变量时是否分配内存

标签 c memory types

#include<stdio.h>
int main()
{
    int a,b;
    float e;
    char f;
    printf("int &a = %u\n",&a);
    printf("int &b = %u\n",&b);
    printf("float &e = %u\n",&e);
    printf("char &f = %u\n",&f);
}

输出是

int &a = 2293324
int &b = 2293320
float &e = 2293316
char &f = 2293315

但是当我使用这段代码并将 printf 替换为 float--

#include<stdio.h>
int main()
{
    int a,b;
    float e;
    char f;
    printf("int &a = %u\n",&a);
    printf("int &b = %u\n",&b);
    printf("char &f = %u\n",&f);
}

那么输出是

int &a = 2293324
int &b = 2293320
char &f = 2293319

这里地址没有提供给float,而是声明在最上面。 我的问题是

  1. 内存是否没有分配给程序中未使用的变量?
  2. 为什么要按递减顺序分配地址。 ex- 它将从 2293324 变为 2293320?

最佳答案

1) Is memory not allocated to variables not used in program?

是的,这可能会发生,允许编译器对其进行优化。

2) Why addresses allocated in decreasing order. ex- it's going from 2293324 to 2293320?

对于大多数本地存储实现来说,这是常见的,它们使用 CPU 支持的堆栈指针,从堆栈顶部堆栈底部。所有这些局部变量很可能会在堆栈中分配。

关于c - c中不使用变量时是否分配内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40535199/

相关文章:

c - 从 Int 10h/AX=4F00h 检索 VESA 视频模式列表

c# - UINT8 中的自定义符号 - 如何转换为 C#?

arrays - 二维动态数组指针访问

c - GNU C 中的激活记录(嵌套函数)

c - 静态数组分配

java - 内存管理 : how to reset a list correctly

C - 将非常大的文件中的行插入到数组中

Java:将不同类型的对象放入 ModelCollection 的 ArrayList 中。接口(interface)?

javascript - 遇到 undefined 的问题 !== undefined

python - 在一个 NumPy 数组中存储不同的数据类型?