C memcpy 错误 - 在 Windows XP 上无法写入内存

发布于 2024-09-15 10:18:59 字数 1125 浏览 1 评论 0原文

我有一个 C 程序,它将文件逐行加载到一个字符串中,然后使用以下代码将此字符串 memcpy 加载到另一个字符串指针中:

   typedef struct inmod_struct {
       Int32 ReturnCode;
       Int32 Length;
       char  Body[ROWSIZE];
    } inmdtyp,*inmdptr;

    inmdptr inmodptr;
    char line[600];
    int doit()
    {
        char *p;
        p = inmodptr->Body;
        ReadFile();  //reads a line of the file into **line** string variable


    memcpy(p, line, strlen(line));
    p += strlen(line);

    inmodptr->ReturnCode = 0;
    inmodptr->Length = p - inmodptr->Body;

    reccnt++;
}

但是当我在 Windows XP SP3 计算机上执行上述程序时,我收到一个错误消息框说:

“<内存地址>”处的指令引用的内存位于“<内存地址>”。内存无法被“写入”。单击“确定”终止...

当我尝试执行类似的内存操作时,我遇到了类似的问题,它给出了类似的问题。 strcpy 最适合我,但 strcpy 会忽略读取直到该行的 NUL 字符,但即使在该行中的 NUL 字符之后我也会读取更多字符。任何人都可以为此提供解决方法或任何解决方案吗?

环境:Windows XP SP3,GCC编译器。

我什至在solaris unix 上尝试了类似的代码编译和使用,我在那里遇到了同样的问题。

我在使用一些 OpenCV Python、C 示例时也遇到了类似的错误。

编辑: 对不起大家..我已经初始化了指针 p p = inmodptr->Body; inmodptr 是一个结构。我可以确认指针初始化不是问题。为了清晰起见,发布了完整的代码

I have a C program which loads a file line-by-line into one string and then memcpy's this string into another string pointer using this code:

   typedef struct inmod_struct {
       Int32 ReturnCode;
       Int32 Length;
       char  Body[ROWSIZE];
    } inmdtyp,*inmdptr;

    inmdptr inmodptr;
    char line[600];
    int doit()
    {
        char *p;
        p = inmodptr->Body;
        ReadFile();  //reads a line of the file into **line** string variable


    memcpy(p, line, strlen(line));
    p += strlen(line);

    inmodptr->ReturnCode = 0;
    inmodptr->Length = p - inmodptr->Body;

    reccnt++;
}

But when I execute the above program on my Windows XP SP3 machine, I get an error message box saying:

The instruction at "<memory address>" referenced memory at "<memory address>". The memory could not be "written". Click OK to terminate....

I faced similar problem when I tried to do similar memory operations it is giving similar problems. strcpy works best for me but strcpy omits reads till NUL character of the line but I will have some more characters to be read even after NUL in the line. Can anybody please provide a workaround or any soultion for this.

Environment: Windows XP SP3, GCC compiler.

I even tried similar code compiling and usage on solaris unix and I am facing same issue there.

I have been facing similar errors with some of the OpenCV Python, C samples also.

Edit:
Sorry guys.. I have that pointer p initialized p = inmodptr->Body; inmodptr is a structure. and I can confirm that the pointer initialization is not the issue. Posted the full code for clarity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月牙弯弯 2024-09-22 10:18:59

您需要分配一个缓冲区并让 p 保存该缓冲区的地址。然后你可以像你一样使用 p

指针是保存内存地址的变量。您的指针保存一些未初始化的值,该值可能未指向有效的内存地址。

char *p = malloc(strlen(line) + 1);

如果您要存储字符串,则额外的 +1 用于 NULL 终止的字符 \0。如果由于某种原因您使用 memcpy 而不是 strcpy,则必须手动以 NULL 终止缓冲区。

另外,如果您malloc,请务必在使用完毕后free。您还可以简单地堆栈分配一个字符数组并使用它来代替 p

You need to allocate a buffer and have p hold the address of that buffer. Then you can use p like you did.

A pointer is a variable that holds a memory address. Your pointer holds some uninitialized value which probably doesn't point to a valid memory address.

char *p = malloc(strlen(line) + 1);

If you are storing a string the extra +1 is for the NULL terminated char \0. If you are using memcpy instead of strcpy for some reason, you'll have to manually NULL terminate your buffer.

Also if you malloc be sure to free once you are done using it. You could also simply stack allocate an array of characters and use that instead of p.

够钟 2024-09-22 10:18:59

此行:

inmdptr inmodptr;

声明一个指针 inmodptr,但不初始化它 - 它指向某个随机内存位置。

您需要使其指向可以保存 struct inmod_struct 对象的实际实例的位置 - 例如:

inmdtyp inmod;
inmdptr inmodptr = &inmod;

This line:

inmdptr inmodptr;

declares a pointer inmodptr, but doesn't initialise it - it's pointing at some random memory location.

You need to make it point somewhere that can hold an actual instance of the struct inmod_struct object - for example:

inmdtyp inmod;
inmdptr inmodptr = &inmod;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文