内存如何分配给不同数据类型的变量?

时间:2022-03-29 21:22:16

I wrote the following Code.

我写了以下代码。

#include<stdio.h>

int main()
{
    int x = 1 ;
    int *j = &x ;
    int y =  2 ;
    int *t = &y ;

    printf("%p\n" , (void *)j);
    printf("%p" , (void *)t);
}   

Output is 0028FF14 0028FF10.

输出为0028FF14 0028FF10。

The Point I want to make is that the difference between the addresses is `4'.

我要说的是,地址之间的差异是“4”。

Whereas in this case

而在这种情况下

#include<stdio.h>

int main()
{
    char x = 't' ;
    char *j = &x ;
    char y =  'f' ;
    char *t = &y ;
    printf("%p\n" , (void *)j);
    printf("%p" , (void *)t);    

   }   

Output is 0028FF17 0028FF16

输出为0028FF17 0028FF16

The difference is 1.

差异是1。

Difference In First Case is 4. Whereas in the second case it is 1. Why is it so?

第一种情况的差异是4.而在第二种情况下它是1.为什么会这样?

And What will I get if I printed value at all memory addresses individually?

如果我单独在所有内存地址打印值,我会得到什么?

Maybe It is really general and known, but I just started C, So the output of the program confuses me.

也许这是非常普遍和已知的,但我刚开始使用C,所以程序的输出让我很困惑。

Update
Now Using %p format and converted the pointer value to void* to print the pointer value as suggested by Keith Thompson.

立即更新使用%p格式并将指针值转换为void *以按照Keith Thompson的建议打印指针值。

3 个解决方案

#1


3  

There are no requirements on the order in which declared objects are laid out in memory. Apparently the compiler you're using happens to place x and y next to each other. It could have placed j between them, but it didn't.

对于在内存中布置声明对象的顺序没有要求。显然,您正在使用的编译器恰好将x和y放在一起。它本可以在它们之间放置j,但事实并非如此。

Also, the correct way to print a pointer value is to use the %p format and convert the pointer value to void*:

此外,打印指针值的正确方法是使用%p格式并将指针值转换为void *:

printf("%p\n", (void*)j);
printf("%p\n", (void*)t);

This produces an implementation-defined human-readable representation of the pointer value, typically but not always in hexadecimal.

这产生了指针值的实现定义的人类可读表示,通常但不总是十六进制。

If you care about the order in which declared variables are allocated in memory, you're probably doing something wrong, or at least not useful. Let the compiler worry about where to put things. It knows what it's doing.

如果你关心在内存中分配声明变量的顺序,你可能做错了什么,或者至少没用。让编译器担心放东西的位置。它知道它在做什么。

#2


2  

Well for starters the difference isn't always four, it just happens to be four by happy coincidence. The compiler is allowed to stick variables in memory where ever it wants to. In this case it has put your two variables next to each other in memory and the difference can be explained as that is how big an integer is on your system(4 bytes) and how big a character is on your system(1 byte). On other systems they may be different sizes and placed in different locations.

对于初学者而言,差异并不总是四个,恰好是幸运巧合恰好是四个。允许编译器将变量粘贴到内存中。在这种情况下,它将两个变量放在内存中,并且差异可以解释为系统中的整数大小(4个字节)以及系统中的字符大小(1个字节)。在其他系统上,它们可以是不同的尺寸并放置在不同的位置。

#3


1  

Each integer takes up four bytes, therefore, each integer memory address is offset by 4. A char only takes up one byte, therefore its memory addresses are offset by one.

每个整数占用四个字节,因此,每个整数存储器地址偏移4.一个char只占用一个字节,因此其存储器地址偏移一个。

#1


3  

There are no requirements on the order in which declared objects are laid out in memory. Apparently the compiler you're using happens to place x and y next to each other. It could have placed j between them, but it didn't.

对于在内存中布置声明对象的顺序没有要求。显然,您正在使用的编译器恰好将x和y放在一起。它本可以在它们之间放置j,但事实并非如此。

Also, the correct way to print a pointer value is to use the %p format and convert the pointer value to void*:

此外,打印指针值的正确方法是使用%p格式并将指针值转换为void *:

printf("%p\n", (void*)j);
printf("%p\n", (void*)t);

This produces an implementation-defined human-readable representation of the pointer value, typically but not always in hexadecimal.

这产生了指针值的实现定义的人类可读表示,通常但不总是十六进制。

If you care about the order in which declared variables are allocated in memory, you're probably doing something wrong, or at least not useful. Let the compiler worry about where to put things. It knows what it's doing.

如果你关心在内存中分配声明变量的顺序,你可能做错了什么,或者至少没用。让编译器担心放东西的位置。它知道它在做什么。

#2


2  

Well for starters the difference isn't always four, it just happens to be four by happy coincidence. The compiler is allowed to stick variables in memory where ever it wants to. In this case it has put your two variables next to each other in memory and the difference can be explained as that is how big an integer is on your system(4 bytes) and how big a character is on your system(1 byte). On other systems they may be different sizes and placed in different locations.

对于初学者而言,差异并不总是四个,恰好是幸运巧合恰好是四个。允许编译器将变量粘贴到内存中。在这种情况下,它将两个变量放在内存中,并且差异可以解释为系统中的整数大小(4个字节)以及系统中的字符大小(1个字节)。在其他系统上,它们可以是不同的尺寸并放置在不同的位置。

#3


1  

Each integer takes up four bytes, therefore, each integer memory address is offset by 4. A char only takes up one byte, therefore its memory addresses are offset by one.

每个整数占用四个字节,因此,每个整数存储器地址偏移4.一个char只占用一个字节,因此其存储器地址偏移一个。