Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2 
Author Message
 Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

I'm new to the language, so I'm probably doing something lame,
and it probably involves the pointers I only vaguely realise I'm
implicitely using, but I can't for the life of me determine
what I'm doing wrong.  This is a fairly simple piece of code,
an attempt to implement Exercise 2-4 from p 48 of K&R, 2nd ed.
A significant part of this code comes direct from the example
on page 47.

I'd be very appreciative if some guru could whap me on the
head with exactly what I'm doing wrong.  Here's the error
I'm getting at runtime:

D:\cygwin\usr\local\src\hellow>squeeze2
      0 [main] SQUEEZE2 107087 handle_exceptions: Exception:
STATUS_ACCESS_VIOLATION
    493 [main] SQUEEZE2 107087 stackdump: Dumping stack trace to
SQUEEZE2.EXE.stackdump

I can provide the stackdump, but I figure the error will
be obvious from my actual code (which follows below).  
Oh, my compiler version:
$ gcc -v
Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/2.95.2-5/specs
gcc version 2.95.2-5 19991024 (cygwin experimental)

--

TIA,
Jonadab, a Perl, Inform, and elisp programmer trying to learn C.

Source code follows:
/*-----------------------------------------------------------------*/

/* My attempt at K&R2 Exercise 2-4 p 48. */

int charinstring (char c, char t[])
{
    int i;
    for (i=0; t[i] != '\0'; i++)
        if (t[i] == c) return 1;
    return 0;

Quote:
}

/* squeeze:  delete all chars from s that are in x */
void squeeze(char s[], char x[])
{
    int i, j;

    for (i = j = 0; s[i] != '\0'; i++)
        if (! charinstring(s[i], x))
            s[j++] = s[i];
    s[j] = '\0';

Quote:
}

void testsqueeze (char sentence[], char toss[])
{
    /* test squeeze function */
    printf ("Squeezing \"%s\" from \"%s\" ", toss, sentence);
    squeeze (sentence, toss);
    printf ("results in \"%s\".\n", sentence);

Quote:
}

main ()
{
    testsqueeze("The quick brown fox jumped over lazy dogs.",
                "rstlne");
Quote:
}

--



Fri, 18 Jul 2003 13:42:46 GMT  
 Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2


Quote:
>main ()
>{
>    testsqueeze("The quick brown fox jumped over lazy dogs.",
>                "rstlne");
>}

String literals are non-modifiable. You have been fortunate enough to
discover this early, because your system is storing them in a write
locked segment. Never forget it. Syntactically they are of type char []
semantically they are char const []

--
Francis Glassborow
See http://www.accu.org for details of The ACCU Spring Conference, 2001
(includes many regular participants to C & C++ newsgroups)
--



Sat, 19 Jul 2003 07:14:35 GMT  
 Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

Quote:
>     testsqueeze("The quick brown fox jumped over lazy dogs.",
>                 "rstlne");

testsqueeze() modifies the string given as the first parameter.  You
are giving it a literal string, which must not be modified according
to the C standard.  To make it work, put the string in a variable:

  main ()
  {
      char str[] = "The quick brown fox jumped over lazy dogs.";
      testsqueeze(str, "rstlne");
      return 0;  /* <-- this is a separate issue */
  }

As you're using GCC, you could also compile the program with the
-traditional option; that makes literal strings writable, among other
things.  It isn't good style to use that in new programs, though.
--



Sat, 19 Jul 2003 07:14:39 GMT  
 Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

Quote:

> I'm new to the language, so I'm probably doing something lame,
> and it probably involves the pointers I only vaguely realise I'm
> implicitely using, but I can't for the life of me determine
> what I'm doing wrong.  This is a fairly simple piece of code,
> an attempt to implement Exercise 2-4 from p 48 of K&R, 2nd ed.
> A significant part of this code comes direct from the example
> on page 47.

.... snipo ...

This was asked and answered, possibly in c.l.c.  Why are you
wasting time and bandwidth?

--

http://www.qwikpages.com/backstreets/cbfalconer
   (Remove "NOSPAM." from reply address. my-deja works unmodified)

--



Sat, 19 Jul 2003 07:15:25 GMT  
 Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

Quote:
> I'm new to the language, so I'm probably doing something lame,
> and it probably involves the pointers I only vaguely realise I'm
> implicitely using, but I can't for the life of me determine
> what I'm doing wrong.  This is a fairly simple piece of code,
> an attempt to implement Exercise 2-4 from p 48 of K&R, 2nd ed.
> A significant part of this code comes direct from the example
> on page 47.

snip non-problematic code...

Quote:

> main ()
> {
>     testsqueeze("The quick brown fox jumped over lazy dogs.",
>                 "rstlne");
> }
> --


The following replacement:

main ()
{
  char  a[] = "The quick brown fox jumped over lazy dogs.",
        b[] = "rstlne";

  testsqueeze(a,b);

Quote:
}

will work.  The tricky problem here is that, while you have declared
the appropriate parameters as arrays, since they are function
parameters they are really pointers and not arrays.  So initializing
the parameters directly will result in them pointing to
*non-writeable* memory.  The way around this is to initialize some
very writeable arrays yourself, which the functions you wrote will
accept pointers to (which are automatically converted).

Micah
--



Sat, 19 Jul 2003 07:16:13 GMT  
 Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

Quote:
> I'm new to the language, so I'm probably doing something lame,
> and it probably involves the pointers ...

So far as I can see you did only two "lame" things:
you invoked printf without #including <stdio.h>; and
you neglected to return an int status (presumably 0)
from main.  If you fix these, the program should work.

These are things that K&R 1st Edition didn't worry about,
since it didn't keep the programs from working on the C
platforms that existed at the time.  For K&R 2nd Edition
we diebated this issue and the authors decided for
pedagogical reasons to push off a discussion of it until
later in the book.
--



Sat, 19 Jul 2003 07:16:27 GMT  
 Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

[...]

Quote:
>/* squeeze:  delete all chars from s that are in x */
>void squeeze(char s[], char x[])
>{
>    int i, j;

>    for (i = j = 0; s[i] != '\0'; i++)
>        if (! charinstring(s[i], x))
>            s[j++] = s[i];

here you are replacing characters 'in place', i.e. you modify
s[].

Quote:
>    s[j] = '\0';
>}

>void testsqueeze (char sentence[], char toss[])
>{
>    /* test squeeze function */
>    printf ("Squeezing \"%s\" from \"%s\" ", toss, sentence);
>    squeeze (sentence, toss);
>    printf ("results in \"%s\".\n", sentence);
>}

>main ()
>{
>    testsqueeze("The quick brown fox jumped over lazy dogs.",
>                "rstlne");

and here you are passing a string literal as first argument ... I think your
platform doesn't like it when this one is modified: just the
pointer to the string is passed, and not a copy.
--



Sun, 20 Jul 2003 02:19:09 GMT  
 Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

Quote:

>main ()
>{
>    testsqueeze("The quick brown fox jumped over lazy dogs.",
>                "rstlne");
>}

You're trying to modify a string constant stored in read-only memory.
--



Sun, 20 Jul 2003 02:20:02 GMT  
 Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

Quote:

> So far as I can see you did only two "lame" things:

As others have observed, there was a third problem that
very likely was the most immediate cause of the program's
failure, namely that on some platforms string literals
are not modifiable.  The C standard also allows different
string literals to overlap, e.g. "def" might be the tail
of "abcdef", which can lead to surprises if you are able
to modify the data.
--



Mon, 21 Jul 2003 11:22:36 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2

2. System::Runtime::InteropServices::Out with Arrays

3. Anyone have any fully implemented STRING Class(es)???

4. How to implement TRY & CATCH function

5. Problem trying to implement custom class factory for an Internet Explorer Browser Helper Object

6. Trying to implement a 'splash screen'

7. Problem trying to implement custom class factory for an Internet Explorer Browser Helper Object

8. Error LNK2001 when I try to implement a RUNTIME_CLASS

9. How to implement runtime loadable device drivers (written in C)

10. Trying to implement a 'splash screen'

11. Trying Again - Runtime Error R6002

12. Error LNK2001 when I try to implement a RUNTIME_CLASS

 

 
Powered by phpBB® Forum Software