memcpy问题;复制错误的块和多次
memcpy issues; copying wrong block and multiple times
#include <iostream>
#include <string.h>
using namespace std;
int sk00(char * a) //use to find length, in characters, of an expression
{
int b = 1, c = 0;
while(b != 0)
{
if (a[c] == '`'){b++;}
else{b--;}
c++;
}
return c;
}
void sk07(char * a)
{
int b = 0; while(a[b]!=0){b++;}b -= 2;
memcpy(a,a+3,b);
memcpy(a+sk00(a),a+sk00(a)+sk00(a+sk00(a)),b);
}
void sk20(char * z)
{
char * a = z + 2;
int b = 0;while(a[b]!=0){b++;}b-=1;
memcpy(a,a+2,b);
int al = sk00(a), bl = sk00(a+al), cl = sk00(a+al+bl);
b=b-(al+bl+cl);
memcpy(a+al+cl+1, a+al, b+cl+bl);
}
int main()
{
char a[] = "``````s`k`sikabcd";
sk20(a+3);
cout << a << "; Final.n";
}
终端输出:pup@aurora-217:~/sk$ g++ sk5.c
pup@aurora-217:~/sk$ ./a.out
``````k`sikakakad; Final.
pup@aurora-217:~/sk$
我期望输出沿着
`````k
sik`sikabcd
Technically you have a problem in sk07 if and when b is larger than 2 or 3, as you cannot use memcpy for cases where the source and target overlaps. You could try memmove instead.
Whether we have an overlap here, for example
memcpy(a+al+cl+1, a+al, b+cl+bl);
的行在可能存在内存位置重叠的情况下,您应该使用memmove函数而不是memcpy例程。在内存位置重叠的情况下,使用memcpy例程被认为是不安全的,因为优化可能会使结果输出完全混乱。
memmove接受与memcpy相同的形参,顺序相同,因此可以用作替换符
相关文章:
- 警告处理为错误这里有什么问题
- Project Euler问题4的错误解决方案
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 类C++中的函数问题(LNK2019和LNK1120错误)
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- posix_spawn():使用posix_scawn()时的错误处理问题
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- 动态类的分段错误(家庭作业问题)
- C 模板错误问题
- isdigit 的分段错误问题
- 用于防止重复和语法错误问题的布尔数组
- 遇到错误问题:在'{'之前,预期的非限定 id 只是一个我需要的类的标头,只是不确定导致错误的原因
- 对象内部结构:解决分割错误问题
- 与我定义的类的成员有关的c++向量数据类型编译器错误问题
- 警告错误问题
- 使用 C++ 的代码块中的链接器错误问题
- 链接器错误问题
- 分段错误问题 # n
- 如何解决支持库中发生的分段错误问题