使用 C 将其他结构中的值存储在结构内时出现分段错误

发布于 2024-12-01 07:37:38 字数 416 浏览 4 评论 0原文

我有两个结构

typedef struct profile_datagram_t
{
    unsigned char *src;
    unsigned char *dst;
    unsigned char ver;
    unsigned char n;
    struct profile_t profiles[MAXPROFILES];
} header;

header outObj;

struct pearson_record
{
    unsigned char *src;
};

现在我想将 outObj.src 中的值 memcpy 到 struct pearson_record 的 unsigned char *src 中。

如何做到这一点?任何类型的例子或任何帮助都会有很大的帮助。提前致谢。

I have two structures

typedef struct profile_datagram_t
{
    unsigned char *src;
    unsigned char *dst;
    unsigned char ver;
    unsigned char n;
    struct profile_t profiles[MAXPROFILES];
} header;

header outObj;

struct pearson_record
{
    unsigned char *src;
};

Now i want to memcpy the value inside outObj.src into unsigned char *src of struct pearson_record.

How to do this?? Any kind of example or any help would be of great help. Thanks in advance.

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

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

发布评论

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

评论(4

对岸观火 2024-12-08 07:37:38

假设 outObj.src 指向包含空终止字符串的有效内存:

struct person_record x;
x.src = malloc(strlen(outObj.src) + 1);  // +1 for the null terminator!
strcpy(x.src, outObj.src);               // could have used strncpy as well

Assuming that outObj.src points to valid memory which contains a null-terminated string:

struct person_record x;
x.src = malloc(strlen(outObj.src) + 1);  // +1 for the null terminator!
strcpy(x.src, outObj.src);               // could have used strncpy as well
ぇ气 2024-12-08 07:37:38

需要意识到的重要一点是,结构体包含指向字符串的指针;它们不包含任何字符串本身的空间。您不能使用 memcpy() 因为没有任何目标内存可供复制。一种方法是使用 strdup():

header h;
struct pearson_record p;
p.src = "the source";
h.src = strdup(p.src);

The important thing to realize is that the structs contain pointers to character strings; they don't contain any space for the character strings themselves. You can't use memcpy() because there isn't any destination memory to copy into. One way to do this would be to use strdup():

header h;
struct pearson_record p;
p.src = "the source";
h.src = strdup(p.src);
时光病人 2024-12-08 07:37:38
outObj.src = (unsigned char *)malloc(10 * sizeof(char));
pearson_record = (unsigned char *)malloc(10 * sizeof(char));
memcpy(pearson_record.src, outObj.src, 10);
outObj.src = (unsigned char *)malloc(10 * sizeof(char));
pearson_record = (unsigned char *)malloc(10 * sizeof(char));
memcpy(pearson_record.src, outObj.src, 10);
狼亦尘 2024-12-08 07:37:38

当您创建 outObjpearson_record 实例时,您拥有的指针不指向任何内容。在使用它们之前,您必须使它们指向内存中的某个缓冲区。

outObj.src = malloc(length);
// do something with outObj.src

struct pearson_record prec;
prec.src = malloc(length);
memcpy(prec.src, outObj.src, length);

// do something with them

// free memory allocated by malloc
free(outObj.src);
free(prec.src);

其中 length 是数据的长度。

When you created outObj or an instance of pearson_record, you have pointers that don't point to anything. You have to make them point to some buffer in memory before using them.

outObj.src = malloc(length);
// do something with outObj.src

struct pearson_record prec;
prec.src = malloc(length);
memcpy(prec.src, outObj.src, length);

// do something with them

// free memory allocated by malloc
free(outObj.src);
free(prec.src);

where length is the length of the data.

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