Re: Inconsistent Program Results



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?

Yes, you have a very small piece of code with a very large number of errors. Here is a starting place for you to think about how to write this program somewhat better:

/*
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".
*/

#if 0
/* mha: There are no such standard headers as <malloc.h> or <memory.h>
*/
#include <malloc.h>
#include <memory.h>
#endif
#include <stdio.h>
#include <string.h> /* mha: added for strchr */
#include <stdlib.h> /* mha: added for malloc and free (and
exit) */

#define LEN 1000

void rdinpt(char **); /* mha: completed prototype */

/* mha: declaring main with a return type of void is a gross error.
Do not let anyone see code in which you show that you can't get
even the simplest possible C program right. I have fixed this. */
int main(void)
{
char *s, *rstrct; /* mha: "restrict" is a keyword for
C99. Even though you may have a C89
(C90 or C95) compiler, try to keep
your code clean so it will compile
in the current standard version of
C. I have changed the spelling by
removing the vowels. */
printf("Please type something: "); /* mha: just so the user
doesn't invoke the program
and then see the machine
play dead */
fflush(stdout);

rdinpt(&s);
rstrct = strchr(s, ' ');
/* mha: added handling for input lines with no ' ' */
if (rstrct)
printf("%s\n", ++rstrct);
else
printf("The input string \"%s\" contains no space character\n"
"So an attempt to print a string beginning after\n"
"that space would be nonsense.\n", s);
free(s);
return 0; /* mha: this is OK now, but before you
were (incorrectly) claiming that
main returned nothing, but were
trying to return a value */
}

void rdinpt(char **s)
{
char *nl;

/* mha: fixed verbose and error-prone use of malloc */
*s = malloc(LEN);
/* mha: added error check */
if (!*s) {
fprintf(stderr, "malloc failed.\nQuitting ...\n");
exit(EXIT_FAILURE);
}

/* mha: one must be insane to use gets. I have changed this */
if (!fgets(*s, LEN, stdin)) {
fprintf(stderr, "Undiagnosed error on input.\nQuitting ..\n");
free(*s);
exit(EXIT_FAILURE);
}
if ((nl = strchr(*s, '\n')))
*nl = 0;

/* mha: fixed erroneous attempt to return a value from a function
declared not to return anything. */
}


TIA!


#include <malloc.h>
#include <stdio.h>
#include <memory.h>

#define LEN 1000

void rdinpt();

void main()
{
char *s, *restrict;
rdinpt(&s);
restrict=strchr(s,' ');
printf("%s\n", ++restrict);
free(s), s=restrict=0;
return (0);
}

void rdinpt(char **s)
{
*s=(char *) malloc( (unsigned int) LEN);
(void) gets(*s);
return(0);
}

.



Relevant Pages

  • Inconsistent Program Results
    ... 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. ... void rdinpt; ...
    (comp.lang.c)
  • race condition during termination process
    ... Have you ever heard about lupg's multithread tutorials? ... There are 2 exercise at the last part of chapter 7.7(Threads ... int request_index; ...
    (comp.programming.threads)
  • Re: InitializeCriticalSection code in kernel
    ... After calling the same for about ... I think this exercise brings out the purpose of my initial mail. ... void createcritical; ... privilege apps can crash themselves but nobody else, ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Hiding symbols in DSOs
    ... using either strip or during the compile/linking phase. ... void license_foo ... $ cat junk.lds ... In order to understand recursion you must first understand recursion. ...
    (comp.os.linux.development.apps)
  • Re: realloc(): invalid next size
    ... void *s2=NULL; ... but a size zero is. ... it's based on compiler secrets. ... I understand this is nothing more than an exercise in second ...
    (comp.lang.c)