关于空的arrary的记忆 [英] The memory about empty arrary

查看:97
本文介绍了关于空的arrary的记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的英语很差。请尽量了解我...



阅读下面的代码,我虽然 p 的值是随机的,

我多次运行这个程序,p的值在& ch1和& ch2之间是多个。

我想知道为什么...



构建环境:CentOS6.3(gcc版本4.4.6)



1.Programm

I'm poor in English.Please try hard to understand me...

Read the code below, I though the value of p is random,
I ran this programm many times, the value of p is alwayls between &ch1 and &ch2.
I want to know why...

Build env:CentOS6.3(gcc version 4.4.6)

1.Programm

int main(void)
{
    char ch1 = 0x12;
    char *p[] = {};
    char ch2 = 0x34;

    printf("%#x\n", &ch1);
    printf("%#x\n", p);
    printf("%#x\n", &ch2);
    return 0;
}





2.Result



2.Result

0xbf8648af
0xbf8648ac
0xbf8648ab

推荐答案

您正在打印局部变量的内存地址,因此结果完全符合被期望。您认为它们会是什么?
You are printing the memory addresses of your local variables so the results are exactly as is to be expected. What did you think they would be?


因为空数组是一个数组:它只有零元素。

它不能有随机给予 p 的值,因为 p 是一个合法的指针 - 它只是指一个没有元素的数组。如果传递给检查例程,随机值可能会导致问题。
Because an empty array is an array: it just has zero elements.
It can't have a "random" value given to p because p is a legitimate pointer - it just refers to an array that has no elements. A random value could cause problems if passed to check routines.


这篇关于关于空的arrary的记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆