有没有一个类似memset的函数可以在visualstudio中设置整数值



1,很遗憾,memset(void*dst,int value,size_t size)第一次使用这个函数时愚弄了很多人!第二个参数"int value"应该是"uchar value"来描述内部的实际操作。不要误解我的意思,我在问一个类似记忆集的函数!

2,我知道有一些c++糖果函数,比如std::fill_n(my_array,array_length,constant_value);甚至是OS X中的纯c函数:memset_pattern4(grid,&pattern,sizeof grid);在一个完美的线程中提到了为什么memset()错误地初始化了int?。

那么,visualstudio的运行库中是否有类似于memset_pattern4()的c函数?

3,有人问我为什么不使用for循环来逐个设置整数。我的答案是:至少在x86中,当设置大主干(10K?)内存时,memset的性能会更好。

http://www.gamedev.net/topic/472631-performance-of-memset/page-2给出了更多的讨论,尽管没有结论(我怀疑会有)。

4、所述函数可以通过避免无用的斐波那契累加来简化计数排序。原件:

for (int i = 0; i < SRC_ARRY_SIZE; i++)
    counter_arry[src_arry[i]]++; 
for (int i = SRC_LOW_BOUND; i < SRC_HI_BOUND; i++)//forward fabnacci??
    counter_arry[i+1] += counter_arry[i];
for (int i = 0; i < SRC_ARRY_SIZE; i++) 
{   
    value = src_arry[i];
    map = --counter_arry[value];//then counter down!
    temp[map] = value;
}

预期:

for (int i = 0; i < SRC_ARRY_SIZE; i++)
    counter_arry[src_arry[i]]++; 
for (int i = SRC_LOW_BOUND; i < SRC_HI_BOUND+1; i++)//forward fabnacci??
{           
    memset_4(cur_ptr,i, counter_arry[i]);
    cur_ptr += counter_arry[i];
}

感谢您的友好评论和回复!

这里有一个memset_pattern4()的实现,您可能会发现它很有用。它与达尔文的SSE汇编语言版本完全不同,只是它有相同的接口。

#include <string.h>
#include <stdint.h>
/*
A portable implementation of the Darwin memset_patternX() family of functions:
These are analogous to memset(), except that they fill memory with a replicated
pattern either 4, 8, or 16 bytes long.  b points to a buffer of size len bytes
which is to be filled.  The second parameter points to the pattern.  If the
buffer length is not an even multiple of the pattern length, the last instance
of the pattern will be truncated.  Neither the buffer nor the pattern pointer
need be aligned.
*/
/*
alignment utility macros stolen from Linux
see https://lkml.org/lkml/2006/11/25/2 for a discussion of why typeof() is used
*/
#if !_MSC_VER
       #define __ALIGN_KERNEL(x, a)            __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
       #define __ALIGN_KERNEL_MASK(x, mask)    (((x) + (mask)) & ~(mask))
       #define ALIGN(x, a)             __ALIGN_KERNEL((x), (a))
       #define __ALIGN_MASK(x, mask)   __ALIGN_KERNEL_MASK((x), (mask))
       #define PTR_ALIGN(p, a)         ((typeof(p))ALIGN((uintptr_t)(p), (a)))
       #define IS_ALIGNED(x, a)        (((x) & ((typeof(x))(a) - 1)) == 0)
       #define IS_PTR_ALIGNED(p, a)      (IS_ALIGNED((uintptr_t)(p), (a)))
#else
       /* MS friendly versions */
       /* taken from the DDK's fltKernel.h header */
       #define IS_ALIGNED(_pointer, _alignment)                        
                     ((((uintptr_t) (_pointer)) & ((_alignment) - 1)) == 0)
       #define ROUND_TO_SIZE(_length, _alignment)                      
                           ((((uintptr_t)(_length)) + ((_alignment)-1)) & ~(uintptr_t) ((_alignment) - 1))
       #define __ALIGN_KERNEL(x, a)    ROUND_TO_SIZE( (x), (a))
       #define ALIGN(x, a)             __ALIGN_KERNEL((x), (a))
       #define PTR_ALIGN(p, a)         ALIGN((p), (a))
       #define IS_PTR_ALIGNED(p, a)      (IS_ALIGNED((uintptr_t)(p), (a)))
#endif

void nx_memset_pattern4(void *b, const void *pattern4, size_t len)
{
       enum { pattern_len = 4 };
       unsigned char* dst = (unsigned char*) b;
       unsigned const char* src = (unsigned const char*) pattern4;
       if (IS_PTR_ALIGNED( dst, pattern_len) && IS_PTR_ALIGNED( src, pattern_len)) {
              /* handle aligned moves */
              uint32_t val = *((uint32_t*)src);
              uint32_t* word_dst = (uint32_t*) dst;
              size_t word_count = len / pattern_len;
              dst += word_count * pattern_len;
              len -= word_count * pattern_len;
              for (; word_count != 0; --word_count) {
                     *word_dst++ = val;
              }
       }
       else {
              while (pattern_len <= len) {
                     memcpy(dst, src, pattern_len);
                     dst += pattern_len;
                     len -= pattern_len;
              }
       }
       memcpy( dst, src, len);
}

最新更新