memcat fn [英] memcat fn

查看:139
本文介绍了memcat fn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


是否可以编写一个名为memcat的函数,它提供类似于strcat fn的函数
,即我的意思功能

如下: -


void * memcat(void * s1,void * s2);


现在s1应该指向连接内存的开头

区域。现在如strcat如何确定终止内存

位置...... ???

Hi all,

is it possible to write a function named memcat, which offers
functionality similar to that of the strcat fn, i.e i mean a function
on the following lines:-

void * memcat(void *s1, void *s2);

now s1 should point to the beginning of the concatenated memory
region. now as in strcat how to determine the terminating memory
location...???

推荐答案

aa*****@gmail.com 写道:

大家好,


是否可以编写一个名为memcat的函数,它提供类似于strcat fn的函数
,即我的意思是函数

如下: -


void * memcat(void * s1,void * s2);
Hi all,

is it possible to write a function named memcat, which offers
functionality similar to that of the strcat fn, i.e i mean a function
on the following lines:-

void * memcat(void *s1, void *s2);



您需要定义您希望从这样的函数中获得的功能。

You need to define the functionality you expect from such a function.


现在s1应该指向连锁记忆的开始

地区。现在如strcat如何确定终止内存

位置...... ???
now s1 should point to the beginning of the concatenated memory
region. now as in strcat how to determine the terminating memory
location...???



正如那个男人如此难忘地说的那样,这就是问题。

That, as the man said so memorably, is the question.


aa ***** @ gmail.com 写道:

大家好,


是否可以编写一个名为memcat的函数,它提供类似于strcat fn的

功能,即我的意思是一个函数

以下几行: -


void * memcat(void * s1,void * s2);


现在s1应指向连锁记忆的开始

地区。现在如strcat如何确定终止内存

位置...... ???
Hi all,

is it possible to write a function named memcat, which offers
functionality similar to that of the strcat fn, i.e i mean a function
on the following lines:-

void * memcat(void *s1, void *s2);

now s1 should point to the beginning of the concatenated memory
region. now as in strcat how to determine the terminating memory
location...???



让我们从最后一个问题开始:如何确定

结束。两个结尾,真的,因为你需要找到从s1开始的现有作品的结尾

从s2开始的添加作品。另一个memxxx

函数(memset,memchr,...)使用字节计数:

调用者提供额外的参数给出数字
$ b内存区域中的$ b字节。对于memcat,有两个

内存区域,因此有两个计数,函数看起来像


void * memcat(void * s1,size_t n1,void * s2,size_t n2);


但是,让我们不止于此。想一下内部将要做什么内存的memcat会做什么。所有区域1个字节将保持原样,不受影响,新材料将在它们之后添加。所以memcat可能以


void * target =(char *)s1 + n1;


....来获取指针新材料

的地方。 (需要(char *)强制转换,因为你无法对void *指针进行
算术运算。)下一步是什么?

工作的其余部分只是将区域2材料复制到新位置。

因此,memcat的完整实现可能看起来像


void * memcat(void * s1,size_t n1,void * s2,size_t n2){

void * target =(char *)s1 + n1;

memcpy(target,s2,n2);

返回s1;

}


....或者,有些缩写

void * memcat(void * s1,size_t n1,void * s2,size_t n2){

memcpy((char *)s1 + n1,s2,n2) ;

返回s1;

}


(工业强度版本可能会使用const
s2上的
,以及C99编译器的版本也会使用

限制限定符,但大纲也一样。)


In换句话说,memcat只是memcpy,具有不同的

起点!这可能就是为什么它在标准库中不存在的原因:它是一个微不足道的函数变量

'已经提供了。添加它有点像

添加一个sqrt_half函数来计算它的参数的一半的平方根:一个可以通过调用轻松完成的任务

通常的sqrt函数,开头减半。


-

Eric Sosman
es ***** @ ieee-dot-org.inva lid

Let''s begin with the last question: How to determine
the end. Two ends, really, because you need to find the
end of the existing piece that begins at s1 and the end
of the added piece that begins at s2. The other memxxx
function (memset, memchr, ...) use byte counts for this:
the caller provides an extra argument giving the number
of bytes in the memory area. For memcat there are two
memory areas, hence two counts, and the function looks like

void *memcat(void *s1, size_t n1, void *s2, size_t n2);

Let''s not stop there, though. Think for a minute about
what memcat will do, internally. All the area 1 bytes will
remain as they are, untouched, and the new material will be
added right after them. So memcat probably begins with

void *target = (char*)s1 + n1;

.... to get a pointer the the spot where the new material
will go. (The (char*) cast is needed because you can''t do
arithmetic on a void* pointer.) What next? The rest of the
job is just copying the area 2 material to its new position.
So the complete implementation of memcat might look like

void *memcat(void *s1, size_t n1, void *s2, size_t n2) {
void *target = (char*)s1 + n1;
memcpy(target, s2, n2);
return s1;
}

.... or, with some abbreviation
void *memcat(void *s1, size_t n1, void *s2, size_t n2) {
memcpy((char*)s1 + n1, s2, n2);
return s1;
}

(An "industrial-strength" version would probably use const
on s2, and a version for C99 compilers would also use the
restrict qualifier, but the outline would be the same.)

In other words, memcat is just memcpy with a different
starting point! And that''s probably why it doesn''t exist in
the Standard library: it''s a trivial variation on a function
that''s already provided. Adding it would be a little bit like
adding a sqrt_half function that computed the square root of
one-half its argument: A task that''s easily done by calling
the usual sqrt function with a halved argument to begin with.

--
Eric Sosman
es*****@ieee-dot-org.invalid


2007年11月23日星期五09:10:52 -0500,

Eric Sosman< es ***** @ ieee-dot-org.invalidwrote:
On Fri, 23 Nov 2007 09:10:52 -0500,
Eric Sosman <es*****@ieee-dot-org.invalidwrote:
aa*****@gmail.com 写道:

>大家好,

是否可以编写一个名为memcat的函数,它提供类似于strcat fn的功能,即我的意思一个函数
在以下几行: -

void * memcat(void * s1,void * s2);
现在s1应该指向的开头连锁记忆
区域。现在如在strcat中如何确定终止内存的位置...... ???
>Hi all,

is it possible to write a function named memcat, which offers
functionality similar to that of the strcat fn, i.e i mean a function
on the following lines:-

void * memcat(void *s1, void *s2);

now s1 should point to the beginning of the concatenated memory
region. now as in strcat how to determine the terminating memory
location...???



[snip]

[snip]


>

void * memcat(void * s1,size_t n1,void * s2,size_t n2){

memcpy((char *)s1 + n1,s2,n2);

return s1;

}
>
void *memcat(void *s1, size_t n1, void *s2, size_t n2) {
memcpy((char*)s1 + n1, s2, n2);
return s1;
}



当然,如果你这样写,你必须包含string.h,在

这种情况​​你不应该命名你的函数memcat()我只要它

有外部链接,因为那将是一个保留的标识符。


Martien

-

|

Martien Verbruggen |未来的电脑可能不再重量

|超过1.5吨。 - Popular Mechanics,1949

|

of course, if you wrote it like this, you''d have to include string.h, in
which case you shouldn''t be naming your function memcat()i as long as it
has external linkage, as that would be a reserved identifier.

Martien
--
|
Martien Verbruggen | Computers in the future may weigh no more
| than 1.5 tons. -- Popular Mechanics, 1949
|


这篇关于memcat fn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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