数组如何存储在内存中? [英] How is the array stored in memory?

查看:127
本文介绍了数组如何存储在内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序,将数组初始化为:

I have a simple program which initializes an array as:

int a[]={10,20,30,40,50};

char *p;

p=(char*)a;

现在,我想通过指针p访问每个字节的值.为此,我需要知道数组如何存储在内存(堆栈或堆)中?

Now I want to access the value at each byte through pointer p. For that I need to know how does the array stored in memory(stack or heap)?

推荐答案

一个数组将其元素存储在连续的内存位置中.
如果您在本地创建阵列,则它将在堆栈上.元素的存储位置取决于 存储规范 .
例如:
全局或静态声明的数组与本地声明的数组具有不同的存储规范.从技术上讲, where 部分是 实现定义的 ,但是通常实现会使用类似的使用模式.

An array stores its elements in contiguous memory locations.
If You created the array locally it will be on stack. Where the elements are stored depends on the storage specification.
For Example:
An array declared globally or statically would have different storage specification from an array declared locally. Technically, the where part is implementation defined but usually implementations would use similar usage patterns.

  • 本地数组 (通常)是在
  • 时在 stack 上创建的.
  • 通常会在 bss/数据段 和上创建 全局或静态数组 . li>
  • 将在 上创建 动态创建的数组 .
  • A local array will be (usually) created on stack while
  • A global or static array will be (usually) created on bss/data segments and
  • A dynamically created array will be created on heap.

这篇关于数组如何存储在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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