3

friends I was just playing around with some pointer programs and realized that the GCC (and maybe the C standard) differentiates the static and dynamic arrays.

A dynamic array has a place-holder for the address of the elements in the array whereas for the static array, there is no memory location where the compiler stores the starting address of the element array.

I have an example program to demonstrate my confusion.

#include <iostream>
#int main(void)
{
  int _static[10];
  int *_dynamic;

  _dynamic = new int [10];

  std::cout<<"_static="<<_static<<" &_static="<<&_static<<" &_static[0]="<<&_static[0]<<std::endl;
  std::cout<<"_dynamic="<<_dynamic<<" &_dynamic="<<&_dynamic<<" &_dynamic[0]="<<&_dynamic[0]<<std::endl;

  return 0;
}

For the above program, _static and &_static[0] return the same address on the expected lines. However, the &_static also return the same address as the other two.

So, _static and &_static refer to the same number (or address whatever we would like to call it). As expected, _dynamic and &_dynamic indicate different locations.

So, why did the C standard say that _static and &_static must refer to the same location. It sounds confusing. One reason I feel is that &_static does not make much sense. But then should its usage not be reported as an error instead ?

Can somebody please help me with this confusion ?

Mysticial
  • 464,885
  • 45
  • 335
  • 332
prathmesh.kallurkar
  • 5,468
  • 8
  • 39
  • 50
  • 2
    Because arrays **decay** into pointers. `_static` decays into a pointer to the first element for the output, and since there's no overload for `int *` like there is for `char *` (where it outputs the string), the address held in the `int *` is used instead. Therefore `_static` outputs the value of a pointer to the first element, or `&_static[0]`. – chris Jul 12 '12 at 04:35
  • 1
    You may be interested in this post http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c where you can learn all about arrays. – R. Martinho Fernandes Jul 12 '12 at 04:36
  • 2
    Do they emit Higgs bosons as they decay? – Hot Licks Jul 12 '12 at 04:36
  • @R.MartinhoFernandes I appreciate the help but the mentioned link seems way too exhaustive and complex for my question. Thanks anyways – prathmesh.kallurkar Jul 12 '12 at 04:40
  • @prathmesh.kallurkar, It includes array decay (rhyme intended), which is what's causing the behaviour you're unsure about. It just has many other great parts that are important to know as you move on. Another such thing is to `delete[]` what you `new[]` when you're done with it. – chris Jul 12 '12 at 04:41
  • How about [another link](http://stackoverflow.com/a/10378203/179910) that I think covers most of what you care about here without going into a lot of more or less unrelated details. – Jerry Coffin Jul 12 '12 at 04:43
  • 2
    @HotLicks: no but they do emit a bunch of confused programmers, forum threads, and articles. – Lie Ryan Jul 12 '12 at 04:46
  • @LieRyan, So true. I was reading the book our first C++ course used on their pointers chapter, and, being a good book otherwise, they had some really fundamental problems in there. Most of the correct points were subtly hinted at, but you'd never know otherwise if you didn't read very carefully, almost as if you were watching to see if they correctly identified them. – chris Jul 12 '12 at 04:48
  • 1
    @chris -- The older I get the less trouble I seem to have deleting what I knew. – Hot Licks Jul 12 '12 at 12:11

3 Answers3

9

A static array inside a function is allocated on the stack. This way _static (decayed as pointer to the first entry), &_static[0] and &_static have the same value, same memory address.

On the other hand, a dynamic array is in fact a pointer to a contiguous memory area. Only the pointer is stored on the stack. This is why &_dynamic (from the stack) and _dynamic (from the heap) differ.

Hope this image shows it all:

static vs dynamic arrays

Have also a look at this article about static and dynamic global arrays continued with the distinction between extern and non-extern.

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
4

Actually _static and &_static don't refer to the same location. The only reason they appear to is because you use _static in a context in which it decayed into a pointer. That is, by the way you used them, you made them refer to the same location. But they didn't before you did that -- one was an array and the other was a pointer. They couldn't be the same because they were fundamentally different things.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • The term "decaying into pointer" seems a bit alien to me. Can you please elaborate ? – prathmesh.kallurkar Jul 12 '12 at 04:48
  • 1
    The term "decay" is used to refer to the way arrays turn into pointers to their first element, losing their size information in the process. This is why `sizeof(_static)` and `sizeof(&_static)` are different. The array does not decay when used with `sizeof` but does decay when passed to `operator<<` as in the example code. – David Schwartz Jul 12 '12 at 04:50
0

in simple words static is created in stack and dynamic is created in heap, in static array you have to tell the size before program runs but in dynamic you can take input from user and then make array of that size.

Example static:

int array[5];

Example dynamic:

int *array;
cout << "Enter size of array: ";
cin >> size;
array = new int[size];
Salman
  • 1,380
  • 7
  • 25
  • 41