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

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

问题描述

我有一个简单的程序来初始化一个数组:

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 is the array stored in memory? Is it stored on the stack or the 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.

  • 本地数组(通常)会在堆栈上创建,而
  • 全局或静态数组(通常)会在bss/数据段和上创建莉>
  • 将在上创建动态创建的数组.

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

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