简体   繁体   English

就处理内存而言,这两个C函数之间有什么区别?

[英]What is the difference between these two C functions in terms of handling memory?

typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, size_t len) {
    size_t i;
    for (i = 0; i < len; i++)
        printf(" %.2x", start[i]);    //line:data:show_bytes_printf
    printf("\n");
}

void show_integer(int* p,size_t len){
    size_t i;
    for(i=0;i<len;i++){
       printf(" %d",p[i]);
    }
    printf("\n");
}

Suppose I have two functions above, and I use main function to test my functions: 假设我上面有两个函数,并且我使用main函数来测试我的函数:

int main(int argc, char *argv[])
{
    int a[5]={12345,123,23,45,1};
    show_bytes((byte_pointer)a,sizeof(a));
    show_integer(a,5);
}

I got the following results in my terminal: 我在终端中得到以下结果:

ubuntu@ubuntu:~/OS_project$ ./show_bytes
39 30 00 00 7b 00 00 00 17 00 00 00 2d 00 00 00 01 00 00 00
12345 123 23 45 1

Can someone tell me why I got the result? 有人可以告诉我为什么我得到结果吗? I understand the second function, but I have no idea why I got 39 30 00 00 7b 00 00 00 17 00 00 00 2d 00 00 00 01 00 00 00 for the first function. 我了解第二个功能,但是我不知道为什么我要为第一个功能获得39 30 00 00 7b 00 00 00 17 00 00 00 2d 00 00 00 01 00 00 00 Actually I know the number sequence above are hexadecimal decimal for 12345 , 123 , 23 , 45 , 1 . 事实上,我知道上面的数字序列是十六进制小数为1234512323451 However, I have no idea: start[i] pointer doesn't point to the whole number such as 12345 or 123 in the first function. 但是,我不知道: start[i]指针没有指向第一个函数中的整数,例如12345123 Instead, the start[0] just point to the least significant digit for the first number 12345 ? 相反, start[0]仅指向第一个数字12345的最低有效位? Can someone help me explain why these two functions are different? 有人可以帮我解释一下这两个功能为何不同吗?

12345 is 0x3039 in hex. 12345是十六进制的0x3039 because int is 32bits on your machine it will be represented as 0x00003039 . 因为int在您的计算机上是32位,所以它将表示为0x00003039 then because your machine is little endian it will be represented as 0x39300000 . 然后,因为您的计算机是0x39300000字节序,它将被表示为0x39300000 you can read more about Big and Little endian on: https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html 您可以在以下网站上了解有关大小尾数的更多信息: https : //www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html

the same applies for other results. 其他结果也一样。

On your platform, sizeof(int) is 4 and your platform uses little endian system. 在您的平台上, sizeof(int)4并且您的平台使用Little Endian系统。 The binary representation of 12345 using a 32-bit representation is: 使用32位表示形式的12345的二进制表示形式是:

00000000 00000000 00110000 00111001

In a little endian system, that is captured using the following byte sequence. 在小端系统中,使用以下字节序列捕获该字节。

00111001 00110000 00000000 00000000

In hex, those bytes are: 以十六进制表示,这些字节是:

39 30 00 00

That's what you are seeing as the output corresponding to the first number. 这就是与第一个数字相对应的输出。

You can do similar processing of the other numbers in the array to understand the output corresponding to them. 您可以对数组中的其他数字执行类似的处理,以了解与它们相对应的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM