Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Run time error: The memory could not be written

Reply
Thread Tools

Run time error: The memory could not be written

 
 
Matt
Guest
Posts: n/a
 
      11-09-2003
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[i];
s[strlen(s)-1-i] = temp;
s1[i] = s[strlen(s)-1-i];
s1[strlen(s)-1-i] = temp;
}
strcpy(s, s1);
}
 
Reply With Quote
 
 
 
 
Mark Gordon
Guest
Posts: n/a
 
      11-09-2003
On 9 Nov 2003 07:52:59 -0800
http://www.velocityreviews.com/forums/(E-Mail Removed) (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[i];
> s[strlen(s)-1-i] = temp;
> s1[i] = 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.
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-09-2003
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 : (E-Mail Removed)
"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
 
Reply With Quote
 
Sheldon Simms
Guest
Posts: n/a
 
      11-09-2003
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[i];
> s[strlen(s)-1-i] = temp;
> s1[i] = 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[i];


+---+ +---+---+---+---+---+ +---+---+---+---+---+
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[i] = 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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-09-2003
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[i];
> s[strlen(s)-1-i] = temp;
> s1[i] = 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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generally, are the programs written by C++ slower than written by C10% ? KaiWen C++ 102 09-14-2011 11:12 PM
The instruction at "0x009a96bc" referenced memory at "0x00000000". The memory could not be "written". Double Agent 1118 Computer Support 8 09-04-2009 10:50 AM
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
memory could not be written?!?! queeniesara Computer Support 9 02-27-2005 06:54 AM
Re: Can a usercontrol written in C# be used in Web Forms that is written in VB.Net? Steve C. Orr, MCSD ASP .Net 1 08-24-2003 12:06 AM



Advertisments