Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

inkyvoyd

NOTE: THIS IS BAD CODE. DO NOT! COMPILE AND RUN IT!

  • one year ago
  • one year ago

  • This Question is Closed
  1. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  2. shivam_bhalla
    Best Response
    You've already chosen the best response.
    Medals 2

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

    • one year ago
  3. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

    uh, can you rewrite my code?

    • one year ago
  4. shivam_bhalla
    Best Response
    You've already chosen the best response.
    Medals 2

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

    • one year ago
  5. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

    why is mine good code?

    • one year ago
  6. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

    what exactly does tat do?

    • one year ago
  7. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

    and, it has syntax errors

    • one year ago
  8. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  9. shivam_bhalla
    Best Response
    You've already chosen the best response.
    Medals 2

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

    • one year ago
  10. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  11. shivam_bhalla
    Best Response
    You've already chosen the best response.
    Medals 2

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

    • one year ago
  12. shivam_bhalla
    Best Response
    You've already chosen the best response.
    Medals 2

    comment line *2 *lines

    • one year ago
  13. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

    runs without errors

    • one year ago
  14. shivam_bhalla
    Best Response
    You've already chosen the best response.
    Medals 2

    How about this @inkyvoyd ? #include<iostream> int *ptr; int *ptr = 5;

    • one year ago
  15. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

    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>

    • one year ago
  16. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  17. shivam_bhalla
    Best Response
    You've already chosen the best response.
    Medals 2

    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

    • one year ago
  18. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  19. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  20. shivam_bhalla
    Best Response
    You've already chosen the best response.
    Medals 2

    @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

    • one year ago
  21. shadowfiend
    Best Response
    You've already chosen the best response.
    Medals 3

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

    • one year ago
  22. shadowfiend
    Best Response
    You've already chosen the best response.
    Medals 3

    Though you really want to be careful if you're writing code that runs in the kernel. That gets dangerous.

    • one year ago
  23. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  24. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

    Wouldn't it supposedly crash itself given that I did write the program correctly?

    • one year ago
  25. shadowfiend
    Best Response
    You've already chosen the best response.
    Medals 3

    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.

    • one year ago
  26. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • one year ago
  27. shadowfiend
    Best Response
    You've already chosen the best response.
    Medals 3

    That's perfectly normal. You have an infinite loop in your program ;)

    • one year ago
  28. agdgdgdgwngo
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  29. inkyvoyd
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.