sizeof- function or macro? [duplicate]

我的未来我决定 提交于 2019-12-08 05:29:56

问题


In c, we are using the sizeof() for getting the size of the datatypes. So how it is defined. It is a macro or a function.

Because we can use that as two ways,

sizeof int

and

sizeof(int)

so how this is defined in header file.


回答1:


It's neither. It's a built-in operator, whose value is computed at compile-time unless the argument is the name of a variable-length array (added in C99).

The parentheses that you often see are not part of the "call", since sizeof is not a function. They are part of the argument, and are only needed when the argument is a cast expression, i.e. the name of a type enclosed in parentheses.

I personally recommend against using sizeof with a type name as the argument whenever possible, since it's usually not needed, and creates a disconnect/de-coupling which can lead to errors.

Consider something like this:

float *vector = malloc(100 * sizeof(double));

The above, of course, contains a bug: if float is smaller than double, it will waste a lot of memory. It's easy to imagine ending up with something like the above, if vector started out as an array of double but was later changed to float. To protect aginst this, I always write:

float *vector = malloc(10 * sizeof *vector);

The above uses the argument *vector (an expression of type float) to sizeof, which is not a type name so no parentheses are needed. It also "locks" the size of the element to the pointer used to hold it, which is safer.




回答2:


Sizeof is neither a macro nor a function.Its a operator which is evaluated at compile time.

Macros evaluated during pr-processing phase.

As pointed out by @Yu Hao Variable length arrays is the only exception.

For More Understanding solve this;

#include<stdio.h>
    char func(char x)
    {
           x++;
           return x;
    }

    int main()
    {
           printf("%zu", sizeof(func(3))); 
                return 0;
    }

    A) 1            B)2             C)3     D)4



回答3:


From ISO/IEC9899

6.5.3.4 The sizeof operator

Constraints

1 The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.

So it is neither a macro nor a function.Its a operator!

and the way it is handled is a thing of the compiler.

But regarding to compile time and runtime determination the standard says:

Semantics

2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

So it is even given by standard that it mus be determined on compile time excepting the VLA case.




回答4:


Syntax

  1. sizeof( type )
  2. List sizeof expression

Both versions return a constant of type std::size_t.

Explanation

  1. returns size in bytes of the object representation of type.
  2. returns size in bytes of the object representation of the type, that would be returned by expression, if evaluated.



回答5:


The unary operator sizeof is used to calculate the size of any datatype, measured in the number of bytes required to represent the type.

In many programs, there are situations where it is useful to know the size of a particular datatype (one of the most common examples is dynamic memory allocation using the library function malloc). Though for any given implementation of C or C++ the size of a particular datatype is constant, the sizes of even primitive types in C and C++ are implementation-defined (that is, not precisely defined by the standard). This can cause problems when trying to allocate a block of memory of the appropriate size. For example, say a programmer wants to allocate a block of memory big enough to hold ten variables of type int. Because our hypothetical programmer doesn't know the exact size of type int, the programmer doesn't know how many bytes to ask malloc for. Therefore, it is necessary to use sizeof:



来源:https://stackoverflow.com/questions/28363064/sizeof-function-or-macro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!