Understanding and void keyword sizeof

voidThe literal "no type" void*is "untyped pointer." void*You can point to any type of data. voidAlmost only the role of "Notes" and limitations of the program because no one would ever define a void variable.

void a;    //编译时提示"illegal use of type 'void'"

void really play a role is to:

  • Limiting the function returns
  • Defining a function of the parameters

If the same type of the pointer p1 and p2, it can be assigned to each other between p1 and p2; if p1 and p2 point to different types of data, you must use a cast operator, the pointer type to the right of the left pointer assignment operators the type before it can be assigned. Such as:

float* p1;

int* p2;

p1 = p2;  //can't convert from 'int*' to 'float*',必须改为下面这种形式

p1=(float*)p2;

And void*is different, any type of pointer can be assigned directly to it, without a cast, for example:

void* p1;
int* p2;
p1 = p2;

But this does not mean that void*can also be assigned to other types without pointers cast, this is because "no type" may contain "typed" and "typed" not inclusive "no type"

voidAnd void*keyword usage rules

  1. If the function does not return a value, then the type should be declared void . In the C language, all without the return type of the function defined, the compiler will return an integer value processing , but was mistaken for many programmers to void type

    E.g

    add(int a,int b)
    {
       return a+b; 
    }
    int main() 
    {
        //程序运行结果为2+3=5,这说明不加返回值说明的函数,其返回值的确为int类型
        printf("2+3=%d",add(2,3));
    }
    

    Therefore, to avoid confusion, for any function you must specify its return type. If the function does not return a value, it must be declared void type. Play a role comment from the code. This is both good readability required program, but also the normative programming requirements

  2. If the function has no parameters, it should declare its parametersvoid . If you declare such a function in the C ++ language

    int function(void)              
    {  
        // 使用function(2)运行是不合法的,因为在C++中,函数参数为void的意思是这个函数不接受任何参数,但如果在Turbo C2.0中编译则正确
        // 说明在C语言中,可以给无参数的函数传送任意类型的参数,但在C++编译器中编译同样的代码则会出错。
        // 所以,若函数不接受任何参数,一定要声明为void
      return 1;
    }
    
  3. If the parameter of the function may be any type of pointer, which should be declared parametersvoid*

    A typical memory operation functions such as:

    void* memcpy(void* dest,const void* src,size_t len);
    void* memset(void* buffer,int c,size_t num);
    

    In this way, any type of pointer can be passed memcpy and memset, which also embodies the true meaning of memory operation function, because the object it is only a memory operation, regardless of what type of memory chip that

  4. void can not represent a real variable

    void a;  //错误
    

    void reflects an abstract variable in this world are "typed" in. void appears only to an abstract need, if you correctly understand the concept of object-oriented "abstract base class", but also very easy to understand void data type.

sizeofUse keyword

  • sizeofIs not a function as sizeof unary-expression, sizeof(unary-expression)and sizeof(type-name)are lawful, but not the first brackets

  • sizeofNot a unary operator

    #include <stdio.h>
    int main()
    {
        int a = 0;
        printf("%d\n", sizeof(a=3));
        printf("%d", a);
        return 0;
    }
    

    Operating results of 4,3 instead of 4,0, so it is not a unary operator

    This is because the sizeof the characteristics of the process of compilation phase caused. sizeof can be compiled into machine code, within the scope sizeof, i.e. () inside the content can not be compiled (a = 3 statement can not be compiled into machine code), but is replaced by type, therefore, not a unary operator sizeof because the chain does not support the sizeof expressions, sizeof statements within the scope of the range are not compiled into machine code. Like a special macro, which evaluates at compile time, e.g.

    cout<<sizeof(int)<<endl;     //32位机上输出4
    cout<<sizeof(1==2)<<endl;    //相当于size(bool),为1
    

1. sizeofUsage

sizeofIn two ways, the first is for the object, as usage sizeof(object)or sizeof object, as for the second type, usage is sizeof(typename), for example,

int i = 2;
cout<<sizeof(i)<<endl;         //sizeof(object)的用法,合理
cout<<sizeof i<<endl;        //sizeof object的用法,合理
cout<<sizeof 2<<endl;        //2被解析成int类型的object,sizeof object的用法,合理
cout<<sizeof(int)<<endl;      //sizeof(typename)的用法,合理
cout<<sizeof int<<endl;      //错误,对于操作符,一定要加(),无论是对对象还是对类型取值,sizeof写成sizeof这种函数形式永远是正确的

2. The basic data typessizeof

Function type and pointer types belong to the scope of the pointer, the pointer address is mainly used to store, in the system 32, any type of pointer they are much memory 4 bytes; and the function type rather special, it returns its sizeof value types as its own type. No matter what type of pointer, as long as the pointer, sizeof operation result is 4

Its return type function type value as its own type. Note that the void can not return sizeof function value, not the value of the function pointer sizeof

int f1()
{
    return 0;
}
double f2()
{
    return 0;
}
void f2()
{
    return 0;
}
cout<<sizeof(f1())<<endl;        //f1()返回值为int,因此被认为是int
cout<<sizeof(f2())<<endl;        //f2()返回值为double,因此被认为是double
cout<<sizeof(f3())<<endl;        //错误,无法对void类型使用sizeof
cout<<sizeof(f1)<<endl;         //错误,无法对函数指针使用sizeof

Here is an array of sizeof

char a[] = "abvdef";
char b[] = {'a','b','c','d','e','f'};
int c[20] = {3,4}
char d[2][3]={"aa","bb"};

cout<<sizeof(a)<<endl;  //输出7,表示字符串
cout<<sizeof(b)<<endl;  //输出6,仅表示字符数组
cout<<sizeof(c)<<endl;  //输出80
cout<<sizeof(d)<<endl;  //输出6
cout<<sizeof(*a)<<endl;  //输出1
cout<<sizeof(*b)<<endl;  //输出1
cout<<sizeof(*c)<<endl;  //输出4
cout<<sizeof(*d)<<endl;  //输出3

If the character array representation of the string, then the end of the array automatically inserted \0, can not be missed when the sizeof, so the array a size is not specified when defining the space allocated to it compile time is determined in accordance with the value initialized, i.e. 7.

c is a multidimensional array, the size of the space occupied by each is the product of the number of dimensions, i.e. 6, it can be seen that it is the size of the array is allocated at compile time space, i.e. the number of dimensions of each product, i.e. 6. It can be seen that the spatial size of the array is allocated at compile time, that is 各维数的乘积*数组元素的大小. Corresponding to two-dimensional array d, *dequivalent to d[0](d [0] is a one-dimensional array of length 3), so sizeof (* d) is equal to 3

C language to achieve multi-dimensional array is achieved by a one-dimensional array, that is to say for multidimensional arrays, array element you operated likely is an array itself. This should attract attention. Size of the array is the product of the size of the dimension of each array elements *

Opening pass an array to a function, the array will degenerate into pointers, lost its array of features

int GetStrLength(char str[])
{
   return sizeof(str); 
}
int main()
{
    char szStr[] = "abcdef";
    cout<<"sizeof(szStr[])="<<GetStrLength()<<endl;
    //输出sizeof(szStr[])=4
}      

sizeof(void*)

sizeof(void *)Get pointer type size. Depending on the platform, and the results will be different.

  • Win32 platform is the result of 4
  • X64 platform is the result 8

The reason is that the pointer is stored in the memory address, the 32-bit address Win32 index (4 bytes), an index X64 64-bit address (8 bytes)

Reference material

Deep understanding of the void, void and sizeof keyword
sizeof (void)

Released five original articles · won praise 0 · Views 61

Guess you like

Origin blog.csdn.net/taokexia/article/details/105330612