的memcpy()从较小的阵列较大的一个 [英] memcpy() from smaller array to larger one

查看:110
本文介绍了的memcpy()从较小的阵列较大的一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到这个问题的任何引用。
我有我需要调整到一个更大的阵列结构的数组。
这两个结构完全初始化(每个单元具有NULL以外的值)

I couldn't find any reference to this question. I have an array of structs which I need to resize into a larger array. both structs are completely initialized (each cell has a value other than NULL)

typedef struct Square {
...
...
}Square;

Square s1[1024];
Square s2[2048];

如果我使用复制的memcpy() S1 S2进会怎么S2会是什么样?我知道它复制字节的数据。
将前1024细胞将是相同的s1和剩余的1024将它们初始化?或者它会影响他们呢?

If I copy using memcpy() s1 into s2, how would s2 would look? I know it copies byte data. will the first 1024 cells would be the same as s1 and the remaining 1024 would be as they initialized? or does it affect them too?

感谢

P.S这里的数组是静态分配的,但我写的只是舒适在这里。我有他们使用的malloc()分配

P.S The arrays here are statically allocated, but I wrote that just for convinience here. I have them allocated using malloc()

推荐答案

如果你这样做:

memcpy(s2, s1, sizeof(s1));

第1024 广场 S IN S2 会从 S1 ,剩下的将是不变(所以如果他们未初始化,他们仍然会未初始化)。

The first 1024 Squares in s2 would be copied from s1, and the rest would be untouched (so if they were uninitialized, they'll still be uninitialized).

请记住,如果他们像你说的堆分配,你不能使用的sizeof 。您可能会发现 realloc的有用的,如果你想要做的一切都是成长的数组。

Remember that if they're heap allocated as you say, you can't use sizeof. You might find realloc useful if all you're trying to do is grow the array.

这篇关于的memcpy()从较小的阵列较大的一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆