Simple C Question

Status
Not open for further replies.

Jacobi

Ars Tribunus Militum
2,557
Subscriptor++
In the following ...

code:<HR>
Code:
 #include<stdio.h><P>void main()
{<P>	int myArray[50];
	int *myNewArray;<P>	printf("----------[myArray]-----------------\n");
	printf("myArray        (hex) = %10x\n",myArray);
	printf("&myArray       (hex) = %10x\n",&myArray);
	printf("(myArray + 5)  (hex) = %10x\n",(myArray + 5));
	printf("&myArray + 5   (hex) = %10x\n",&myArray + 5);
}

Why does <span style="font-family: Courier">$myArray + 5</span> produce a different hex value than <span style="font-family: Courier">myArray + 5</span>? eg:

code:<HR>
Code:
----------[myArray]--------------
myArray        (hex) =     12feb8
&myArray       (hex) =     12feb8
(myArray + 5)  (hex) =     12fecc
&myArray + 5   (hex) =     1302a0

In the first two printf() statements I test the values of these and they're equal to each other. My guess is that I'm missing some sort of conversion rule, but I'm not sure.
 

Jacobi

Ars Tribunus Militum
2,557
Subscriptor++
Ok, the above was compiled on VC++ 6... here's the output from the same code compiled on Microsoft C Optimizing Compiler Version 5.10:

code:<HR>
Code:
----------[myArray]-----------------
myArray        (hex) =       1348
&myArray       (hex) =       1348
(myArray + 5)  (hex) =       1352
&myArray + 5   (hex) =       1352

and on Borland C++ Version 2.0:

code:<HR>
Code:
----------[myArray]-----------------
myArray        (hex) =       ff92
&myArray       (hex) =       ff92
(myArray + 5)  (hex) =       ff9c
&myArray + 5   (hex) =        186

:confused:

I wish I had more compilers available to see what's going on, but maybe someone here can explain it to me?

Edit:

Thanks stickboy.
 

sroylance

Ars Tribunus Militum
2,156
Originally posted by RedBaron:
Oh. :\

So if I did say <span style=courier>&myArray + (5 * sizeof(int))</span> it should be the same as <span style=courier>(myArray + 5)</span>?

Er, no I guess not.

I see what you mean though, thanks.

If I understand the link correctly the type of &myarray is "pointer to an array of 50 int's", and so because math on pointers uses a base size of the pointed-to type, saying &myarray + 5 is saying
&myarray + ( 5 * ( sizeof(int) * 50 ) ). This seems to agree with the numbers you posted.
 
I do think RedBaron's test on "Microsoft C Optimizing Compiler Version 5.10" shows an interesting error in that compiler.

However, to make the answer from sroylance a bit more clear, try adding the following to the end of your code:

printf("(&myArray + 5) - myArray = %10d\n",(int)(&myArray + 5)
-(int)myArray);
printf("(myArray + 5) - myArray = %10d\n",(int)(myArray + 5)
-(int)myArray);
 
I do think RedBaron's test on "Microsoft C Optimizing Compiler Version 5.10" shows an interesting error in that compiler.

However, to make the answer from sroylance a bit more clear, try adding the following to the end of your code:

printf("(&myArray + 5) - myArray = %10d\n",(int)(&myArray + 5)
-(int)myArray);
printf("(myArray + 5) - myArray = %10d\n",(int)(myArray + 5)
-(int)myArray);
 
wow, i double posted and didn't even get to the bottom of what I intended to write. :) Anyway, it will show in decimal the number of bytes you're allocating in each call... 1000 or 20, respectively. That's because one offset is based on sizeof(int) and the other is of sizeof(int[50]).

anyway, the other thing is shows is that interesting error in the MS 5.0 compiler. To be honest, though, stickboy is right: there is no good reason to use &myArray as an accessor. Any offset is guaranteed to be OOB if you do. Odd behavior, to say the least, but still useful in some tiny edge cases (most frequently seen by old-school C coders who are doing their own memory allocation).
 
Hmm. &myarray will evaluate to the address of a temporary that contains the address of the beginning of an array. At least, it should.

&myarray[0] should return the address of the beginning of the array, as should myarray, but &myarray should evaluate to the address of a compiler generated temporary (probably on the stack).

Then again, it has been 15 yrs since I last taught this stuff so I may have forgotten.
 

xeraae

Wise, Aged Ars Veteran
132
I agree with Fractal... it's like this:

code:<HR>
Code:
int myarray[10];
myarray +2 == &myarray[2]; /* same address */
*(myarray + 2) == myarray[2]; /* same value */
*(myarray +2); /* value of the 3rd element of myarray */
*myarray +2; /* 2 added to the value of the 1st element */

&myarray is the address of the variable storing the address to myarray... not that useful probably.
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
&myArray is not the address of a "variable" that stores the address of the array. Not even a temporary variable. Why would there be a temporary variable allocated on the stack? The entire array is on the stack already (for non-global, non-static arrays)!

&myArray is the address of the array. As explained in the page I linked to before, the difference between &myArray and myArray is the type.

Arrays may decay into pointers in most contexts, but they are not exactly the same.
 
Your eskimo page agrees with me. It also explains why c51 (pre-ansi) did what it did.

Why would there be a temporary variable allocated on the stack?

Compilers generate temporaries all the time. It will generate one any time it one is needed. It needs to generate one to store the pointer to the array when you say &array.

Take a course or read a book on compiler design some time. I like the dragon book by aho and ullman as a fundamental text and is the one I used when I taught the course. I have read a dozen or so since then and still prefer it to the rest.
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
Originally posted by fractal:
Your eskimo page agrees with me.

Which page? Nothing I see in 6.12 seems to back it up.

Compilers generate temporaries all the time. It will generate one any time it one is needed. It needs to generate one to store the pointer to the array when you say &array.

I understand that compilers generate temporaries all the time, but aren't temporaries put into registers (with the former contents being pushed onto the stack if necessary)?

Also, wouldn't pushing a temporary onto the stack be redundant if the array is on the stack? Isn't the address of the array a fixed distance from the stack pointer, and isn't that known at compile-time?

And if what you're saying is true, then why wouldn't the compiler generate a temporary to point to the temporary? And another temporary to point to that? And so on?

[This message was edited by stickboy on February 02, 2002 at 02:34.]
 
Your page says
In Standard C, &arr yields a pointer, of type pointer-to-array-of-T, to the entire array
Now, no such pointer exists. I believe from your earlier post you interpreted this as saying that they simply took the value and "cast" it into a pointer of type pointer-to-array-of-T. I do not believe this to be correct as this would be a VERY dangerous thing for a compiler to do. I believe the compiler will create a temporary of type pointer-to-array-of-T and store the address of the array in that temporary. That is how I interpret the reference and that is how I would do it.

Temporaries can be in registers, on the stack, on the heap or in a special "temporary space" pool, the latter not being very popular when multiple source files are involved. It does not matter where the temporary lives unless it needs to be passed by reference to another function. In this case, &array is being passed by value to printf so, you are correct. There would be no need to generate a temporary for it on the stack for it.
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
Originally posted by fractal:
I believe from your earlier post you interpreted this as saying that they simply took the value and "cast" it into a pointer of type pointer-to-array-of-T. I do not believe this to be correct as this would be a VERY dangerous thing for a compiler to do.

Yes, that is how I interpreted it. The page says the type is different and nothing more. I would think that if the actual addresses returned were different, it would say, "The type (and possibly the value)".

Why would casting the address be a very dangerous thing to do? Using &array is dubious in the first place.

Furthermore, wouldn't giving the user access to compiler-generated temporaries would be much more dangerous in comparison? I also don't think that allowing &array to yield different values in different contexts is appropriate compiler behavior.
 

newton_meter

Ars Scholae Palatinae
1,424
fractal:
Compilers generate temporaries all the time. It will generate one any time it one is needed. It needs to generate one to store the pointer to the array when you say &array.

Maybe. Some compiler passes will insert temporaries. Typically, what happens is the compiler just pretends there is an infinite number of registers, and uses a fresh register to store every new value. Then, toward the back end, it does a register allocation pass that determines which values will actually be in a register and which will get flushed to a temporary in the stack. That's why stickboy is sort of right when he says:

stickboy:
I understand that compilers generate temporaries all the time, but aren't temporaries put into registers (with the former contents being pushed onto the stack if necessary)?

But, not always. In a machine like the x86 with a (ridiculously) small number of general purpose registers, temporaries almost always have to spend part of their lifetime on the stack. And:

stickboy:
Also, wouldn't pushing a temporary onto the stack be redundant if the array is on the stack? Isn't the address of the array a fixed distance from the stack pointer, and isn't that known at compile-time?

Correct. Instead of:

code:<HR>
Code:
;; pseudo assembler, RISC architecture:
move [sp+myArray] -> r5    ; myArray is the offset of the start of the array
move r5 -> [sp+temp3]      ; temp3 is a compiler generated temporary offset
push [sp+temp3]            ; push the temp to use as an argument to a call

The compile would probably generate the following code directly (and if not, a peephole optimizer would fix it up):

code:<HR>
Code:
push [sp+myArray]

fractal:
Now, no such pointer exists. I believe from your earlier post you interpreted this as saying that they simply took the value and "cast" it into a pointer of type pointer-to-array-of-T. I do not believe this to be correct as this would be a VERY dangerous thing for a compiler to do. I believe the compiler will create a temporary of type pointer-to-array-of-T and store the address of the array in that temporary. That is how I interpret the reference and that is how I would do it.

That's C for you: VERY dangerous. C programmers expect that when they write &foobar, that there is not an extra memory write. No memory write. They're willing to accept the danger in this case.

I think you're conflating two different things, fractal. The compiler may well generate a temporary location (either in the stack or in a register) and place the address of the start of the array in that location. But, there is no way you can write any bit of C code (that is, without using some trickery to examine the stack or dump the registers or examine the code stream) that can determine if there is a temporary or not, and if so, what it's address is or which register it's in.

As stickboy said, if we declare:

code:<HR>
Code:
int myArray[50];

then myArray has type pointer to int (which is "like" the type "array of int", it is a pointer to the first element of the array). &myArray has type pointer to array of int (i.e. the address of the array, identical with the address of the first element of the array). Maybe what you're thinking about is something like &(&myArray) (the address of this value), but that's meaningless.

(kg*m*m)/(s*s)
 
Status
Not open for further replies.