Thread: Memory Eating - Dangerous?

  1. #1
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42

    Memory Eating - Dangerous?

    I was looking into memory managment for C/C++ and found that it can make your computer go bonkers if you don't watch it.

    Anyways, take a look at this thing I put together:

    Code:
    #include <windows.h>
    
    int main()
    {
       FreeConsole();
       int x = 1;
       while(x==1)
       {
          int* fat = new int[999];
          int* ugly = new int[999];
       }
       return 0;
    }
    If you run it, you will notice that your computer will slow down immediatly! And if you run it too long, it will just stop your system totally. My question is: Can *poor* memory management like this damage your system?

    Best Regards,
    Machewy
    "All things come to an end"

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    In general, it won't provide any lasting effects that a reboot won't cure.

    Most modern OS's provide virtual memory space for each process, so that processes cannot access each other's memory. The only real potential that I can think of for lasting damage is if something that you don't own gets overwritten by your program, and then inadvertently written to disk (before the system just crashes). However, with the virtual memory space, that is not really a problem. Perhaps on a few (probably older) systems it is possible, but I find it more likely that it would just cause the system to shutdown.

    There are, however, good reasons to avoid poor memory management.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I think the general rule is to turn off the computer (and power supply) for 30 seconds to clear all RAM... I've had some pretty bad memory leaks, and I've created programs like the one you just wrote just to see what would happen... I have XP, so all that happened was windows killed the process and everything was all right again...

    but no, I dont' think it will do any real damage to your computer...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    When you request dynamic memory, *all* that happens is that the OS's memory manager looks for an available chunk of ram of that size (or greater since it will be aligned to some boundary), and then honors the request if it is indeed available or else denies it if not. And though Windows proportedly limits the amount of available dynamic (heap) memory (based on a value that is stored directly in the executable) I have personally seen it go far past this amount - as if it were a mere suggestion.

    As for permanent damage, that is a total myth. It won't overwrite any out-of-range piece of memory, either. Nothing a simple reboot can't cure.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    All that does is allocate memory that is never returned to the system. When it runs out of memory the OS will shut it down because there is no more heap...then on return the memory is never given back to the OS.

    On XP, this should not be a problem as it is supposed to run code within its own 'fence' per se. So your program will never allocate another processes memory, data, and/or code - theoretically.

    In real mode, this would shut the system down totally and DOS would crash - the next time you tried to run it. Since new is basically malloc(sizeof(<object>)) malloc will return an error code indicating that no more heap memory is available. Since you never return it to the system - it is very likely that on returning from this code, your Borland Turbo IDE or DOS itself will crash.

    No physical damage, however, is done to the system.

    A reboot or reset will fix it.

  6. #6
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    if your looking for a way to mess up your pc
    take a mice of metal(doesnt matter what type) and a metal grinder(black'n'Decker)
    hold the metal over your open case(on its side and the power on) then grind it down to nothing.
    that should do it. if it doesnt its an act of god and your pc will live forever
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A very good idea for killing your PC would be to search for that small red switch on the back of your PSU and switch it to the other position. Works wonderfully here in Europe, maybe not as well in the USA.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Originally posted by CornedBee
    A very good idea for killing your PC would be to search for that small red switch on the back of your PSU and switch it to the other position. Works wonderfully here in Europe, maybe not as well in the USA.

    I always experimented with my system.. did that and blew my SMPS.. nothing more than that... got it replaced for Rs 400 (10 $)....

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You never know - kinda like Russian roulette
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    I'd like to ask, FreeConsole() "detaches the calling process from its console", what does this mean / imply ? Thanks.

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    ^try number 5 for destroying your computer
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Ooooh! Oooh! I know! How to kill your computer:

    Step 1: Get a REALLY, REALLY BIG magnet.

    Step 2: Put the magnet on the motherboard.

    Result: Total and utter computer memory death

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    FreeConsole.

    Since a child process inherits the console from its parent, more than one process can "own" a console. Calling FreeConsole causes a process to give up its ownership to the console. If there are no other owners of the console, it is destroyed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    If, when a console has no owner, it is destroyed, why would it be called in this program, if all you want to do is create a massive memory leak ?

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    To hide any trace of the program. If there's a console, the app can easily be killed without even using the task manager.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. available memory from task manager
    By George2 in forum Tech Board
    Replies: 10
    Last Post: 01-18-2008, 02:32 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM