首页技术copy函数,copyfile函数用法

copy函数,copyfile函数用法

编程之家2026-06-26790次浏览

大家好,如果您还对copy函数不太了解,没有关系,今天就由本站为大家分享copy函数的知识,包括copyfile函数用法的问题都会给大家分析到,还望可以解决大家的问题,下面我们就开始吧!

copy函数,copyfile函数用法

strncpy()函数的功能

strcpy,strncpy,strlcpy地用法

好多人已经知道利用strncpy替代strcpy来防止缓冲区越界。

但是如果还要考虑运行效率的话,也许strlcpy是一个更好的方式。

1. strcpy

我们知道,strcpy是依据/0作为结束判断的,如果 to的空间不够,则会引起 buffer overflow。strcpy常规的实现代码如下(来自 OpenBSD 3.9):

char*

copy函数,copyfile函数用法

strcpy(char*to, const char*from)

{

char*save= to;

for(;(*to=*from)!='/0';++from,++to);

return(save);

}

copy函数,copyfile函数用法

但通常,我们的 from都来源于用户的输入,很可能是非常大的一个字符串,因此 strcpy不够安全。

2. strncpy

在 ANSI C中,strcpy的安全版本是 strncpy。

char*strncpy(char*s1, const char*s2, size_t n);

但 strncpy其行为是很诡异的(不符合我们的通常习惯)。标准规定 n并不是 sizeof(s1),而是要复制的 char的个数。一个最常见的问题,就是 strncpy并不帮你保证/0

结束。

char buf[8];

strncpy( buf,"abcdefgh", 8);

看这个程序,buf将会被"abcdefgh"填满,但却没有/0结束符了。

另外,如果 s2的内容比较少,而 n又比较大的话,strncpy将会把之间的空间都用/0填充。这又出现了一个效率上的问题,如下:

char buf[80];

strncpy( buf,"abcdefgh", 79);

上面的 strncpy会填写 79个 char,而不仅仅是"abcdefgh"本身。

strncpy的标准用法为:(手工写上/0)

strncpy(path, src, sizeof(path)- 1);

path[sizeof(path)- 1]='/0';

len= strlen(path);

3. strlcpy

// Copy src to string dst of size siz. At most siz-1 characters

// will be copied. Always NUL terminates(unless siz== 0).

// Returns strlen(src); if retval>= siz, truncation occurred.

size_t

strlcpy(char*dst, const char*src, size_t siz);

而使用 strlcpy,就不需要我们去手动负责/0了,仅需要把 sizeof(dst)告之 strlcpy即可:

strlcpy(path, src, sizeof(path));

len= strlen(path);

if( len>= sizeof(path))

printf("src is truncated.");

并且 strlcpy传回的是 strlen(str),因此我们也很方便的可以判断数据是否被截断。

[*一点点历史*]

strlcpy并不属于 ANSI C,至今也还不是标准。

strlcpy来源于 OpenBSD 2.4,之后很多 unix-like系统的 libc中都加入了 strlcpy函数,我个人在 FreeBSD、Linux里面都找到了 strlcpy。(Linux使用的是 glibc,

glibc里面有 strlcpy,则所有的 Linux版本也都应该有 strlcpy)

但 Windows下是没有 strlcpy的,对应的是strcpy_s函数

///////////////////////////////////////////////////////////////////////////

strncpy

原型:extern char*strncpy(char*dest, char*src, int n);

用法:#include<string.h>

功能:把src所指由NULL结束的字符串的前n个字节复制到dest所指的数组中。

说明:

如果src的前n个字节不含NULL字符,则结果不会以NULL字符结束。

如果src的长度小于n个字节,则以NULL填充dest直到复制完n个字节。

src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。

返回指向dest的指针。

举例:

// strncpy.c

#include<syslib.h>

#include<string.h>

main()

{

char*s="Golden Global View";

char*d="Hello, GGV Programmers";

char*p=strdup(s);

clrscr();

textmode(0x00);// enable 6 lines mode

strncpy(d,s,strlen(s));

printf("%s/n",d);

strncpy(p,s,strlen(d));

printf("%s",p);

getchar();

return 0;

}

------------------------------

memcpy

原型:extern void*memcpy(void*dest, void*src, unsigned int count);

用法:#include<string.h>

功能:由src所指内存区域复制count个字节到dest所指内存区域。

说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。

举例:

// memcpy.c

#include<syslib.h>

#include<string.h>

main()

{

char*s="Golden Global View";

char d[20];

clrscr();

memcpy(d,s,strlen(s));

d[strlen(s)]=0;

printf("%s",d);

getchar();

return 0;

}

面试题:Block为什么用Copy修饰

Block的内存地址显示在栈区,栈区的特点就是创建的对象随时可能被销毁,一旦被销毁后续再次调用空对象就可能会造成程序崩溃,在对block进行copy后,block存放在堆区.所以在使用Block属性时使用Copy修饰,而在ARC模式下,系统也会默认对Block进行copy操作

如果是堆中的block,也就是copy修饰的block。他的生命周期就是随着对象的销毁而结束的。只要对象不销毁,我们就可以调用的到在堆中的block。

这就是为什么我们要用copy来修饰block。因为不用copy修饰的访问外部变量的block,只在他所在的函数被调用的那一瞬间可以使用。之后就消失了。

block内部没有调用外部局部变量时存放在全局区(ARC和MRC下均是)

block使用了外部局部变量,这种情况也正是我们平时所常用的方式。MRC:Block的内存地址显示在栈区,栈区的特点就是创建的对象随时可能被销毁,一旦被销毁后续再次调用空对象就可能会造成程序崩溃,在对block进行copy后,block存放在堆区.所以在使用Block属性时使用copy修饰。但是ARC中的Block都会在堆上的,系统会默认对Block进行copy操作

用copy,strong修饰block在ARC和MRC都是可以的,都是在堆区

补充:一个block要使用self,会处理成在外部声明一个weak变量指向self,然而为何有时会出现在block里又声明一个strong变量指向weakSelf?

原因:block会把写在block里的变量copy一份,如果直接在block里使用self,(self对变量默认是强引用)self对block持有,block对self持有,导致循环引用,所以这里需要声明一个弱引用weakSelf,让block引用weakSelf,打破循环引用。

而这样会导致另外一个问题,因为weakSelf是对self的弱引用,如果这个时候控制器pop或者其他的方式引用计数为0,就会释放,如果这个block是异步调用而且调用的时候self已经释放了,这个时候weakSelf已就变成了nil。

当控制器(也可以是其他的控件)pop回来之后(或者一些其他的原因导致释放),网络请求完成,如果这个时候需要控制器做出反映,需要strongSelf再对weakSelf强引用一下。

但是,你可能会疑问,strongSelf对weakSelf强引用,weakSelf对self弱引用,最终不也是对self进行了强引用,会导致循环引用吗。不会的,因为strongSelf是在block里面声明的一个指针,当block执行完毕后,strongSelf会释放,这个时候将不再强引用weakSelf,所以self会正确的释放。

c语言中的merge函数

在C语言中,merge.c实现的是合并的方法

一、归并排序算法

算法的递推关系:一个大的数列需要排序,把它从中间分成两部分,每一部分归并排序,然后把排好序的这两个部分再合并起来(合并的时候要按顺序合并)。

算法的Base Case:如果分成的这部分只有一个数,那么这个部分就不用再排序(看做已经排好序的)。

实现这个算法用了三个函数,每个函数在一个文件中,分别为:merge.c sort.c和 main.c,其中merge.c实现的是合并的方法,sort.c实现的是排序的方法,main.c是一个测试实例。还有三个头文件,分别指出了函数原型。

merge.c:

/*This is a merge program.

* Given an integer ARRAY and three numbers which indicate the begain

*and the end of two subarrays, merge the two subarrays to a bigger

*one. The two subarrays are alrealy sorted from small to big.

* For example, given an array a[10] and three numbers 0, 3 and 5. The

*first array is from a[0] to a[2], the seconde array is from a[3] to

*a[4]. The number 3 and 5 are the upper side. This program merge the

*two arrays together.

*

*Author: Eric

*Time: 2011.01.08

*/

#include<stdio.h>

#include<stdlib.h>

#include"main.h"

void merge(int*a, int idxa, int idxb, int idxc)

{

int i= idxa, j= idxb, k= 0;

int total= idxc-idxa;

//int temp[total]={0};

int*temp=(int*)malloc(sizeof(int)* total);

if(temp== NULL)

{

fprintf(stderr,"malloc error in merge function\n");

return;

}

while(i< idxb&& j< idxc)

{

if(a[i]< a[j])

temp[k++]= a[i++];

else

temp[k++]= a[j++];

}

if(i== idxb)

{

while(j< idxc)

temp[k++]= a[j++];

}

else if(j== idxc)

{

while(i< idxb)

temp[k++]= a[i++];

}

/*Copy the temp to the sorce array*/

for(i= 0, k= idxa; i< total; k++, i++)

a[k]= temp[i];

free(temp);

}

#ifndef MAIN

/*For test*/

int main()

{

int a[10];

int i= 0;

int idxa=1, idxb=5, idxc=8;

printf("Please input 10 numbers to the array:");

for(i= 0; i< 10; i++)

scanf("%d",&a[i]);

printf("Three indexes are%d,%d and%d.\nThe first subarray is:", idxa, idxb, idxc);

for(i= idxa; i< idxb; i++)

printf("%d", a[i]);

printf("\nThe second subarray is:");

for(i= idxb; i< idxc; i++)

printf("%d", a[i]);

printf("\n");

merge(a, idxa, idxb, idxc);

printf("The merged array is:");

for(i= idxa; i< idxc; i++)

printf("%d", a[i]);

printf("\n");

return 0;

}

#endif

merge.h:

/*Author: Eric

*Time: 2011.01.08

*/

void merge(int*a, int idxa, int idxb, int idxc);

sort.c:

/*This is a function for sorting an array useing merge.c

*

*Author: Eric

*Time: 2011.01.08

*/

#include<stdio.h>

#include"main.h"

#include"merge.h"

/*Sort array a, from a[begin] to a[upend-1]*/

void sort(int*a, int begin, int upend)

{

int n= upend- begin;/*the number to be sorted*/

/*The first array is a[idxa] to a[idxb-1]. The second is a[idxb] to a[idxc-1]*/

int idxa= begin,

idxb=((begin+upend)%2== 0)?(begin+upend)/2:(begin+upend+1)/2,

idxc= upend;

if(n< 2)

{

printf("The array elements are less than two. No need to sort\n");

return;

}

else if(n== 2)

merge(a, idxa, idxb, idxc);

else

{

if(idxb-idxa> 1)

sort(a, idxa, idxb);

if(idxc-idxb> 1)

sort(a, idxb, idxc);

merge(a, idxa, idxb, idxc);

}

}

#ifndef MAIN

#define MAIN

/*For test*/

int main()

{

int a[10]={1, 4, 8, 5, 10, 25, 54, 15, 12, 2};

int i= 0;

sort(a, 0, 10);

printf("The sorted array is:");

for(i= 0; i< 10; i++)

printf("%d", a[i]);

printf("\n");

return 0;

}

#endif

关于copy函数到此分享完毕,希望能帮助到您。

英雄联盟战绩查询?lol个人战绩查询网页版王者2020七月 王者荣耀2020