Heh, weird bug

Hi people, I've got code that does something kind of random, I was playing with pointers and was really bored, so I know this isn't supposed to work but check it out

Explanation below code;
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{

	char blink = 'g';
	char* blank = &blink;
	string lines="Bad Body Odor", wait="24 free";
	string hold = "rocket";
	int bail = 807;

cout <<blank;
cin>>bail;
	return 0;
}


so the output changes depending on what variable you get input from at the bottom cin. cin>>bail had the best result of popping up the alt characters, but if you have cin>>lines then hold pops up, and if you do cin>>wait then just wait pops up.

I don't know, I was just wondering if anyone wanted to play with this and find out what logic is going through the computer here if any. I thought it was strange that the bad pointer causes cin to have an affect on the cout.
I know it's junk code, but I just thought it was fun.

I like the idea that I have the special ability of busting through the barriers of idiot-proofing. NO ONE CAN PREVENT ME FROM BEING AN IDIOT!!!
Sorry, did I mention that I was bored?
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{

	char blink = 'g';
	char* blank = &blink;
	string lines="Bad Body Odor", wait="24 free";
	string hold = "rocket";
	int bail = 807;

cout <<blank;
cin>>bail;
	return 0;
}


Should simply display the memory location of blink and then wait for the user to enter something. Nothing else should be displayed unless you changed the code from your IDE to here. The other variables play absolutely no role in this program. And even if you switch which variable you're cin'ing to, it's not going to behave any differently that I'm aware of.

Sometimes it helps to display the output generated with your code so we have a better understanding of what you're talking about.
@Volatile Pulse
Usually the address of variables are sth. in this form : 00FF54E8 (for example)
So, why the addresses of chars is sth. strange? (as we saw in the example newbieg gave)

@newbieg
Sorry for asking my question in your topic.
A pointer only points to a specific memory location. Doesn't matter what kind of pointer (exceptions for char *), they can all point to the same exact memory location. C++ is type-safe, meaning that things can't easily be converted into something they shouldn't be (a Dog can't be turned into an int). You can force the compiler to cast variables, but that's getting off topic.

Whenever you display a pointer, of any type, using cout, it will display it's memory location, again with the exception of char*. Since we're cin'ing in this example, nothing will be displayed, with the exception of the memory location of blank, which will be some random hex number.

The reason I said except for char* is because cout is overloaded to display char* as a string instead of just a char pointer. The purpose behind this is so that, I believe, things like this work as intended: std::cout << "Hello, World!";
@ AIVIO
No problem, I was kind of wondering the same thing.

@ Volatile Pulse
I don't have a function set up that prints the output to a file so I can copy it, but that's a good idea, something for me to work on, thanks. Maybe a function that prints to screen at the same time as printing to file. If I use an overloaded operator then I could just send the finalized program through a word processor to replace where that function is used with cout . . .
Alright, new project.
Last edited on
You don't need an output file. You have two options. In the console when you run your code, you can copy paste right from the console. In windows, just right click the title bar and go to select all then just press ENTER. This will copy it to the clipboard so you can paste here. If you prefer the file routine, you can pipe the output into a file:
/path/to/program/myApp.exe > /path/to/file/output.txt


Then just open the file and copy from there.
Topic archived. No new replies allowed.