Cprogramming 简明教程
Memory Address in C
Memory Address in C
当用 C 语言声明变量时, memory address 会被分配给变量。 C compiler 将变量的值存储在内存的不同段中。
The memory address is assigned to a variable when a variable is declared in C language. C compiler stores the value of the variable in the different segments of the memory.
Segments of Memory
C 程序的不同元素存储在计算机内存的不同段中,这些段具有以下内容 -
Different elements of a C program are stored in different segments of the computer’s memory, which has the following segments −
-
Text segment − A text segment, also known as a code segment or simply as text, is one of the sections of a progris used to store the object version of the C program.
-
Initialized data segment − The global variables and static variables that are initialized by the programmer are allocated the memory in the initialized data segment.
-
Uninitialized data segment − An uninitialized data segment also called as bss (stands for block started by symbol). The program allocates memory for this segment when it loads. Every data in bss is initialized to arithmetic "0" and pointers to null pointer by the kernel before the C program executes.
-
Stack − Stack is a LIFO (last in first out) data structure. Stack segment stores the value of local variables and values of parameters passed to a function. It also maintains the pointer to which a function call returns.
-
Heap − Heap is used for allocating memory during the runtime. All the functions that perform dynamic memory allocation deal with heap.
Accessing Memory Address
可以通过 Address of (&) operator 访问或指定 C 中的内存地址。要使用 printf() function 打印内存地址,您需要使用 %p 格式说明符。
The memory addresses in C can be accessed or specified through the Address of (&) operator. To print a memory address using the printf() function, you need to use %p format specifier.
Example
在以下示例中,我们声明了两个变量并打印它们的内存地址 -
In the following example, we are declaring two variables and printing their memory addresses −
#include <stdio.h>
int main() {
// Declaring two variables
int a;
int b;
// Accessing their memory
// addresses and print them
printf("Memory address of a is %p\n", &a);
printf("Memory address of b is %p\n", &b);
return 0;
}
How Does C Compiler Allocate Memory?
可以将内存视为一个字节数组,其中每个地址都是数组中的索引,并包含 1 个字节。
Memory can be considered of as an array of bytes where each address is on index in the array and holds 1 byte.
当您在 C 程序中声明一个变量时,C 编译器根据大小要求为其分配一个随机的内存位置,而大小要求又取决于类型。
When you declare a variable in a C program, the C compiler allocates a random memory location to it, depending on the size requirement, which depends on the type.
当声明 int 变量时 -
When an int variable is declared −
int x = 10;
编译器在随机字节地址中分配值。由于 int 类型需要 4 个字节,因此为它预留了接下来的四个地址。
The compiler assigns the value in a random byte address. Since an int type needs 4 bytes, the next four addresses are earmarked for it.
C 允许你找出已分配给某变量的地址。你可以使用 %p 格式说明符在内存位置的 16 进制格式中打印地址。
C allows you to find out which address has been allocated to a variable. You can use the %p format specifier to print the hexadecimal address of the memory location.
char x = 'A';
printf ("address of x: %p\n", &x);
这将以 16 进制格式打印“x”的内存地址:
This prints the address of "x" in hexadecimal format −
Address of x: 000000000061FE1F
Example
Arrays in C 是连续的内存区域,这些内存区域包含一定数量相同数据类型(int、long、*char 等)的值。
Arrays in C are contiguous memory areas that hold a number of values of the same data type (int, long, *char, etc.).
#include <stdio.h>
int main() {
// initialize an array of ints
int numbers[5] = {1,2,3,4,5};
int i = 0;
// print the address of the array variable
printf("numbers = %p\n", numbers);
// print addresses of each array index
do {
printf("numbers[%u] = %p\n", i, (void *)(&numbers[i]));
i++;
} while(i < 5);
// print the size of the array
printf("sizeof(numbers) = %lu\n", sizeof(numbers));
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
numbers = 0x7fff0815c0e0
numbers[0] = 0x7fff0815c0e0
numbers[1] = 0x7fff0815c0e4
numbers[2] = 0x7fff0815c0e8
numbers[3] = 0x7fff0815c0ec
numbers[4] = 0x7fff0815c0f0
sizeof(numbers) = 20