char x = 0;
-
Given the following code, what would you expect the output to be?
#include <string>
#include <iostream>
using namespace std;char a = 'a';
char b = 'b';
char res1 = 0;
char c = 'c';
char res2 = 0;
char d = 'd';
char res3 = 0;int main(int argc, char* argv[])
{
string s = &a;
cout << s << endl;
s = &b;
cout << s << endl;
s = &c;
cout << s << endl;
s = &d;
cout << s << endl;return 0;
}
The output I had expected was:
ab
b
c
dBut that is not what I got. The output I ended up with is:
abcd<random garbage>
bcd<random garbage>
cd<random garbage>
d<random garbage>It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.
It's not rocket surgery!
Within you lies the power for good, use it!!!
Yeah, don't rely on side effects like that.
-
Given the following code, what would you expect the output to be?
#include <string>
#include <iostream>
using namespace std;char a = 'a';
char b = 'b';
char res1 = 0;
char c = 'c';
char res2 = 0;
char d = 'd';
char res3 = 0;int main(int argc, char* argv[])
{
string s = &a;
cout << s << endl;
s = &b;
cout << s << endl;
s = &c;
cout << s << endl;
s = &d;
cout << s << endl;return 0;
}
The output I had expected was:
ab
b
c
dBut that is not what I got. The output I ended up with is:
abcd<random garbage>
bcd<random garbage>
cd<random garbage>
d<random garbage>It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.
It's not rocket surgery!
Within you lies the power for good, use it!!!
Honestly, I don't think this qualifies as a "subtle bug". My first thought after seeing this was "this should blow up right in your face and you deserve it!" ;P Seeing something like this in production code would be a good reason for a firm open palm slap to the back of the programmer's head who wrote this code...
Regards, mav -- Black holes are the places where God divided by 0...
-
Honestly, I don't think this qualifies as a "subtle bug". My first thought after seeing this was "this should blow up right in your face and you deserve it!" ;P Seeing something like this in production code would be a good reason for a firm open palm slap to the back of the programmer's head who wrote this code...
Regards, mav -- Black holes are the places where God divided by 0...
mav.northwind wrote:
My first thought after seeing this was "this should blow up right in your face and you deserve it!"
Yeah, normally I would never use this, but it is for my entry in this[^] competition. So the worse the better. The result is just not what I had expected. I have developed another work-around though.
Darker than a black steer's tookus on a moonless praire night
Within you lies the power for good, use it!!!
-
mav.northwind wrote:
My first thought after seeing this was "this should blow up right in your face and you deserve it!"
Yeah, normally I would never use this, but it is for my entry in this[^] competition. So the worse the better. The result is just not what I had expected. I have developed another work-around though.
Darker than a black steer's tookus on a moonless praire night
Within you lies the power for good, use it!!!
Oh, well that's alright then.
-
Given the following code, what would you expect the output to be?
#include <string>
#include <iostream>
using namespace std;char a = 'a';
char b = 'b';
char res1 = 0;
char c = 'c';
char res2 = 0;
char d = 'd';
char res3 = 0;int main(int argc, char* argv[])
{
string s = &a;
cout << s << endl;
s = &b;
cout << s << endl;
s = &c;
cout << s << endl;
s = &d;
cout << s << endl;return 0;
}
The output I had expected was:
ab
b
c
dBut that is not what I got. The output I ended up with is:
abcd<random garbage>
bcd<random garbage>
cd<random garbage>
d<random garbage>It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.
It's not rocket surgery!
Within you lies the power for good, use it!!!
-
Given the following code, what would you expect the output to be?
#include <string>
#include <iostream>
using namespace std;char a = 'a';
char b = 'b';
char res1 = 0;
char c = 'c';
char res2 = 0;
char d = 'd';
char res3 = 0;int main(int argc, char* argv[])
{
string s = &a;
cout << s << endl;
s = &b;
cout << s << endl;
s = &c;
cout << s << endl;
s = &d;
cout << s << endl;return 0;
}
The output I had expected was:
ab
b
c
dBut that is not what I got. The output I ended up with is:
abcd<random garbage>
bcd<random garbage>
cd<random garbage>
d<random garbage>It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.
It's not rocket surgery!
Within you lies the power for good, use it!!!
I bet the chars got stored sequentially in memory, like this... abcd????? s then points to the 'a' memory location and cout prints "abcd" and whatever memory follows it until it hits the first 0 (what it thinks is the null terminator). s is then moved to point to the 'b' memory location and prints "bcd" and whatever memory follows it and so on and so forth. Interesting!
-
Given the following code, what would you expect the output to be?
#include <string>
#include <iostream>
using namespace std;char a = 'a';
char b = 'b';
char res1 = 0;
char c = 'c';
char res2 = 0;
char d = 'd';
char res3 = 0;int main(int argc, char* argv[])
{
string s = &a;
cout << s << endl;
s = &b;
cout << s << endl;
s = &c;
cout << s << endl;
s = &d;
cout << s << endl;return 0;
}
The output I had expected was:
ab
b
c
dBut that is not what I got. The output I ended up with is:
abcd<random garbage>
bcd<random garbage>
cd<random garbage>
d<random garbage>It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.
It's not rocket surgery!
Within you lies the power for good, use it!!!
I don't know if you're allowed to submit a full project or just the .h/.cpp files, but if the former you might try turning off all optimization to see if you can stop the compiler from outsmarting your designed WTFs.
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
I don't know if you're allowed to submit a full project or just the .h/.cpp files, but if the former you might try turning off all optimization to see if you can stop the compiler from outsmarting your designed WTFs.
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
I bet the chars got stored sequentially in memory, like this... abcd????? s then points to the 'a' memory location and cout prints "abcd" and whatever memory follows it until it hits the first 0 (what it thinks is the null terminator). s is then moved to point to the 'b' memory location and prints "bcd" and whatever memory follows it and so on and so forth. Interesting!
-
mav.northwind wrote:
My first thought after seeing this was "this should blow up right in your face and you deserve it!"
Yeah, normally I would never use this, but it is for my entry in this[^] competition. So the worse the better. The result is just not what I had expected. I have developed another work-around though.
Darker than a black steer's tookus on a moonless praire night
Within you lies the power for good, use it!!!
Then why don't you just assign something else to them first, then assign the 0, just to "work around those stupid compiler limitations"?
Cheers, Sebastian -- "If it was two men, the non-driver would have challenged the driver to simply crash through the gates. The macho image thing, you know." - Marc Clifton
-
Then why don't you just assign something else to them first, then assign the 0, just to "work around those stupid compiler limitations"?
Cheers, Sebastian -- "If it was two men, the non-driver would have challenged the driver to simply crash through the gates. The macho image thing, you know." - Marc Clifton
-
Given the following code, what would you expect the output to be?
#include <string>
#include <iostream>
using namespace std;char a = 'a';
char b = 'b';
char res1 = 0;
char c = 'c';
char res2 = 0;
char d = 'd';
char res3 = 0;int main(int argc, char* argv[])
{
string s = &a;
cout << s << endl;
s = &b;
cout << s << endl;
s = &c;
cout << s << endl;
s = &d;
cout << s << endl;return 0;
}
The output I had expected was:
ab
b
c
dBut that is not what I got. The output I ended up with is:
abcd<random garbage>
bcd<random garbage>
cd<random garbage>
d<random garbage>It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.
It's not rocket surgery!
Within you lies the power for good, use it!!!
My initial response ie "EEK!" - this code, if it did work, would be 'getting away with it' except that you didn't. I would kick it ould in a code review (Yes, I do QA!). Elaine (harsh fluffy tigress)
-
Given the following code, what would you expect the output to be?
#include <string>
#include <iostream>
using namespace std;char a = 'a';
char b = 'b';
char res1 = 0;
char c = 'c';
char res2 = 0;
char d = 'd';
char res3 = 0;int main(int argc, char* argv[])
{
string s = &a;
cout << s << endl;
s = &b;
cout << s << endl;
s = &c;
cout << s << endl;
s = &d;
cout << s << endl;return 0;
}
The output I had expected was:
ab
b
c
dBut that is not what I got. The output I ended up with is:
abcd<random garbage>
bcd<random garbage>
cd<random garbage>
d<random garbage>It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.
It's not rocket surgery!
Within you lies the power for good, use it!!!
if you put all the characters into a struct, and put #pragma pack before the struct, I think you'll get what you were expecting.
-
Since you never used res* it's possible the compiler optimized them out of existence entirely. If you use by never modify them it's possible they were optimized into a single memory location.
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
:)
Regards, Satips.:rose: Don't walk in front of me, I may not follow; Don't walk behind me, I may not lead; Walk beside me, and just be my friend. - Albert Camus
-
Yeah, don't rely on side effects like that.
:laugh::laugh:
Regards, Satips.:rose: Don't walk in front of me, I may not follow; Don't walk behind me, I may not lead; Walk beside me, and just be my friend. - Albert Camus