Problems in using gdb to catch the seg fault (was: Re: Greetings...)

Andreas Eibach a.eibach@gmx.net
Tue Oct 3 12:47:00 GMT 2000


> I'm used to using tools like gcc, gdb, make,
> etc in a mixed environment of Solaris and Linux.
>> My problem is taking a simple program like so:

[1]> int main()
[2]> {
[3]> char * foo = 0;
[4]> crashme(foo);
[5]> }
[6]>
[7]> int crashme(char * cp);
[8]> {
[9]> strcpy(cp, "KABOOM!!");
[10]> }
[11]>
AARGHH...
Well I'm no C professional, but I can say there's something odd about your
code.
To put this another way: I've _never_ _seen_ _this_!
You can be quite happy that this is not considered a crime you could be
arrested for
*grin*
Why do people need to crash programs _intentionally_ - just for fun?
OH BOY...;)
I've added line numbers to be able to better refer to specific lines.
Rule Of Thumb:
 - NEVER program any function without a prototype!!
- Line [7] IS a *prototype*, but where's the *function*??
 [7]> int crashme(char * cp);
You MUST NOT set a semicolon if you want to _code_ the function.
So:
 void function(char * parm) /* no semicolon!! */
 {
 }
And WHY do you define the function as INT if you don't want a return value??
This can't be reasonable.
If you insist to use an INT function you must call it this way:
 int dummy;
 dummy = crashme(foo);
 And crashme() must contain return <something>;
 So do declare the function as VOID if you don't want return values!
-----
So use this code if you want to "debug" your "program":
/* _prototype_ of function = DECLARATION */
void crashme (char *);
 int main(void)
 {
 char * foo = 0;
 crashme(foo);
 }
/* _implementation_ of function = DEFINITION, don't confuse these! */
void crashme(char * cp) /* no semicolon!! */
 {
 strcpy(cp, "KABOOM!!");
 }
--------
Let's test it now.
I know it still _crashes_ now (that's what you wanted, eh? :P )
because if the program was written correctly,
main() had got a char[] vector and not foo would've passed to
crashme(), but &foo (the address).
Andreas
NB: I got a correctly dumped core now.
--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


More information about the Cygwin mailing list

AltStyle によって変換されたページ (->オリジナル) /