Re: Inconsistent Program Results



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.

TIA!


#include <malloc.h>
This is a nonstandard header; malloc is declared in stdlib.h, so you
should include that header.
#include <stdio.h>
#include <memory.h>
Another nonstandard header that doesn't seem to do anything.

#define LEN 1000

void rdinpt();
This should be
void rdinpt (char **);

void main()
The return type of main is int, so this should be int main (),
or int main (void), most regulars I believe would recommend the second.
{
char *s, *restrict;
rdinpt(&s);
restrict=strchr(s,' ');
printf("%s\n", ++restrict);
Here's one problem, check the standard (or the man page), if strchr
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)
}

void rdinpt(char **s)
{
*s=(char *) malloc( (unsigned int) LEN);
In C, the cast to char * is unnecessary and can mask failure to #include
<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>
.



Relevant Pages

  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: invalid pointer adress
    ... >> pointer causes the program to exit with a core dump. ... struct s2{void *p;}; ... int leseExterneHinweise_masch_storno ... typedef struct s_AusdatFeldbeschreibung ...
    (comp.lang.c)
  • Should io(read|write)(8|16|32)_rep take (const|) volatile u(8|16|32) __iomem *addr?
    ... the destination pointer on user-space ... @src: ... const void *data, int bytelen); ...
    (Linux-Kernel)
  • Re: function pointer help!
    ... //the return void and input prameters are defined in the manual... ... void MyProjectView::CallHandler(int,unsigned int, unsigned int, void*) ... You are attempting to use a C++ member function as the callback, but the callback is defined in terms or C, not C++. ... The underlying problem is that C++ functions receive a hidden parameter, the 'this' pointer, so their signature is incompatible with C definitions. ...
    (microsoft.public.vc.mfc)
  • Re: Whats the meaning of this code
    ... void * work ... Whats the meaning of this line:- ... The meaning...a function called work that accepts a pointer of type ... pointer value is cast to an int value. ...
    (comp.lang.c)