我正在尝试复制 x 字节。计算并打印尺寸(见下文)。我使用 memcpy 复制计数的字节数。但结果我有时会得到更长的值。怎么了?
这是一些代码:
size_t symlen = tmpP - cp;
char * triP = malloc(symlen);
printf("mallocated %zu\n", symlen) ;
memcpy (triP, tmpP - (symlen) , symlen);
printf(">>VAL %s<<\n", triP);
Run Code Online (Sandbox Code Playgroud)
这是一些输出,您可以看到,该值长达 15 个字符。
mallocated 15
>>VAL 558657,1033,8144399,814<<
mallocated 15
>>VAL 558657,1033,8144399,814<<
Run Code Online (Sandbox Code Playgroud) 这可能是重复的,但我还没有找到任何其他涉及我的具体情况的问题。
这就是我想做的:
int n = 12;
char s[sizeof(n)];
memcpy(s, (char *)n, sizeof(n));
printf("%d", s);
Run Code Online (Sandbox Code Playgroud)
基本上,我想复制n到s而不需要获取 的地址n。当我运行它时,它给了我一个分段错误。然而,这有效:
printf("%d", (char *)n);
Run Code Online (Sandbox Code Playgroud)
所以我知道问题出在 memcpy 调用上。为什么我不能像这样将 int 复制到 char[] 中?
我试图将一个数组的内容复制到另一个数组,但仅限使用 memcpy() 函数。
这是我的尝试:
int source_array[3] = {1,2,3};
int destination_array[3];
memcpy(destination_array, source_array, sizeof(source_array) * 3);
Run Code Online (Sandbox Code Playgroud)
问题是,在关闭程序时是否需要对重复数组进行delete[]以避免任何内存泄漏?
Cppreference 指出,关于std::memcpy()(强调):
如果对象可能重叠或不可平凡复制,则 的行为
memcpy未指定并且可能未定义。
因此,我总是在使用之前检查以确保对象是可简单复制的memcpy(),如下所示:
#include <type_traits>
static_assert(std::is_trivially_copyable<T>::value, "Type T must "
"be a trivially-copyable type in order to guarantee that `memcpy()` is safe "
"to use on it.");
memcpy(&outputData, &data, sizeof(data));
Run Code Online (Sandbox Code Playgroud)
std::copy()然而,似乎没有这个限制:https://en.cppreference.com/w/cpp/algorithm/copy。
类型必须可简单复制才能不具有未定义行为的限制是否不适用于std::copy()?
另外,我刚刚在我的“placement new”答案中意识到,这让我想知道整个事情,我只是用了memcpy()代替std::memcpy(),而我没有, using namespace std;那么调用了哪个函数?是memcpy()与 不同的实现吗std::memcpy()?
我正在尝试移动结构数组的内容。
#include<stdio.h>
#include<string.h>
typedef struct {
char texts[1024];
}text_i;
text_i textlist[5];
int main()
{
int ind;
strcpy( textlist[0].texts, "hello ..");
strcpy( textlist[1].texts, "world ..");
printf("texts before memcpy\n");
for (ind = 0; ind < 5 ; ind++)
printf("texts ind(%d) is %s \n", ind, textlist[ind].texts);
memcpy( &textlist[1], &textlist[0], 4 * sizeof(text_i));
printf("texts after memcpy\n");
for (ind = 0; ind < 5 ; ind++)
printf("texts ind(%d) is %s \n", ind, textlist[ind].texts);
}
Run Code Online (Sandbox Code Playgroud)
这会将 5 的整个列表打印texts到 string hello ..。
texts before …Run Code Online (Sandbox Code Playgroud) 在我正在编写的代码中,我需要执行以下操作:
以下代码是该概念的一个小示例:
#include <iostream>
#include <map>
#include <cstring>
int main()
{
struct hdr {
int cpi_length = 42;
} ;
void* this_hdr = new hdr;
std::map<int, hdr(*)[20]> SP;
std::cout << sizeof(hdr) << std::endl; // "4"
std::cout << sizeof(this_hdr) << std::endl; // "4"
std::cout << sizeof(SP[0][0]); // "80"
std::memcpy(SP[0][0], this_hdr, sizeof(hdr)); // "Error: access violation"
std::cout << SP[0][0]->cpi_length;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这给我带来了两个问题:1)如何解决尝试使用 memcpy 时的运行时错误,2)一旦解决了错误,我如何确保此代码将执行我所描述的操作?
如命令所示sizeof,被复制的结构实例的大小与结构本身相同,并且(显然)比它被复制到的数组元素的大小小得多。那么为什么会出现访问冲突呢?应该有足够的空闲空间。
我知道为什么我会memcpy在这个实例中使用,为什么我要使用缓冲区指针而不是向量等,这可能看起来很愚蠢。这只是一个可重现的示例,请理解在实际代码中我仅限于这些实现选择。现在,我不再假设我只是犯了一些简单的错误,例如在引用/取消引用指针时混淆了语法。
哪两个更快:?
1.
char* _pos ..;
short value = ..;
*((short*)_pos = va;
Run Code Online (Sandbox Code Playgroud)
2.
char* _pos ..;
short value = ..;
memcpy(_pos, &value, sizeof(short));
Run Code Online (Sandbox Code Playgroud) 阅读以下内容后memcpy(),我继续阅读memmove():
To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes, and should not overlap (for overlapping memory blocks, memmove is a safer approach).(链接)
并且在检查用于说明工作的程序后,memmove()我决定通过使用memcpy()而不是看输出有多么不同来调整它.令我惊讶的是,它们是相同的,即使它是重叠内存块的情况.这是程序和输出,然后我继续描述我的困惑:
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "memmove can be very useful......";
//memmove (str+20,str+15,11);
memcpy(str+20,str+15,11); //Simply used memcpy instead of memmove
puts (str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
产量 memmove …
我有以下一组代码无法找到我获得垃圾值的原因.我的意图是将字节数复制为目标而不管源是否为我的应用程序制作通用副本.但没有得到正确的结果.有没有办法实现这一目标.
int main()
{
char x[6];
char *i="pra";
memset(&x,0,6); //Doing memset
memcpy(&x,i,6);
printf("%x %x %x %x %x %x",x[0],x[1],x[2],x[3],x[4],x[5]);
}
o/p:
70 72 61 0 25 78
Run Code Online (Sandbox Code Playgroud)
我们可以看到0之后的输出是垃圾.但是为什么它正在进行以及它的来源.乳清memset不能正常工作.Pleae有助于了解这一概念的原因.
我想避免memset()在这样的结构上使用:
typedef struct {
int size;
float param1;
} StructA;
typedef struct {
StructA a;
unsigned int state;
float param2;
} Object;
Run Code Online (Sandbox Code Playgroud)
我可以做这样的事情(伪代码,我现在无法检查)?
Object obj;
int *adr = (int*)&obj;
for (int i; i < sizeof(obj); i++) {
*adr++ = 0;
}
Run Code Online (Sandbox Code Playgroud)
它会将obj的每个成员设置为零吗?
编辑:回答评论问题.我一直在研究一些情况(使用uni-type结构),其memset速度比手动初始化慢两倍.所以我会考虑尝试初始化多类型结构.
避免memcpy()也会很好(避免使用<string.h>lib).
memcpy ×10
c ×7
c++ ×4
pointers ×2
arrays ×1
char ×1
dictionary ×1
int ×1
libc ×1
linux ×1
memmove ×1
memory ×1
memory-leaks ×1
memset ×1
optimization ×1
overlapping ×1