NOTE: THIS IS BAD CODE. DO NOT! COMPILE AND RUN IT!
#include <ctime> #include <iostream> using namespace std; int main(){ for(;;){ srand ( time(NULL) ); int *ptr; int a; ptr=&a; *ptr =rand()%4,294,967,295; } return 0; }
Inky, this is a good code LOL :P The bad code Is : int *ptr; *ptr = 5;
uh, can you rewrite my code?
Just type : #include<iostream> int *ptr; int *ptr = 5;
why is mine good code?
what exactly does tat do?
and, it has syntax errors
back to my code: I want to turn it into bad code. how?
@inkyvoyd , Pointer should hold address of the variable it is pointing to. First an address of a variable must be given to a pointer to which it is pointing to. (eg. ptr=a) . Then *ptr will give you the value of the variable whose address the pointer ptr (i,e a in your case)is storing,. For a variable(say a), it's address is given by &variable.(i.e. &a)
#include <ctime> #include <iostream> using namespace std; int main(){ for(;;){ srand ( time(NULL) ); int *ptr; int a; a=rand()%4,294,967,295; ptr=&a; *ptr =rand()%4,294,967,295; } return 0; }
To turn your code into bad one, just comment line one line: #include <ctime> #include <iostream> using namespace std; int main() { for(;;) { srand ( time(NULL) ); int *ptr; //int a; //ptr=&a; *ptr =rand()%4,294,967,295; } return 0; }
comment line *2 *lines
runs without errors
How about this @inkyvoyd ? #include<iostream> int *ptr; int *ptr = 5;
Source.cpp(4): error C2086: 'int *ptr' : redefinition 1> Source.cpp(3) : see declaration of 'ptr' 1>Source.cpp(4): error C2440: 'initializing' : cannot convert from 'int' to 'int *' 1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast 1>
#include<iostream> int main(){ int *ptr; int *ptr = 5; return 0; }
This should do the trick :P #include<iostream> int main() { int *ptr; *ptr = 5; return 0; } read more about pointers here @inky http://library.thinkquest.org/11127/i09.htm
It's not diong anything. Can we put that in a for loop?
#include<iostream> #include<ctime> int main() { for(;;){ srand(time(NULL)); int *ptr; *ptr = rand(); } return 0; }
@inkyvoyd , read the quote below : "If you carelessly declare and use pointers, they can actually cause your computer to crash - and crash badly. Why? Well, when you first create a variable, what it contains is trash - some crap left behind by some other program. Only when you give it a value is that variable 'safe' to use. The same is with pointers, except that the results are far worse. While undeclared variables normally just return 'trash' values, undeclared pointers, when used, may access memory locations which are sensitive to the computer's inner health, causing it to crash. So, remember to give values to pointers before you use them. Otherwise, you'll face the undesirable consequences! " Source : http://library.thinkquest.org/11127/i09.htm
Notably, most modern operating systems don't allow access to memory belonging to anything but the current program. So your own program will crash with something called a segmentation fault, rather than crashing the computer ;)
Though you really want to be careful if you're writing code that runs in the kernel. That gets dangerous.
I think there's something wrong with my code though, because my console is working perfectly fine.
Wouldn't it supposedly crash itself given that I did write the program correctly?
The behavior is usually described as unpredictable. It may crash, but it may not. Sometimes you're overwriting something you didn't expect. The OS may have some sort of protection that lets the program keep running instead of crashing it. The behavior is simply undefined, and the OS can do whatever at that point.
Oh, and I forget to mention (in case anyone's interested) that this program has used up 99-100% of my VM's cpu I'm using VMware, and the OS is win xp. I think I compiled it for NET framework 2.0
That's perfectly normal. You have an infinite loop in your program ;)
I once had a Python script that worked fine for small inputs \( n \in (9,150)\), but give it a huge number like 1300 and it blew up in memory. Took windows a long time to open up the task manager and click on 'end process tree' on python (which was consuming over 3 GB out of 4).
Well, my response to that is that I have 8GB of RAM (muahahaa)
Join our real-time social learning platform and learn together with your friends!