473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Run time error: The memory could not be written

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);
}
Nov 13 '05 #1
4 4869
On 9 Nov 2003 07:52:59 -0800
jr********@hotmail.com (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.
Nov 13 '05 #2
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 : bi****@eton.powernet.co.uk
"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
Nov 13 '05 #3
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

Nov 13 '05 #4
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gizmo | last post by:
hello all have been trying to write a Mid() function a bit like the one in vb. i come to compile it and there are no errors however when i run it an error accours and it says the program has to...
1
by: abhilash agarwal | last post by:
hi all!! pl help me i m getting a run time error "memory address .... refer to this location....... memory could not be written at this location....." ..... means some memory address when...
0
by: Morten Gulbrandsen | last post by:
mysql> USE company; Database changed mysql> mysql> DROP TABLE IF EXISTS EMPLOYEE; -------------- DROP TABLE IF EXISTS EMPLOYEE -------------- Query OK, 0 rows affected (0.00 sec)
3
by: DFS | last post by:
I get these errors intermittently using a 3rd party COM object, under VB or Access (in a module). These seem to be happening at a similar instruction location. Instruction at 0x77fcb032...
7
by: José Joye | last post by:
On all my Windows Services (written in C# and for some of them in MC++), I got from time to time crashes at startup (1 out of 10 startup). It occurs really at the time I click on the "Start...
11
by: zhong | last post by:
Error Message: Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention...
1
by: Francesc Guim Bernat | last post by:
Dear colleagues, i'm getting in troubles using one XML library with Visual Studio .NET and Xerces with Xalan. When i execute the code i get the next run time error: "Run-Time Check Failure #2...
13
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing...
1
by: sanjupommen | last post by:
I am in the process of exploring the possibility of providing our products on databases other than Oracle.I am able to migrate the data, procedures etc without too much effort (latest version of DB2...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.