memcpy出错

shiter
人工智能领域优质创作者
博客专家认证
2013-07-30 05:50:04

char* GBKToUtf8(const char* strGBK)
{
int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, (LPWSTR)wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, (LPSTR)szUtf8, len, NULL,NULL);
return szUtf8;
}


memcpy(file.FileName,GBKToUtf8(file.FileName),sizeof(file.FileName));


这句出错,定位到汇编是
Dword_align:
test edi,11b ;U - destination dword aligned?
jnz short CopyLeadUp ;V - if we are not dword aligned already, align

shr ecx,2 ;U - shift down to dword count
and edx,11b ;V - trailing byte count

cmp ecx,8 ;U - test if small enough for unwind copy
jb short CopyUnwindUp ;V - if so, then jump

rep movsd ;N - move all of our dwords///这里运行不下去了,大概是什么问题?




平时不出问题,运行快了,就开始出问题。
...全文
460 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-08-01
  • 打赏
  • 举报
回复
引用 10 楼 amoyman 的回复:
[quote=引用 9 楼 zhao4zhong1 的回复:] new开销大,还得记得别忘delete,麻烦。
//使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int i,L;
char *p;
void main() {
    for (i=0;i<20000;i++) {
        L=rand();
        p=malloc(L);
        if (NULL==p) {
            printf("malloc error!\n");
            continue;
        }
        memset(p,0,L);
        free(p);
    }
}
//不使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define MAXLEN 30000
int i,L;
char buf[MAXLEN];
char *p;
void main() {
    p=&buf[0];
    for (i=0;i<20000;i++) {
        L=rand();
        if (L>MAXLEN) {
            printf("L>MAXLEN==%d, ignore spilth.\n",MAXLEN);
            L=MAXLEN;
        }
        memset(p,0,L);
    }
}
多线程咋办?[/quote] 参考系统函数rand在多线程时的解决方法:
/***
*rand.c - random number generator
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines rand(), srand() - random number generator
*
*******************************************************************************/

#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>
#include <stdlib.h>

/***
*void srand(seed) - seed the random number generator
*
*Purpose:
*       Seeds the random number generator with the int given.  Adapted from the
*       BASIC random number generator.
*
*Entry:
*       unsigned seed - seed to seed rand # generator with
*
*Exit:
*       None.
*
*Exceptions:
*
*******************************************************************************/

void __cdecl srand (
        unsigned int seed
        )
{
        _getptd()->_holdrand = (unsigned long)seed;
}


/***
*int rand() - returns a random number
*
*Purpose:
*       returns a pseudo-random number 0 through 32767.
*
*Entry:
*       None.
*
*Exit:
*       Returns a pseudo-random number 0 through 32767.
*
*Exceptions:
*
*******************************************************************************/

int __cdecl rand (
        void
        )
{
        _ptiddata ptd = _getptd();

        return( ((ptd->_holdrand = ptd->_holdrand * 214013L
            + 2531011L) >> 16) & 0x7fff );
}
赵4老师 2013-07-31
  • 打赏
  • 举报
回复
改为
#define MAXLEN 8192
char* GBKToUtf8(const char* strGBK)
{ 
    static unsigned short wszUtf8[MAXLEN+1];
    int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0); 
    if (len>=MAXLEN) len=MAXLEN;
    memset(wszUtf8, 0, len * 2 + 2); 
    MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, (LPWSTR)wszUtf8, len);
    len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);
    static char szUtf8[MAXLEN/2*3+1]; 
    if (len>=MAXLEN/2*3) len=MAXLEN/2*3;
    memset(szUtf8, 0, len + 1); 
    WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, (LPSTR)szUtf8, len, NULL,NULL);
    return szUtf8; 
}
阿麦 2013-07-31
  • 打赏
  • 举报
回复
引用 9 楼 zhao4zhong1 的回复:
new开销大,还得记得别忘delete,麻烦。
//使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int i,L;
char *p;
void main() {
    for (i=0;i<20000;i++) {
        L=rand();
        p=malloc(L);
        if (NULL==p) {
            printf("malloc error!\n");
            continue;
        }
        memset(p,0,L);
        free(p);
    }
}
//不使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define MAXLEN 30000
int i,L;
char buf[MAXLEN];
char *p;
void main() {
    p=&buf[0];
    for (i=0;i<20000;i++) {
        L=rand();
        if (L>MAXLEN) {
            printf("L>MAXLEN==%d, ignore spilth.\n",MAXLEN);
            L=MAXLEN;
        }
        memset(p,0,L);
    }
}
多线程咋办?
赵4老师 2013-07-31
  • 打赏
  • 举报
回复
new开销大,还得记得别忘delete,麻烦。
//使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int i,L;
char *p;
void main() {
    for (i=0;i<20000;i++) {
        L=rand();
        p=malloc(L);
        if (NULL==p) {
            printf("malloc error!\n");
            continue;
        }
        memset(p,0,L);
        free(p);
    }
}
//不使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define MAXLEN 30000
int i,L;
char buf[MAXLEN];
char *p;
void main() {
    p=&buf[0];
    for (i=0;i<20000;i++) {
        L=rand();
        if (L>MAXLEN) {
            printf("L>MAXLEN==%d, ignore spilth.\n",MAXLEN);
            L=MAXLEN;
        }
        memset(p,0,L);
    }
}
shiter 2013-07-31
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
改为
#define MAXLEN 8192
char* GBKToUtf8(const char* strGBK)
{ 
    static unsigned short wszUtf8[MAXLEN+1];
    int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0); 
    if (len>=MAXLEN) len=MAXLEN;
    memset(wszUtf8, 0, len * 2 + 2); 
    MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, (LPWSTR)wszUtf8, len);
    len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);
    static char szUtf8[MAXLEN/2*3+1]; 
    if (len>=MAXLEN/2*3) len=MAXLEN/2*3;
    memset(szUtf8, 0, len + 1); 
    WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, (LPSTR)szUtf8, len, NULL,NULL);
    return szUtf8; 
}
引用 6 楼 zhao4zhong1 的回复:
改为
#define MAXLEN 8192
char* GBKToUtf8(const char* strGBK)
{ 
    static unsigned short wszUtf8[MAXLEN+1];
    int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0); 
    if (len>=MAXLEN) len=MAXLEN;
    memset(wszUtf8, 0, len * 2 + 2); 
    MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, (LPWSTR)wszUtf8, len);
    len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);
    static char szUtf8[MAXLEN/2*3+1]; 
    if (len>=MAXLEN/2*3) len=MAXLEN/2*3;
    memset(szUtf8, 0, len + 1); 
    WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, (LPSTR)szUtf8, len, NULL,NULL);
    return szUtf8; 
}
赵老师为啥这样写,不new了,换成static了
shiter 2013-07-31
  • 打赏
  • 举报
回复
引用 3 楼 AnYidan 的回复:
运行快了,出什么问题
debug 下面程序停到我贴出来汇编的最后一行了。
大尾巴猫 2013-07-30
  • 打赏
  • 举报
回复
首先,在函数中,指针wszUtf8是new的,当函数返回前,没有及时delete 第二,在函数中为指针szUtf8申请了内存,然后返回,本来不算错误,只要在外面用完能释放。 但是 memcpy(file.FileName,GBKToUtf8(file.FileName),sizeof(file.FileName)); 这句运行完,还找得到szUtf8指向的内存吗?至少先用一个指针把函数返回的指针保存下,用完再delete吧。 就这2点,运行时间长了肯定出问题。
阿麦 2013-07-30
  • 打赏
  • 举报
回复
引用 1 楼 majia2011 的回复:
有多快?这么不停的new,也不delete 判断下函数返回值吧
++
AnYidan 2013-07-30
  • 打赏
  • 举报
回复
运行快了,出什么问题
max_min_ 2013-07-30
  • 打赏
  • 举报
回复

不知道是不是这个原因!
当你循环调用这个接口的话,建议不要用memset,这个函数很消耗资源的!
建议用calloc 这个可以帮你作清空操作
majia2011 2013-07-30
  • 打赏
  • 举报
回复
有多快?这么不停的new,也不delete 判断下函数返回值吧

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧