PDA

View Full Version : Pop Quiz


kmj
10-22-2002, 09:21 PM
okay, what do you expect to happen if you try to compile and run the following bit of code in the context of an otherwise valid application?


for (int i = 0; i < 5; i++)
cout << i["Hello"];

EscapeCharacter
10-22-2002, 09:38 PM
dang that gives a really wierd result, i would have thought it wouldnt even compile

recluse
10-23-2002, 01:24 AM
My initial guess was also that it wouldn't compile. Now why it did what it did?

Strike
10-23-2002, 03:49 AM
kmj: should I just post the chatlog? :)

jamessan
10-23-2002, 05:04 AM
That's really bizarre, although I kinda figured it would compile fine. Something about it makes sense in the back of my mind, but I can't actually figure it out. I'm really interested in seeing the explanation for how it works.

EscapeCharacter
10-23-2002, 05:26 AM
Originally posted by jamessan
That's really bizarre, although I kinda figured it would compile fine. Something about it makes sense in the back of my mind, but I can't actually figure it out. I'm really interested in seeing the explanation for how it works.
same here

signed puzzled...

bwkaz
10-23-2002, 08:52 AM
Because i["Hello"] is exactly the same thing as "Hello"[i] is exactly the same thing as *("Hello" + i). All the compiler is doing in all cases is adding the value of i to the pointer to the "Hello" string (in one case, i is the base and there's a constant offset, and in the other, there's a constant base and i is the offset, and in the third, it's explicit), and printing out that character. Addition is commutative.

Of course, it is completely counter-intuitive, but hey, if it works, I guess...

kmj
10-23-2002, 12:16 PM
:) I'm not suggesting you use this when you program, btw. It's just interesting. :)

bwkaz
10-23-2002, 01:30 PM
Oh, of course not! That would just be ... whoa. :wtf:

jamessan
10-23-2002, 02:06 PM
That's what I thought was happening, but I didn't realize that C++ would dynamically assign the string to memory and then use i as the offset like that. It looks very similar to how you would do the same thing in MIPS otherwise I wouldn't have had a clue as to how it worked.

Bradmont
10-23-2002, 03:23 PM
Originally posted by bwkaz
Because i["Hello"] is exactly the same thing as "Hello"[i] is exactly the same thing as *("Hello" + i). All the compiler is doing in all cases is adding the value of i to the pointer to the "Hello" string (in one case, i is the base and there's a constant offset, and in the other, there's a constant base and i is the offset, and in the third, it's explicit), and printing out that character. Addition is commutative.

Of course, it is completely counter-intuitive, but hey, if it works, I guess... Darn! You beat me to it! ;)

Bradmont
10-23-2002, 03:25 PM
Originally posted by jamessan
That's what I thought was happening, but I didn't realize that C++ would dynamically assign the string to memory and then use i as the offset like that. It looks very similar to how you would do the same thing in MIPS otherwise I wouldn't have had a clue as to how it worked. Well, if you think about it, the string has to be stored in memory somewhere, so the program can access it. It's the same thing as creating a const char *, but it's just done implicitly for you by the compiler.

liquience
12-07-2002, 03:40 PM
interesting i see this here now.. i figured this out on my own a while back and even figured out that if you create the const char array "hello" or whatever else you want int the heap you can still do it. also, by changing the values of your for loop you can actually go beyond the char array and read into other areas of your callstack.

just for fun one time i stet the sentinal check value to MAX_INT and it continued along spitting out whatever was in memory untill i got the line "environment setting: kernel32.dll" at which point my program stopped instantly and i got a error saying i wasn't allowed to look at that basicly. :)

bwkaz
12-07-2002, 07:40 PM
That's what happens when you use Windows. :P

On Linux, your program will segfault fairly quickly -- before you get some error in kernel32.dll, at least...