![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2008
Posts: 2
Rep Power: 0
![]() |
Hi, I'm trying to learn C coding and I happen to be at a chapter in a book I'm reading which deals with segmentation faults. What I'm trying to do is get a program, that accepts command-line arguments, to crash (with a seg fault) when it isn't given any arguments. The code from the book is as follows:
convert2.c C Syntax (Toggle Plain Text)
When I compile this code in gcc (v4.1.3) on Ubuntu 7.10 I get this message: $ gcc -g -o convert2 convert2.c convert2.c: In function ‘usage’: convert2.c:5: warning: incompatible implicit declaration of built-in function ‘exit’ It still compiles, but when I run it, it does this: $ ./convert2 Repeating 0 times... Since I'm trying to break the program, this isn't the result I want. Does anyone know how to "break" this program so that it will cause a seg fault? BTW, the desired result would be this: $ ./convert2 Segmentation fault (core dumped) $
__________________
Boy, it sure would be nice if we had some grenades, don't you think? - Jayne Cobb Last edited by SnakeByte; Mar 11th, 2008 at 12:33 PM. |
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
Re: Causing a Seg Fault on Purpose
>What I'm trying to do is get a program, that accepts command-line
>arguments, to crash (with a seg fault) when it isn't given any arguments. What you're seeing is undefined behavior. It might crash, it might not. In this case, accessing argv[2] doesn't touch memory outside of your address space, and when atoi fails, it returns 0. Both of these are undefined behavior, but you lucked out in that it doesn't cause the error you were expecting. The only way to guarantee a seg fault is to raise it yourself.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 1
![]() |
Re: Causing a Seg Fault on Purpose
#include <stdlib.h> will get rid of the warning about exit being implicitly defined to get a seg fault you should just try writing to a specific memory address. something along the lines of: int *i; i = 0; *i = 0; i think that would generate a seg fault. |
|
|
|
|
|
#4 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 532
Rep Power: 4
![]() |
Re: Causing a Seg Fault on Purpose
There are lots of things that will generate seg faults -- division by 0 if another one of them
int x = 123 / 0;
__________________
I Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
|
|
#5 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
Re: Causing a Seg Fault on Purpose
Out of curiosity, any particular reason you want to cause a seg fault? It is an odd request...
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 124
Rep Power: 0
![]() |
Re: Causing a Seg Fault on Purpose
#include <signal.h> /* ... */ raise (SIGSEGV);
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#7 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
Re: Causing a Seg Fault on Purpose
Quote:
![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
|
#8 | |
|
Newbie
Join Date: Mar 2008
Posts: 2
Rep Power: 0
![]() |
Re: Causing a Seg Fault on Purpose
Quote:
__________________
Boy, it sure would be nice if we had some grenades, don't you think? - Jayne Cobb |
|
|
|
|
|
|
#9 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 532
Rep Power: 4
![]() |
Re: Causing a Seg Fault on Purpose
This sounds like a good idea for a game -- 100 ways to create a seg fault
![]()
__________________
I Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
|
|
#10 | |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 1
![]() |
Re: Causing a Seg Fault on Purpose
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| linked list seg fault | sackarias | C++ | 2 | Apr 30th, 2007 3:25 PM |
| Segmentation Fault | jazz | C | 5 | Aug 21st, 2006 3:16 AM |
| Segmentation Fault | Cipher | C++ | 2 | Feb 21st, 2006 2:10 PM |
| Network Fault Injector | Mad_guy | Project Ideas | 0 | Oct 6th, 2005 4:53 PM |
| fprintf segmentation fault | Michael Edgar | C++ | 0 | Mar 21st, 2005 12:21 AM |