简体   繁体   English

memset,带有新运算符的 memcpy

[英]memset, memcpy with new operator

Can I reliably use memset and memcpy operators in C++ with memory been allocated with new?我可以在 C++ 中可靠地使用 memset 和 memcpy 运算符,并使用 new 分配内存吗?

Edited:编辑:

Yes, to allocate native data type是,分配原生数据类型

Example例子

BYTE *buffer = 0;
DWORD bufferSize = _fat.GetSectorSize();
buffer = new BYTE[bufferSize];

_fat.ReadSector(streamChain[0], buffer, bufferSize);

ULONG header = 0;
memcpy(&header, buffer, sizeof(ULONG));

So long as you are only using new to allocate the built-in and/or POD types, then yes.只要您仅使用 new 来分配内置和/或 POD 类型,就可以。 However, with something like this:但是,有这样的事情:

std::string * s = new string;
memset( s, 0, sizeof(*s) );

then you would be looking at disaster.那么你就会看到灾难。

I have to ask though, why you and others seem so enamoured with these functions - I don't believe I ever use them in my own code.不过我不得不问,为什么你和其他人似乎对这些函数如此着迷——我不相信我曾经在自己的代码中使用过它们。 Using std::vector, which has its own copy and assignment facilities seems like a better bet for memcpy(), and I've never really believed in the magic of setting everything to zero, which seems to be the main use for memset().使用具有自己的复制和赋值功能的 std::vector 似乎是 memcpy() 更好的选择,而且我从来没有真正相信将所有内容都设置为零的魔力,这似乎是 memset() 的主要用途)。

Yes of course, though memory allocated with new is taken from the "free store" it is still just in the application memory address space.是的,当然,虽然用 new 分配的内存是从“空闲存储”中获取的,但它仍然只是在应用程序内存地址空间中。 memset and memcpy simply take address parameters for a source and destination, and these parameters can technically be to any address in your application's address space. memset 和 memcpy 只是获取源和目标的地址参数,这些参数在技术上可以是应用程序地址空间中的任何地址。

In fact some of the standard library algorithms distill down to a simple memcpy themselves (vector source and target copies through iterators).事实上,一些标准库算法本身提炼成一个简单的 memcpy(矢量源和目标副本通过迭代器)。

Yes, you can, indeed是的,你确实可以

Here's a canonical implementation of memset (taken from the libc of Android):这是memset的规范实现(取自 Android 的 libc):

void*  memset(void*  dst, int c, size_t n)
{
    char*  q   = dst;
    char*  end = q + n;

    for (;;) {
        if (q < end) break; *q++ = (char) c;
        if (q < end) break; *q++ = (char) c;
        if (q < end) break; *q++ = (char) c;
        if (q < end) break; *q++ = (char) c;
    }

  return dst;
}

It's obvious that the dst pointer could be allocated any valid way ( new , malloc , or even statically) - memset doesn't care.很明显,可以以任何有效方式( newmalloc甚至静态)分配dst指针 - memset并不关心。

Depends what you have allocated.取决于你分配了什么。 If you have allocated POD (Plain Old Data) ie done如果你已经分配了 POD(Plain Old Data),即完成

char* p = new char[100];
then yes, it is no different from malloc. 那么是的,它与 malloc 没有什么不同。 If you have allocated an object(s) then using memcpy or memset could lead to un-defined behavior. 如果您已经分配了一个对象,那么使用 memcpy 或 memset 可能会导致未定义的行为。

You mentioned using memset, but your code didn't call it.您提到使用 memset,但您的代码没有调用它。

You can leverage the features of the language to allocate & memset all in one blast:您可以利用该语言的功能一次性分配和 memset:

int someInts [256] = {0};

This will allocate an array of 256 ints, and set each one to zero, accomplishing the same as:这将分配一个包含 256 个整数的数组,并将每个整数设置为零,实现与以下相同:

int someInts[256];
memset(someInts, 0, sizeof(someInts));

...but possibly faster, depending on how your compiler will optimize the alloc-memset code block. ...但可能更快,这取决于您的编译器将如何优化 alloc-memset 代码块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM