何时使用函数strncpy或memmove与? [英] When to use strncpy or memmove?

查看:125
本文介绍了何时使用函数strncpy或memmove与?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(GCC 4.4.4 C89)

(gcc 4.4.4 c89)

我一直使用函数strncpy 复制字符串。我从来没有真正使用 memmove与的memcpy 非常多。但是,当你决定是否要使用函数strncpy memmove与我只是想知道的memcpy

I have always used strncpy to copy strings. I have never really used memmove or memcpy very much. However, I am just wondering when would you decide whether to use strncpy, memmove, or memcpy?

在code我写是客户端/服务器应用程序。在本文档中,他们使用 BCOPY 。不过,我可以做同样的与他人?

The code I am writing is for a client/server application. In the documentation they use bcopy. However, could I do the same with the others?

bcopy((char*)server->h_addr,
      (char*)&serv_addr.sin_addr.s_addr,
      server->h_length);

非常感谢,

推荐答案

没有人回答你所有的问题。

No one answered all your questions.

函数strncpy()用于将数据从源复制到一组尺寸,复印(填充)的DEST 0×00 ■如果 0×00 字节的缓冲区结束前的源阵列(串)中找到。相对于的strcpy 将幸福地永远复制,直到 0 字节被发现 - 即使说字节是远远超出你的缓冲区。

strncpy() is used to copy data from a source to a dest of a set size, copying (padding) 0x00s if a 0x00 byte is found in the source array (string) before the end of the buffer. As opposed to strcpy which will blissfully copy forever until a 0 byte is found - even if said byte is well beyond your buffer.

的memcpy()用来从源拷贝至目的无论什么源数据包含。这也往往是非常的硬件在同一时间进行了优化,并会经常阅读系统字( INT S / S)加快复印时对准允许。

memcpy() is used to copy from source to dest no matter what the source data contains. It also tends to be very hardware optimized and will often read system words (ints/longs) at a time to speed up copying when alignment allows.

memmove与()用来从重叠的源和目标区域复制(说在自己或类似的东西四处移动的数组中的内容 - 因此,此举助记符)。它开始在后面,而不是前面的重叠区域重挫的数据就可以被读取之前prevent数据丢失的数据复制。它也趋于略效率较低,因为通常不从后到前多硬件帮助复制数据

memmove() is used to copy from an overlapping source and destination area (say moving an array's contents around within itself or something similiar - hence the move mnemonic). It copies data starting at the back instead of the front to prevent data loss from the overlapping regions clobbering data before it can be read. It also tends to be slightly less efficient as there usually isn't much hardware help copying data from back to front.

BCOPY()及其亲属( bzero ,和其他几个人)是一个字节复制功能我现在已经再在嵌入式土地见过。通常,在从源头一次目的地prevent未对齐的内存地址问题,它复制一个字节的数据,但任何好的的memcpy 将处理此所以它的使用相当频繁犯罪嫌疑人。

bcopy() and its relatives (bzero, and a few others) is a byte copy function I've seen now and then in embedded land. Usually, it copies data one byte at a time from source to destination to prevent misaligned memory address issues, though any good memcpy will handle this so its use is quite often suspect.

祝你好运!

这篇关于何时使用函数strncpy或memmove与?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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