- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试移动可能重叠的内存,对 *src 和 *dst 使用负增量或正增量,并且不使用大型临时缓冲区。
我正在尝试为 memmove()
函数找到一个有效的替代方案,大致如下:
smart_memmove(char *dst, const char *src, size_t num, int dst_inc, int src_inc);
dst 和 src 可以重叠,并且 dst_inc 和 src_inc 可以是任何正整数或负整数(负增量表示在内存中向后移动,起始指针位于顶部)。我想避免使用较大的临时缓冲区,即使这意味着执行速度会降低。
举个例子,从内存位置 0 开始,每隔一个字节递增一次,将 10 个字节复制到内存位置 17,并向后计数 1:
smart_memmove(17, 0, 10, -1, 2);
另一个例子是,通过使用以下参数调用 smart_memmove 来反转内存位置 6、9、12、15、18、21、24、27、30、33 中的 10 个字节序列:
smart_memmove(6, 33, 10, 3, -3); /* or... smart_memmove(33, 6, 10, -3, 3); */
最佳答案
您也可以更喜欢 memcpy() 函数。
void * memcpy ( void * destination, const void * source, size_t num );
目的地
指向要复制内容的目标数组的指针,类型转换为 void* 类型的指针。
来源
指向要复制的数据源的指针,类型转换为 const void* 类型的指针。
数字
要复制的字节数。
size_t
是无符号整数类型。
将 num 个字节的值从源指向的位置直接复制到目标指向的内存块。
源指针和目标指针所指向的对象的基础类型与该函数无关;结果是数据的二进制副本。
该函数不会检查源中是否有任何终止空字符 - 它始终精确复制 num 个字节。
为避免溢出,目标参数和源参数指向的数组大小至少应为 num 个字节,并且不应重叠(对于重叠的内存块,memmove 是更安全的方法)。
示例程序
/* memcpy example */
#include <stdio.h>
#include <string.h>
struct {
char name[40];
int age;
} person, person_copy;
int main ()
{
char myname[] = "user613994";
/* using memcpy to copy string: */
memcpy ( person.name, myname, strlen(myname)+1 );
person.age = 46;
/* using memcpy to copy structure: */
memcpy ( &person_copy, &person, sizeof(person) );
printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );
return 0;
}
关于c - 是否可以有效地替代 memmove() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7721199/
下面的代码片段显示了 memmove() 的实现。 void my_memmove(void* dest, const void* src, size_t size) { unsigned i
在 CLRS 第 2 章中有一个练习,询问是否可以将插入排序的最坏情况运行时间改进为 O(n lg n)。我看到了this question发现做不到。 最坏情况下的复杂性无法改善,但与单独移动数组元
我正在使用memmove(),但目标似乎正在覆盖源,或者也许我不明白覆盖是什么。我有一个 char 数组(目标),然后是一个指向目标的指针,该指针位于 vector 内部。 char destinat
根据 C11 N1570 standard draft : 7.24.2.2 “memmove 函数”: The memmove function copies n characters from t
移动 2D double 组时遇到问题。在下面的代码中,我有 双权重[wt][2] 我可以将 Weights[0][0] 移动到 Weights[0][1],但只能在要移动的字节数末尾添加“*2”。为
我正在尝试读取和处理二进制文件。由于其编写方式,我以字符数组的形式读取它。当我处理完数据后。我将剩余数据移至字符缓冲区的开头以避免缓冲区溢出。我正在为此使用 memmove,但这似乎会导致堆增长,这表
假设有一个指向浮点值数组的指针:float *source; 并且我们知道它的大小为 int sourcesize; 有一个已经实现的函数,它从 inputVec 添加一个元素到 souce 数组:
我正在使用 memmove 将 std::string 元素向右移动一位。目的地的第一个地点是唯一一个乱七八糟、堆满垃圾的地方。我使用 memmove 而不是 strcpy 因为我在需要模板的类中使用
假设我有一个这样的整数数组 #define MAX 5 int bar [MAX] = {0}; int foo [MAX] = {3,1,0,0,0}; 现在我想移动这个数组,使所有空条目都在左边,
我想使用返回结果字符串的函数连接两个字符串。它会是这样的: char *String_Concat (char *String_1, char *String_2) { char *Strin
memmove 并没有真正移动内存,不是吗?它只是将内存从一个区域复制到另一个区域,并允许这两个区域重叠。我问这个问题是因为我只是想知道为什么这个 fnc 以非常误导的方式被调用。 因为我知道当某物从
我正在阅读关于 memcpy 和 memmove 的 c++ 引用资料,它们似乎在做同样的事情,除了 memmove 有一个特定的think called(允许目的地和来源重叠)。 什么是重叠以及何时
标准库提供了std::copy,可以看作是C的memcpy()的泛化/类化。它还保持了 memcpy() 的要求,范围 [first, last) 与范围 [d_first , d_first + s
我编写了以下函数,将给定的完整路径拆分为目录、文件名和扩展名。 #include #include #include struct path_info { char *directory
我在 StackOverflow 上进行了搜索,但找不到我想要做的事情。我想将指针 Items 复制到指针 COPYTO。然后就可以调用COPYTO->x。 #include typedef str
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Bus error troubleshooting 要从字符串中删除重复项,这是我编写的程序: #include #
当我编写时,我的代码不会崩溃: char s[44] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; memmove(s, "asdf", 5); 但
我正在尝试移动可能重叠的内存,对 *src 和 *dst 使用负增量或正增量,并且不使用大型临时缓冲区。 我正在尝试为 memmove() 函数找到一个有效的替代方案,大致如下: smart_memm
我试图了解 memmove 是如何工作的。我举一个例子,我以这种方式在内存中保存数据。 Start at 0 First Memory Block(A) of size 10 Hence A->(0,
我有以下代码,我试图在其中创建一个长度为 7 字节的缓冲区,然后将网络字节顺序中的整数与缓冲区中的 char 数组一起放入,最后我不应该有 Null 字符我的 sprintf() 缓冲区,所以我使用
我是一名优秀的程序员,十分优秀!