tags:

views:

609

answers:

12

Is there such a thing as dangerous knowledge when just starting out learning C or C++? In other words what is the likelihood that I could "accidently" write and compile a code snippet that formats the hard drive, renders the OS unusable, or worse case scenario silently deletes random files on the computer?

Stuff like the veritable

format C:/

or

rm -rf /

If I am just starting out tinkering with low level C code or even messing with libraries what are some basic things to be aware of?

If there are in fact these potential dangers lurking what is a good strategy for keeping the dev environment sandboxed from your day to day system? Are certain areas of tinkering better left to a virtualized environment?

No need to go into explicit code examples, but more general advice is what I am curious about.

I suppose a good rule of thumb is: Be sure you have an understanding of the code before you compile and run some random code snippet you find on the web.

Note: I am on OS X if that is relevant.

I recognize that there is no substitute for a good backup system. Hack away destroy your computer, at most you lose a day or hours work and have to restore.

+5  A: 

If you have reasonable permissions set on your OS X, you will not need to worry about accidental deletions.

However, once you cross over from user-land to kernel-land, it becomes possible to even screw up your hardware.

To elaborate on the possible dangers of kernel-land development:

  1. Overwriting hard drive sectors.

  2. Corrupting memory in other processes, causing unexpected behavior.

  3. Controlling hardware with unexpected consequences. (It may be possible to cause poorly designed hardware to overheat, break, or rewrite the firmware with gibberish).

These problems can be elimnated by running in a virtual machine.

Unknown
Translation into OS X of the above good advice: for maximal peace of mind, create a second non-administrator account and do your experiments there. Or use a Virtual Machine. But it's all theoretical, you will know when you are writing something that might damage the system.
Pascal Cuoq
+5  A: 

if you're really worried, Virtualize! I doubt you'll accidently screw something up LEARNING C++, but if it'll calm you down, run a virtual computer and do all your dev work on it.

bobby
Then you can back up the VM with your projects for a later day to continue dev.
ccook
You have referenced a hugely useful tool, but it just doesn't apply to this question. He can't hurt his computer in C more than he can in any other language, unless he is running an old Win3.1 or DOS thing, in which case he will have no virtualization option anyway.
DigitalRoss
@digitalross: i think it answers the question quite well. he tells that you _might_ mess up your pc, but if you virtualize you cannot. i don't think the answer is actually too bad... regards
Atmocreations
+23  A: 

No

The operating system already contains protection against errant programs. We say that C is "dangerous" simply to mean that it has raw pointers and arrays.

These things make C and C++ kind of close to the hardware, fast, and memory-efficient, but they do allow a C program to clobber something random in its own memory. Its own memory, not any other program's.

You can't accidently hurt anything unless you actually execute something equally dangerous if typed into a command line, say, an "rm -rf" passed to system(3) from within your program. You could spawn a dangerous OS command from any language. Actually, it's even easier to do that in the newer type-"safe" languages.

DigitalRoss
I'm not sure "type-safe" is the word you were looking for. But otherwise right on.
Chuck
Probably worth mentioning unlink, fork bombs, futex hell and other things, but over-all a nice answer.
Tim Post
+2  A: 

Years ago on my 386, I was learning to write low level video card coding in C and I must have messed up because instead of writing to a specific VGA register, I hit the sound card and I my PC made the most un-earthly screaming noise. Totally freaked me out! It was still safe but I learnt a valuable lesson - With power comes responsibility.
By the nature of using a language like C or C++ you naturally want to interact with your PC/WorkStation/OS at a lower level so accept the consequences.
That's why many modern OS have 'Supervisor' and 'User' level access. If you REALLY want to screw your PC up then play around with the DDK

==Edit == Just remembered another one. Years ago I was working with some custom hardware and had one of the first prototype boards. I was testing the new sound chip so knocked up some quick code and sent it across then I heard a massive pop and watched in what seemed like slow-mo as a capacitor blew and sent it's contents hurtling towards my eye. Luckily it just missed my head by inches. Who says C coding isn't bad for your health!

zebrabox
Funny story. Thanks. Good to know. Loud beep noise coming from the computer reminds of the first time I replaced a motherboard in my computer and the power supply happened to flame out. Ah memories and lessons learned. :-)
Gordon Potter
+1  A: 

Learning C/C++ is healthy for your brain! :)

Absolute0
sry, that doesn't really help to solve the question: he asked whether he can mess up his system... regards
Atmocreations
+2  A: 

Nope, it's not a problem. C and C++, like all other programming languages, deal with what happens in your program, on the CPU. Of course you can make a system call which formats your harddrive, but "format harddrive" is not a part of the C++ language. Ordinary buggy code just crashes (or, when you're unlucky, doesn't crash and just behaves oddly)

In this respect, it is no more dangerous than Java, Python or any other language. All of them allow you to make system calls which can cause trouble. But as long as you stay "inside" the language, playing with the constructs defined by the language (functions, classes, pointers, loops, whatever else), the worst that can happen is that you corrupt your own program.

jalf
Good point about the system calls. That makes sense to me. So in a certain sense shell scripting can be more risky than ordinary C coding depending on what your program is trying to accomplish.
Gordon Potter
Depends. Java and Python don't allow you to hit hardware registers and fry circuit boards - for good reason. There's a reason why most embedded programming is still done in C ( and sometimes assembler ) because it gives you the control if you know what you're doing. Of course, you're right that under most circumstances you'll do little harm but the capacity is definitely there in C/C++
zebrabox
+1  A: 

Honestly, I think you answered your own question when you said it's a good idea to verify/understand the code that you run from the web. If you buy yourself a respectable book like The C Programming Language, the chances of you making a mistake serious enough to totally destroy your computer are slim to none. You'd really have to know what you're dong to write code that destroys your system. You're far more likely to accidentally rm -rf / when you run as root or install some kind of bad library that affects system stability.

On a side note, I strongly recommend learning C before learning C++. Otherwise, you'll spend your time learning C without being able to understand the concepts of object oriented programming.

senfo
+4  A: 

You might smash your PC in frustration, but other than that, I think you will be fine. :)

JP Alioto
haha, that was funny. But seriously, C and C++ aren't too bad. I haven't written a line of C++ in probably five or six years, but I still love C.
senfo
+1  A: 

When not beeing logged in as root, you cannot do very much harm on your machine.

codymanix
+1. but you can still kill your entire home-directory. which is usually more valuable than the proper state of the machine... regards
Atmocreations
Yeah, seriously, i wouldn't care about `/boot`, `/var` and `/root`, as long as my `/home/js` isn't touched xD
Johannes Schaub - litb
+1  A: 

As long as you're not making direct system calls, which is probably the case if you're just learning C, then you're not going to do any real damage. If you're just writing vanilla C and sticking with the standard library, about the worst you can do is delete a single file, but you have to deliberately call the function to do it.

Let me put it this way: if it were easy to destroy your system while learning C, I'd have put at least a dozen out of commission the first year I was learning it.

John Bode
A: 

u should have secured keyboard because format c: requires confirmación

pizdarez
+1  A: 

The only problem is getting the demons back up your nose once they're out.

Pete Kirkham
I mod +1 anyone who bothers to respond to my questions. But ??? I am not sure I see the metaphor or analogy. Does this imply the C or C++ programming is some sort of exorcism and that once the demons are out and they are wreaking havoc on my computer? And why the nose do they have blow that I am after? Will I somehow feel like taking cocaine after I head down the path of C programming? Because everything else will have simply lost that adrenalin rush? :-)
Gordon Potter
http://www.urbandictionary.com/define.php?term=nasal%20demons
Pete Kirkham
Interesting, learn something new everyday. Thanks for the reference.
Gordon Potter