17

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
som
  • 481
  • 1
  • 4
  • 9
  • Maybe I've missed something, but why do you need to know whether it is stored in the stack or on the heap? Have you tried p[0]? Perhaps you can ask a question by saying what problem you are having so that we have a starting point... – Ray May 22 '12 at 05:15
  • As @Ray says, depending on what you're doing it may well "not matter"...and may be compiler dependent in various ways, defined by the operations you can do instead of the implementation specifics. Either way, those aren't the only two classes of storage...well at least in C++ (just noticed this was a C question, I know little about the formalizations of C...someone else might have a more appropriate link.) Still, this may be useful: http://www.gotw.ca/gotw/009.htm – HostileFork says dont trust SE May 22 '12 at 05:18
  • 4
    "this kind of question is really what C is all about" -- No, it's not. " why do you need to know whether it is stored in the stack or on the heap?" -- No such need was stated. The question seems to be about byte order. – Jim Balter May 22 '12 at 06:05
  • 1
    @som, Why do you want to access the bytes of an int array? The number of bytes you will get is sizeof(a)*sizeof(int), and their order depends on the machine architecture. – Jim Balter May 22 '12 at 06:12

4 Answers4

36

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.
NAND
  • 663
  • 8
  • 22
Alok Save
  • 202,538
  • 53
  • 430
  • 533
7

Since I can't add comments just yet, here's my two cents in an answer:

If you only want to know if the memory is on the stack or heap, read the other answers, they are much more informed than me.

If you want to know exactly where the values are, you can always print the address:

printf("address at a[0] = %p\n", (void *)&a[0]);
printf("address at p[0] = %p\n", (void *)&p[0]);

where you will notice the same answer. But, then look at

printf("address at a[1] = %p\n", (void *)&a[1]);
printf("address at p[1] = %p\n", (void *)&p[1]);

Which is a fun little exercise. Just for fun, run the following code and see what you get:

 p[2] = 'a';
 printf("a[0] is %d\n", a[0]);
 printf("a[1] is %d\n", a[1]);
 printf("p[2] is %d\n", p[2]);
 putchar(p[2]);
NickO
  • 731
  • 1
  • 6
  • 20
  • 1
    +1 for being new to SO and suggesting experimentation, which is a good way to explore and investigate. Still, one should always remember that compilers are different. So just because you observe a pattern in your experiments doesn't mean it's a "truth". The only "truth" is in the wording of the specification, which makes *some* guarantees...but besides compiler bugs, the spec still leans heavily on the crutch of "undefined behavior". Also, the idiom is "two cents" (as in money)...not "two sense", although that's funny. :) http://en.wikipedia.org/wiki/Undefined_behavior – HostileFork says dont trust SE May 22 '12 at 06:07
  • @HostileFork Your points are well taken. I've decided that, as a new programmer, since google always seems to point me to SO I should probably join! Good catch on the "cents"! – NickO May 22 '12 at 06:25
3

A statically-created array will be on the stack or in the .data/.bss sections of your binary. A dynamically-created array (with new or malloc) will be allocated on the heap.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
-3

First, you pointer has to be of type int. An array is just a group of integer, saved in the memory as single integer, but in one row. A integer has 4-Byte in the memory, so you can access each value of your array by increasing your pointer by 4.

int *p = (int*)a;
for(i = 0; i < 5; i++) {
    p += 4;
    printf("%d ", *p);
}
Stefan Fandler
  • 1,141
  • 7
  • 13
  • 4
    Since the OP says s/he wants to access each byte, and explicitly used a char* pointer and a char* cast, it makes no sense to say that the pointer has to be of type int. Also, ints aren't necessarily 4 bytes and it's very wrong to add 4 to the pointer, since the addend is multiplied by the size of an int. – Jim Balter May 22 '12 at 06:09