Access violation when using strcpy?

拜拜、爱过 提交于 2019-12-17 19:54:11

问题


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 <stdio.h>

int main() {
    char* s = "hello";
    char* t = "abc";
    printf("%s", strcpy(s, t));
    getchar();
    return 0;
}

回答1:


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 s[5];
char* t = "abc";
printf("%s", strcpy(s, t));

Should fix this problem. This allocates the destination array on the stack, which is writable.




回答2:


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 <stdlib.h>

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.




回答3:


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 <stdio.h>

int main() {
    char s[] = "hello";
    char t[] = "abc";
    printf("%s", strcpy(s, t));
    getchar();
    return 0;
}



回答4:


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!




回答5:


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




回答6:


Probably an issue with the caller: did you check the dest pointer? Does it point to something valid or just garbage? 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.




回答7:


There are several errors.

  1. You don't allocate a return buffer that can hold the copied string.
  2. You don't check to see if src is null before using *src
  3. You are both tring to get the answer in a parameter and return the value. Do one or the other.
  4. You can easily overrun the dest buffer.

Good luck.




回答8:


when ever the code starting execution(generaly it starts from main function). here the code means sequence of execution.so, when the process(sequence of execution) starts , the PCB(process control block) is created,the pcb having complete infromation about the process like process address space,kernal stack,ofdt table like this.

in your code

char* s = "hello";
char* t = "abc";

this is the what you have taken inputs of two strings like this.

here, the the strings(which means double quoted) which are present in text section of the process address space . here text section is the one of the section which is present in the process address space and text section only having the permissions of read-only. that is why when you trying to modify the source string/destination string, we MUST NOT allowable to change the whatever data is present in the text setion. so, this is what the reason for your code you need to be CAUTIOUS. hope you understand.



来源:https://stackoverflow.com/questions/1274626/access-violation-when-using-strcpy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!