Access violation when using strcpy?

The compiler placed your destination buffer, s, in read-only memory since it is a constant.

Up vote 0 down vote favorite share g+ share fb share tw.

I've tried reinventing the strcpy C function, but when I try to run it I get this error: Unhandled exception at 0x00411506 in brainf%ck. Exe: 0xC0000005: Access violation writing location 0x00415760. The error occurs in the *dest = *src; line.

Here's the code: char* strcpy(char* dest, const char* src) { char* dest2 = dest; while (*src) { *dest = *src; src++; dest++; } *dest = '\0'; return dest2; } EDIT: Wow, that was fast. Here's the calling code (strcpy is defined in mystring. C): #include "mystring.

H" #include int main() { char* s = "hello"; char* t = "abc"; printf("%s", strcpy(s, t)); getchar(); return 0; } c strcpy link|improve this question edited Mar 13 at 3:15pst38k43366 asked Aug 13 '09 at 21:17Javier Badia1,5821622 90% accept rate.

That code would be helpful. – Jason Aug 13 '09 at 21:18 1 Man, that was a bunch of quick answers. Welcome to the world of overrun buffers.

:) – Craig Aug 13 '09 at 21:22 How are you calling it? – Decker Aug 13 '09 at 21:23 while (*dest++ = *src++); – Mehrdad Afshari Aug 13 '09 at 21:29 I had thought about just putting that, but I wanted to do something a bit more clear. However, I'll probably end up using it.

It would be nice what I'm doing wrong with my current code anyway. – Javier Badia Aug 13 '09 at 21:31.

Char* s = "hello"; char* t = "abc"; printf("%s", strcpy(s, t)); The compiler placed your destination buffer, s, in read-only memory since it is a constant. Char s5; char* t = "abc"; printf("%s", strcpy(s, t)); Should fix this problem. This allocates the destination array on the stack, which is writable.

Thank you. (15 characters) – Javier Badia Aug 13 '09 at 21:37.

The obvious potential problem is that your output buffer doesn't have enough memory allocated, or you've passed in NULL for dest. (Probably not for src or it would have failed on the line before. ) Please give a short but complete program to reproduce the problem, and we can check... Here's an example which goes bang for me on Windows: #include char* strcpy(char* dest, const char* src) { char* dest2 = dest; while (*src) { *dest = *src; src++; dest++; } *dest = '\0'; return dest2; } void main() { char *d = malloc(3); strcpy(d, "hello there this is a longish string"); } Note that in this case I had to exceed the actual allocated memory by a fair amount before I could provoke the program to die - just "hello" didn't crash, although it certainly could depending on various aspects of the compiler and execution environment.

The calling code doesn't care about a return value and the "input" parameter "d" is left high and dry. – Craig Aug 13 '09 at 21:28 I was just copying the code from the OP... – Jon Skeet Aug 13 '09 at 21:30 The exception text says it's a write-access exception to an address that is close to the code address - so dest is a global or a constant in the executable – Michael Aug 13 '09 at 21:34 1 @Craig: strcpy should return a copy of its first parameter, it's what the language standard requires. – Charles Bailey Aug 13 '09 at 21:40.

Your strcpy() is fine. You are writing to read-only memory. See this description here.

If you had written this, you'd be fine: #include "mystring. H" #include int main() { char s = "hello"; char t = "abc"; printf("%s", strcpy(s, t)); getchar(); return 0; }.

No that does not matter. It's as Michael has written. The stuf was places in read-only sections.

You can change that in gcc AFAIKT, but I think changing that is a really poor idea – Friedrich Oct 21 '09 at 16:00.

There is a problem with calling of your reinvented strcpy routine in the main routine, both character array: char* s = "hello"; char* t = "abc"; will land into memory READ ONLY segment at compile time. As you're trying to write to memory pointed by s in the routine strcpy, and since it points to a location in a READ ONLY segment, it will be caught, and you'll get an exception. These strings are READ ONLY!

There's nothing to stop strcpy() writing off the end of dest and that's what the crash you're seeing is telling you about.

Make sure dest has it's memory allocated before calling that function.

Besides that, the least you could do is check for null pointers, like if (!dest ||! Source) { /* do something, like return NULL or throw an exception */ } on function entry. The code looks OK.

Not very safe, but OK.

There are several errors. You don't allocate a return buffer that can hold the copied string. You don't check to see if src is null before using *src You are both tring to get the answer in a parameter and return the value.

Do one or the other. You can easily overrun the dest buffer. Good luck.

Not one of these 'errors' is applicable to the OP's code. – hughdbrown Aug 13 '09 at 21:35.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions