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

本文探讨了在编程中使用memset初始化数组的细节,通过示例`memset(count_3,1,26)`解释了如何低位扩充高位,形成32位数值0x01010101,即16843009。" 125301448,13903409,three.js实现3D天空效果,"['javascript', '3D开发', '图形学']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    如果全部初始化为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
 *
 */



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值