Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

c - How is the array stored in memory?

I have a simple program which initializes an array as:

int a[]={10,20,30,40,50};   
char *p;
p=(char*)a;

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

  • 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...