If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Static array allocation problem

You can think of C as a pointer to a block of memory which starts at itself. That's just the way stack arrays work.

It *is* definitely a fun little curiosity. Would probably make a good trivial question.

I like the answer but where is "c" stored (is it even stored anywhere)? I mean, it has to be stored in memory somewhere, right? This kind of goes back to my example of "a" where cout << a; prints out the value of a but cout << &a; prints off the address of a. I'm having a hard time seeing why "c" is it's own address. Is there any technical page you have off the top of your head that I can look at. I've scoured the gnu documentation but haven't found what I am looking for.

Re: Static array allocation problem

As Lindley says, that's the way arrays on the stack work. When you write:

Code:

int myarray[] = {5, 6, 7};

You are creating a memory block three ints long. The address of that block is myarray[0] because that's where the memory block starts. It just so happens that C++ let's you refer to that memory location as myarray, without specifying the index.

Re: Static array allocation problem

Originally Posted by sockman

As Lindley says, that's the way arrays on the stack work. When you write:

Code:

int myarray[] = {5, 6, 7};

You are creating a memory block three ints long. The address of that block is myarray[0] because that's where the memory block starts. It just so happens that C++ let's you refer to that memory location as myarray, without specifying the index.

The address of that block is myarray, not myarray[0]. myarray[0] would be 5, yes? The address of that block of memory is myarray so what exactly is &myarray... is it also the address of that block because that's what the code is printing out.

Re: Static array allocation problem

I like the answer but where is "c" stored (is it even stored anywhere)? I mean, it has to be stored in memory somewhere, right?

Actually, no. In the immediate scope of the array, every reference to "c" can be replaced by the compiler to a constant offset from the stack frame, since c is a local variable.

If you were to pass c to some other function, then it would "decay" to a pointer holding &c[0], and that decayed pointer would need to be stored somewhere (typically as a function parameter, but it could be stored in any old pointer). But if you don't do that, then there's no need to provide memory to hold the address of the array, because the address of the array is implicit in the stack frame.

If you need a specific example, consider what the difference is between these two locals:

Code:

void func()
{
int i;
int iarr[1];
}

There's really no difference except for datatype and syntax. They both reserve space on the stack for exactly one int, and provide a name to refer to that memory by.