dcy_xm 2016-07-19 06:36 采纳率: 40%
浏览 901
已结题

c/c++ 结构体分配内存问题

struct tftp_rrq {  
short opcode;  
char *req;

};

struct tftp_conn{
int type;
int sock;
char *mode;
char *fname;
char *hostname;
struct sockaddr_in addr;
socklen_t addrlen;
FILE *fp;
int blocknr;
char msgbuf[512];
};

struct tftp_rrq* p_rrq = (struct tftp_rrq *)malloc(sizeof(struct tftp_rrq));
p_rrq->req = (char *)malloc(strlen(t->fname)+strlen(t->mode)+2);

p_rrq->opcode = htons(1);

memcpy(p_rrq->req, t->fname, strlen(t->fname));  
memcpy(p_rrq->req + strlen(t->fname) + 1, t->mode,  
    strlen(t->mode));

    其中t是struct tftp_conn *t。这个是没有问题的。

        p_rrq->opcode = htons(1);
        memcpy(p_rrq->req, t->fname, strlen(t->fname));  
        memcpy(p_rrq->req + strlen(t->fname) + 1, t->mode,  strlen(t->mode));
        在赋值的时候出的问题。上面三句都有问题,求大神
  • 写回答

3条回答 默认 最新

  • oyljerry 2016-07-19 07:03
    关注

    你req分配的空间大小是否正确。能否装下需要memcpy的数据

    评论

报告相同问题?