Re: Inconsistent Program Results
- From: "D. Power" <powerd@xxxxxxxxxx>
- Date: Tue, 06 Mar 2007 17:18:28 -0700
In article <1173220901.761854.318370@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Francine.Neary@xxxxxxxxxxxxxx wrote:
I am learning C, having fun with strings & pointers at the moment!
The following program is my solution to an exercise to take an input,
strip the first word, and output the rest. It works fine when you give
it 2 or more words, but when there's only 1 word the results vary
depending on whether it's on Windows or Linux: under MSVC it displays
no output (as it should); under gcc/Linux it instead gives
"Segmentation fault".
Any ideas what's going on?
<delurk>
I'll take a crack at this, others might find other problems, though.
This is a nonstandard header; malloc is declared in stdlib.h, so you
TIA!
#include <malloc.h>
should include that header.
#include <stdio.h>Another nonstandard header that doesn't seem to do anything.
#include <memory.h>
This should be
#define LEN 1000
void rdinpt();
void rdinpt (char **);
The return type of main is int, so this should be int main (),
void main()
or int main (void), most regulars I believe would recommend the second.
{Here's one problem, check the standard (or the man page), if strchr
char *s, *restrict;
rdinpt(&s);
restrict=strchr(s,' ');
printf("%s\n", ++restrict);
doesn't find the character being searched for, it sets the return value
to NULL, as will be the case when you input only one word. You then try
to increment a NULL pointer, that's undefined behavior, so anything can
happen, including doing nothing or segfaulting. You have to guard
against this by making sure that restrict is not NULL, so do something
like:
if (restrict)
{
printf ("%s\n", ++restrict);
}
By the way, restrict is a type qualifier in C, so it's probably not a
good name for a variable.
free(s), s=restrict=0;If you want to set these pointers to NULL, say so. Although, since you
free s then end the program, it's not necessary.
return (0);The parentheses around 0 are unnecessary (but not wrong)
}In C, the cast to char * is unnecessary and can mask failure to #include
void rdinpt(char **s)
{
*s=(char *) malloc( (unsigned int) LEN);
<stdlib.h>. Also, LEN will be automatically converted to size_t (I
believe), so the cast to unsigned int is unnecessary. Also, before
using s, you need to make sure that malloc sucessfully allocated the
memory requested, if it didn't, s will be NULL, so test s for NULL, like
I did for restrict above.
(void) gets(*s);Never use gets; you cannot prevent buffer overflow. Better to use
fgets, or use CBFalconer's ggets (I'm sure he'll be along soon with the
URL for his download page)
return(0);You declared this function with a return type of void, i.e. no return,
so don't return anything.
}
That's all I can find here (how'd I do?)
<\delurk>
.
- Follow-Ups:
- Re: Inconsistent Program Results
- From: Peter Nilsson
- Re: Inconsistent Program Results
- References:
- Inconsistent Program Results
- From: Francine . Neary
- Inconsistent Program Results
- Prev by Date: Re: question on static variable in a function
- Next by Date: Re: Whats an lvalue?
- Previous by thread: Re: Inconsistent Program Results
- Next by thread: Re: Inconsistent Program Results
- Index(es):
Relevant Pages
|
|