Are the elements of an array guaranteed to be stored from lower to higher addresses?

后端 未结 3 1552
遥遥无期
遥遥无期 2021-02-09 23:35

Suppose I have the following array:

int list[3]={2,8,9};
printf(\"%p,%p,%p\",(void*)&list[0],(void*)&list[1],(void*)&list[2]);

Is i

3条回答
  •  你的背包
    2021-02-10 00:40

    If you are asking about how memory appears inside the C model, then arrays appear to be contiguous in C code, and the C expression &list[0] < &list[1] is true.

    If you are asking about how actual memory appears inside a C implementation, the C standard does not require any particular arrangement of arrays in memory. Most C implementations use consecutive ascending virtual memory for arrays, but descending addresses would be a simple variation. And, at the level of physical memory, arrays are not generally consecutive, because the map from virtual memory to physical memory is determined by the operating system based on whatever it has available and may even change during execution of a process.

    Additionally, there is no guarantee that the strings printed by %p are memory addresses.

提交回复
热议问题