Run time error: The memory could not be written

M

Matt

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);
}
 
M

Mark Gordon

On 9 Nov 2003 07:52:59 -0800
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.
 
R

Richard Heathfield

Matt said:
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.
 
S

Sheldon Simms

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
 
C

CBFalconer

Matt said:
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 */
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top