数组的初始化与memset的注意事项

Posted 王世晖

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组的初始化与memset的注意事项相关的知识,希望对你有一定的参考价值。

如果全部初始化为0,可以int count[26]=0;这样初始化为26个0
    如果int count[26]=2015;,则只有第一个按照给定值初始化,其余初始化为0
    这种方法对于double数组同样适用
    对char数组也适用,只是第一个元素按照给定值初始化外,其余的初始化为小写字母a
    这种方法并不具有普适性,因为编译器的差异会导致不一样的结果,比如可能会有
    个别编译器对局部数组不初始化,输出随机值。
    因此尽量使用memset对数组进行初始化
    void * memset ( void * ptr, int value, size_t num ); <cstring>
    Fill block of memory
    Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
    Parameters
    ptr
    Pointer to the block of memory to fill.
    value
    Value to be set. The value is passed as an int,but the function fills the block of memory using the unsigned char conversion of this value.
    num
    Number of bytes to be set to the value.
    Return Value
    ptr is returned.
    但是要注意该函数的第二个参数value值
    The value is passed as an int,but the function fills the block of memory using the unsigned char conversion of this value.
    这个值以int的形式传进来,但是会被转化为unsigned char类型,这会导致
    一些问题,导致给全部元素赋值0方便,其他数值不方便。
    如memset(count_1,0xff,26);0xff是以unsigned char(截取低位填充高位)填充的,但是是以int形式显示的,有符号数-1的补码为32个1,因此显示全部为-1
    同理memset(count_2,-1,26);先把-1转化为 unsigned char 0xff,同上

    memset(count_3,1,26); 0x01 低位扩充高位成32位 0x01010101,是16843009


#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <cstdio>
/** 数组的初始化与memset的注意事项
    如果全部初始化为0,可以int count[26]=0;这样初始化为26个0
    如果int count[26]=2015;,则只有第一个按照给定值初始化,其余初始化为0
    这种方法对于double数组同样适用
    对char数组也适用,只是第一个元素按照给定值初始化外,其余的初始化为小写字母a
    这种方法并不具有普适性,因为编译器的差异会导致不一样的结果,比如可能会有
    个别编译器对局部数组不初始化,输出随机值。
    因此尽量使用memset对数组进行初始化
    void * memset ( void * ptr, int value, size_t num ); <cstring>
    Fill block of memory
    Sets the first num bytes of the block of memory pointed by ptr
    to the specified value (interpreted as an unsigned char).
    Parameters
    ptr
    Pointer to the block of memory to fill.
    value
    Value to be set. The value is passed as an int,
    but the function fills the block of memory using the unsigned char
    conversion of this value.
    num
    Number of bytes to be set to the value.
    Return Value
    ptr is returned.
    但是要注意该函数的第二个参数value值
    The value is passed as an int,
    but the function fills the block of memory using the unsigned char
    conversion of this value.
    这个值以int的形式传进来,但是会被转化为unsigned char类型,这会导致
    一些问题,导致给全部元素赋值0方便,其他数值不方便。
    如memset(count_1,0xff,26);0xff是以unsigned char
    (截取低位填充高位)填充的,但是是以
    int形式显示的,有符号数-1的补码为32个1,因此显示全部为-1
    同理memset(count_2,-1,26);先把-1转化为 unsigned char 0xff,同上
    memset(count_3,1,26); 0x01 低位扩充高位成32位 0x01010101,是16843009
 *
 */

using namespace std;

int main()

//    freopen("o.txt","w",stdout);
    char input[5]='X';//这种初始化方式只有第一个字符被按照要求初始化
    //之后的字符默认为小写字母a
    cout<<"\\nchar input[5]='X';"<<endl;
    for(int i=0;i<5;i++)
    
        cout<<input[i]<<' ';
        //输出 X a a a a
    
    int count[26]=1991;//这样的初始化经下边测试会初始化为
//    1个1991 和25个0
    cout<<"\\nint count[26]=1991;"<<endl;
    for(int i=0;i<5;i++)
    
        cout<<count[i]<<' ';
//        输出 1991 0 0 0 0
    
    double d[5]=20.15;
    cout<<"\\ndouble d[5]=20.15;"<<endl;
    for(int i=0;i<5;i++)
    
        cout<<d[i]<<' ';
    

    int count_1[26];
    memset(count_1,0xff,26);//2的32次方等于4294967296
    cout<<"\\nmemset(count_1,0xff,26);"<<endl;
    for(int i=0;i<5;i++)
    
        cout<<count_1[i]<<' ';
    
    int count_2[26];
    memset(count_2,-1,26);
    cout<<"\\nmemset(count_2,-1,26);"<<endl;
    for(int i=0;i<5;i++)
    
        cout<<count_2[i]<<' ';
    
    int count_3[26];
    memset(count_3,1,26);//2的32次方等于4294967296
    cout<<"\\nmemset(count_3,1,26);"<<endl;
    for(int i=0;i<5;i++)
    
        cout<<count_3[i]<<' ';
    
    char input_[10001];
    memset(input_,'S',10001);//void * memset ( void * ptr, int value, size_t num );
    cout<<"\\nmemset(input_,'S',10001);"<<endl;
    for(int i=0;i<5;i++)
    
        cout<<input_[i]<<' ';
    
    cout<<endl;
    cout<<"sizeof(unsigned char) is :"<<sizeof(unsigned char)<<endl;
    cout<<"sizeof(int) is :"<<sizeof(int)<<endl;
    cout<<"sizeof(long) is :"<<sizeof(long)<<endl;
    return 0;

/** output:
char input[5]='X';
X  a a a a
int count[26]=1991;
1991 0 0 0 0
double d[5]=20.15;
20.15 0 0 0 0
memset(count_1,0xff,26);
-1 -1 -1 -1 -1
memset(count_2,-1,26);
-1 -1 -1 -1 -1
memset(count_3,1,26);
16843009 16843009 16843009 16843009 16843009
memset(input_,'S',10001);
S S S S S
sizeof(unsigned char) is :1
sizeof(int) is :4
sizeof(long) is :4
 *
 */



以上是关于数组的初始化与memset的注意事项的主要内容,如果未能解决你的问题,请参考以下文章

memset使用技巧

C语言中数组高位转为低位

数组的初始化与memset的注意事项

C语言 对字节的高位和低位进行互换!

K伪进制

从四个 __m128i 变量的 64 个高位或低位初始化 __m256i