inkyvoyd
  • inkyvoyd
NOTE: THIS IS BAD CODE. DO NOT! COMPILE AND RUN IT!
Computer Science
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions

inkyvoyd
  • inkyvoyd
#include #include using namespace std; int main(){ for(;;){ srand ( time(NULL) ); int *ptr; int a; ptr=&a; *ptr =rand()%4,294,967,295; } return 0; }
anonymous
  • anonymous
Inky, this is a good code LOL :P The bad code Is : int *ptr; *ptr = 5;
inkyvoyd
  • inkyvoyd
uh, can you rewrite my code?

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
Just type : #include int *ptr; int *ptr = 5;
inkyvoyd
  • inkyvoyd
why is mine good code?
inkyvoyd
  • inkyvoyd
what exactly does tat do?
inkyvoyd
  • inkyvoyd
and, it has syntax errors
inkyvoyd
  • inkyvoyd
back to my code: I want to turn it into bad code. how?
anonymous
  • anonymous
@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)
inkyvoyd
  • inkyvoyd
#include #include 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; }
anonymous
  • anonymous
To turn your code into bad one, just comment line one line: #include #include using namespace std; int main() { for(;;) { srand ( time(NULL) ); int *ptr; //int a; //ptr=&a; *ptr =rand()%4,294,967,295; } return 0; }
anonymous
  • anonymous
comment line *2 *lines
inkyvoyd
  • inkyvoyd
runs without errors
anonymous
  • anonymous
How about this @inkyvoyd ? #include int *ptr; int *ptr = 5;
inkyvoyd
  • 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>
inkyvoyd
  • inkyvoyd
#include int main(){ int *ptr; int *ptr = 5; return 0; }
anonymous
  • anonymous
This should do the trick :P #include int main() { int *ptr; *ptr = 5; return 0; } read more about pointers here @inky http://library.thinkquest.org/11127/i09.htm
inkyvoyd
  • inkyvoyd
It's not diong anything. Can we put that in a for loop?
inkyvoyd
  • inkyvoyd
#include #include int main() { for(;;){ srand(time(NULL)); int *ptr; *ptr = rand(); } return 0; }
anonymous
  • anonymous
@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
shadowfiend
  • shadowfiend
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 ;)
shadowfiend
  • shadowfiend
Though you really want to be careful if you're writing code that runs in the kernel. That gets dangerous.
inkyvoyd
  • inkyvoyd
I think there's something wrong with my code though, because my console is working perfectly fine.
inkyvoyd
  • inkyvoyd
Wouldn't it supposedly crash itself given that I did write the program correctly?
shadowfiend
  • shadowfiend
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.
inkyvoyd
  • 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
shadowfiend
  • shadowfiend
That's perfectly normal. You have an infinite loop in your program ;)
anonymous
  • anonymous
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).
inkyvoyd
  • inkyvoyd
Well, my response to that is that I have 8GB of RAM (muahahaa)

Looking for something else?

Not the answer you are looking for? Search for more explanations.