Run time error: The memory could not be written

Discussion in 'C Programming' started by Matt, Nov 9, 2003.

  1. Matt

    Matt Guest

    I am writing a function to reverse a string. It compiles fine, but
    when I run it under VS6.0, it says "The instruction at 0X004011
    reference memory at 0x0042e00. The memory could not be written."

    Any ideas??
    void reverseString(char* s)
    { char temp;
    char* s1 = new char[strlen(s) + 1];
    for (int i=0; i<strlen(s)/2; i++)
    { temp = s;
    s[strlen(s)-1-i] = temp;
    s1 = s[strlen(s)-1-i];
    s1[strlen(s)-1-i] = temp;
    }
    strcpy(s, s1);
    }
    Matt, Nov 9, 2003
    #1
    1. Advertising

  2. Matt

    Mark Gordon Guest

    On 9 Nov 2003 07:52:59 -0800
    (Matt) wrote:

    > I am writing a function to reverse a string. It compiles fine, but
    > when I run it under VS6.0, it says "The instruction at 0X004011
    > reference memory at 0x0042e00. The memory could not be written."
    >
    > Any ideas??
    > void reverseString(char* s)
    > { char temp;
    > char* s1 = new char[strlen(s) + 1];

    ^^^
    new is not part of the C language. comp.lang.c++ is just down the hall
    on the right.

    > for (int i=0; i<strlen(s)/2; i++)
    > { temp = s;
    > s[strlen(s)-1-i] = temp;
    > s1 = s[strlen(s)-1-i];
    > s1[strlen(s)-1-i] = temp;
    > }
    > strcpy(s, s1);
    > }


    I would assume you have to do something to get rid of s1 here or you
    have a memory leak. However, you don't really need s1 at all so you
    could just delete all the lines referring to s1 and tweak your code
    slightly.

    First decide if you are using C++ or C then ask only in the correct
    group for help.

    Also provide a small complete program (including the definition of main
    you are using to test the function) and we (or the nice folks in
    clc++) will stand a better chance of helping you.
    --
    Mark Gordon
    Paid to be a Geek & a Senior Software Developer
    Although my email address says spamtrap, it is real and I read it.
    Mark Gordon, Nov 9, 2003
    #2
    1. Advertising

  3. Matt wrote:

    > I am writing a function to reverse a string. It compiles fine, but
    > when I run it under VS6.0, it says "The instruction at 0X004011
    > reference memory at 0x0042e00. The memory could not be written."
    >
    > Any ideas??
    > void reverseString(char* s)
    > {char temp;
    > char* s1 = new char[strlen(s) + 1];


    Here's your problem right here. That's a syntax error. :)

    I think you might be looking for the people over in comp.lang.c++, where
    your code is topical.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Nov 9, 2003
    #3
  4. On Sun, 09 Nov 2003 07:52:59 -0800, Matt wrote:

    > I am writing a function to reverse a string. It compiles fine, but
    > when I run it under VS6.0, it says "The instruction at 0X004011
    > reference memory at 0x0042e00. The memory could not be written."
    >
    > Any ideas??


    I have an idea. I'm guessing that you are trying to test your
    code by calling it with a string literal like this:

    reverseString("my test string");

    or

    char * test = "my test string";
    reverseString(test);

    Am I right? If so, then you are trying to write into a string
    literal in this function and that is something that you should
    not do.

    Now to the code:

    > void reverseString(char* s)
    > { char temp;
    > char* s1 = new char[strlen(s) + 1];

    ^^^
    "new" does not exist in C. This is C++ code and off-topic here.
    I'll be nice and assume that you were not aware of this. If
    you want to allocate memory in C, use malloc(). The way you
    would do that in this case is to #include <stdlib.h> and then:

    char * s1 = malloc(strlen(s)+1);
    if (s1 == NULL)
    exit(EXIT_FAILURE);

    You are, of course, free to do something more user-friendly than
    exit(EXIT_FAILURE) if malloc fails.

    > for (int i=0; i<strlen(s)/2; i++)
    > { temp = s;
    > s[strlen(s)-1-i] = temp;
    > s1 = s[strlen(s)-1-i];
    > s1[strlen(s)-1-i] = temp;


    This code is totally confused and won't reverse anything. Look
    at what is going on:

    Beginning of loop, i == 0:

    +---+ +---+---+---+---+---+ +---+---+---+---+---+
    temp: | | s: | Y | u | c | k | 0 | s1: | ? | ? | ? | ? | ? |
    +---+ +---+---+---+---+---+ +---+---+---+---+---+

    > temp = s;


    +---+ +---+---+---+---+---+ +---+---+---+---+---+
    temp: | Y | s: | Y | u | c | k | 0 | s1: | ? | ? | ? | ? | ? |
    +---+ +---+---+---+---+---+ +---+---+---+---+---+

    > s[strlen(s)-1-i] = temp;


    +---+ +---+---+---+---+---+ +---+---+---+---+---+
    temp: | Y | s: | Y | u | c | k | Y | s1: | ? | ? | ? | ? | ? |
    +---+ +---+---+---+---+---+ +---+---+---+---+---+

    > s1 = s[strlen(s)-1-i];


    +---+ +---+---+---+---+---+ +---+---+---+---+---+
    temp: | Y | s: | Y | u | c | k | Y | s1: | Y | ? | ? | ? | ? |
    +---+ +---+---+---+---+---+ +---+---+---+---+---+

    > s1[strlen(s)-1-i] = temp;


    +---+ +---+---+---+---+---+ +---+---+---+---+---+
    temp: | Y | s: | Y | u | c | k | Y | s1: | Y | ? | ? | ? | Y |
    +---+ +---+---+---+---+---+ +---+---+---+---+---+

    Work out the rest by yourself and then figure out how to do it
    correctly. Hint: you don't need s1 at all unless you want the
    reversed string to be separate from the original string. If that
    is actually what you want, then return the newly allocated
    string from the function.

    -Sheldon
    Sheldon Simms, Nov 9, 2003
    #4
  5. Matt

    CBFalconer Guest

    Matt wrote:
    >
    > I am writing a function to reverse a string. It compiles fine, but
    > when I run it under VS6.0, it says "The instruction at 0X004011
    > reference memory at 0x0042e00. The memory could not be written."
    >
    > Any ideas??
    > void reverseString(char* s)
    > { char temp;
    > char* s1 = new char[strlen(s) + 1];
    > for (int i=0; i<strlen(s)/2; i++)
    > { temp = s;
    > s[strlen(s)-1-i] = temp;
    > s1 = s[strlen(s)-1-i];
    > s1[strlen(s)-1-i] = temp;
    > }
    > strcpy(s, s1);
    > }


    It shouldn't compile. "new" is a syntax error. There is no
    declaration of strlen, nor strcpy, nor #include of string.h.
    There is no main declaration. Even if these were all taken care
    of it is overly complex. Try something like:

    void reverseString(char* s)
    {
    char temp;
    char *p;

    p = s;
    while (*p) p++;
    while (p > s) {
    p--;
    temp = *p; *p = *s; *s = temp;
    s++;
    }
    } /* untested */

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Nov 9, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve C. Orr, MCSD
    Replies:
    1
    Views:
    547
    reaway lee
    Aug 24, 2003
  2. abhilash agarwal
    Replies:
    1
    Views:
    332
    Peter van Merkerk
    Nov 13, 2003
  3. Pierre Yves
    Replies:
    2
    Views:
    469
    Pierre Yves
    Jan 10, 2008
  4. KaiWen
    Replies:
    102
    Views:
    2,637
    Jorgen Grahn
    Sep 15, 2011
  5. ll
    Replies:
    1
    Views:
    251
    Evertjan.
    Nov 29, 2007
Loading...

Share This Page