Re: Inconsistent Program Results



Francine.Neary@xxxxxxxxxxxxxx said:

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?

TIA!


#include <malloc.h>

Replace this with <stdlib.h> which is the standard header you need when
using malloc.

#include <stdio.h>

This is fine.

#include <memory.h>

Replace this with <string.h> which is the standard header you need when
using strchr.

#define LEN 1000

void rdinpt();

Make this:

void rdinput(char **);

Incidentally, readinput would have been a better name.


void main()

Make this:

int main(void)

{
char *s, *restrict;
rdinpt(&s);
restrict=strchr(s,' ');

If the malloc fails, s will have the value NULL, which is not legal for
passing to strchr.

If there is no space in the input string, strchr will return NULL, which
you must not pass to printf to match %s, and which you must not
increment.

printf("%s\n", ++restrict);
free(s), s=restrict=0;

This made me reach for the book. Simplify:

free(s);
s = restrict = 0;

return (0);

return 0;

is fine. The return keyword is not a function name.

}

void rdinpt(char **s)
{
*s=(char *) malloc( (unsigned int) LEN);

Better:

*s = malloc(LEN * sizeof **s);

The general form is: p = malloc(n * sizeof *p);

(void) gets(*s);

Avoid gets() - it cannot be used safely. Instead, use:

fgets(s, LEN, stdin);

return(0);

Remove this, since rdinpt doesn't return a value.

}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.



Relevant Pages

  • Re: Inconsistent Program Results
    ... void rdinput; ... you need to test that restrict is not NULL before ... Don't cast the result of malloc(). ...
    (comp.lang.c)
  • Re: What does restrict mean?
    ... but what does the keyword 'restrict' mean? ... void copy ... return pDest; ... void *pTemp = malloc; ...
    (comp.lang.c)
  • Re: Inconsistent Program Results
    ... void rdinpt; ... The return type of main is int, so this should be int main, ... to increment a NULL pointer, that's undefined behavior, so anything can ... against this by making sure that restrict is not NULL, ...
    (comp.lang.c)
  • Re: Help with reading this line of code -- thanks!
    ... Notice that memcpy takes void* parameters, ... void *memcpy(void * restrict s1, ... In general casts in C source are bad, preventing error messages, ... The exception is variadic function parameters, ...
    (comp.lang.c)
  • Re: What is this noalias thing Dennis Ritchie is railing about ?
    ... And IF it were written as: void Func(int *restrict p), ... The 'restrict' keywords allow the compiler to assume that p and q do ...
    (comp.lang.c)