## inkyvoyd 2 years ago NOTE: THIS IS BAD CODE. DO NOT! COMPILE AND RUN IT!

1. inkyvoyd

#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; }

2. shivam_bhalla

Inky, this is a good code LOL :P The bad code Is : int *ptr; *ptr = 5;

3. inkyvoyd

uh, can you rewrite my code?

4. shivam_bhalla

Just type : #include<iostream> int *ptr; int *ptr = 5;

5. inkyvoyd

why is mine good code?

6. inkyvoyd

what exactly does tat do?

7. inkyvoyd

and, it has syntax errors

8. inkyvoyd

back to my code: I want to turn it into bad code. how?

9. shivam_bhalla

@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)

10. inkyvoyd

#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; }

11. shivam_bhalla

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; }

12. shivam_bhalla

comment line *2 *lines

13. inkyvoyd

runs without errors

14. shivam_bhalla

15. inkyvoyd

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>

16. inkyvoyd

#include<iostream> int main(){ int *ptr; int *ptr = 5; return 0; }

17. shivam_bhalla

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

18. inkyvoyd

It's not diong anything. Can we put that in a for loop?

19. inkyvoyd

#include<iostream> #include<ctime> int main() { for(;;){ srand(time(NULL)); int *ptr; *ptr = rand(); } return 0; }

20. shivam_bhalla

@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.

23. inkyvoyd

I think there's something wrong with my code though, because my console is working perfectly fine.

24. inkyvoyd

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.

26. inkyvoyd

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 ;)

28. agdgdgdgwngo

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).

29. inkyvoyd

Well, my response to that is that I have 8GB of RAM (muahahaa)