Two problems with sizeof vs. type casting

Updated on technology 2024-04-12
19 answers
  1. Anonymous users2024-02-07

    1.After compilation, runtime.

    You can test it with an array, my IDE:VC++

    The tests are as follows:

    int g_itest=sizeof(int);

    char ch[g_itest];

    The compile-time error cannot allocate an array of constant size 0, and the compiler doesn't know how big the array is.

    Into. #define g_itest 1024char ch[g_itest];

    The compilation passes, apparently compiled.

    2.Sensory type conversion is a way of changing the representation of memory. int a=1, a is =0x00000001 in memory;

    float b=(float)a, b is 0x3f800000 in memory; The internal mechanism is the algorithm, and it is easy to implement the algorithm knowing the int and float memory access representation.

  2. Anonymous users2024-02-06

    1) I feel like it's OK at runtime.

    #include

    #include

    using namespace std;

    void f(const int &i)

    int arr[i];

    cout <<"size of arry:"<>i;

    f(i);system("pause");

    return 0;

    2) Data is stored in memory in binary form, and each data type has its own reading algorithm.

  3. Anonymous users2024-02-05

    sizeof(struct depositor) The size of a structure malloc function takes an int(the size of that structure), allocates space for that number of bytes, and returns an indistinguishable pointer to that area (void* pointer).

    Since the void pointer has to be converted to a different type before it can be used, we convert it to the target format (struct depositor *) and then we can use it.

    Overall effect, dynamically assigning a new struct depositor, accessed by pointer p1.

  4. Anonymous users2024-02-04

    It is to do a cast type conversion of the applied memory (an address, that is, a pointer) into a pointer type pointing to the struct depositor, because malloc returns the void * type, so such a cast is needed.

    But in reality, even if you don't do this type conversion, it's nothing, the compiler will just give you a warning.

  5. Anonymous users2024-02-03

    sizeof(struct depositor) gets the memory space size of the depositor structure, malloc allocation size is a piece of memory of the depositor, but it returns void*, and you need to force the type to convert to depositor*, which is what (struct depositor*) means.

  6. Anonymous users2024-02-02

    To open up a space of type struct deposito (sizeof(struct depositor) is to calculate the size of this space. Malloc is carried out to open up.

  7. Anonymous users2024-02-01

    I haven't learned about assembly, I just want to say that the accuracy of the various types is not the same, and the loss of departmental accuracy during the conversion leads to different results.

  8. Anonymous users2024-01-31

    &php + sizeof(hope);The pointer of hope* is shifted by sizeof(hope), that is, the address of PHP will be shifted by hope size * sizeof(hope) = 8 * 8 = 64 bits, i.e., 0x0012ff78 + 64(0x40) = 0x0012ffb8

    And (char*)&php + sizeof(hope) moves the pointer in char type, so char size * sizeof(hope) = 1 * 8 = 8 bytes, that is: 0x0012ff78 + 8(0x08) = 0x0012ff80

  9. Anonymous users2024-01-30

    Sizeof is OK but can only be used in the following cases:

    1 char ch;sizeof(ch) for the length of the variable area The result is 1

    2 char *str = "abcdefg"; sizeof(str);It's also the same for the variable, but here's the pointer, so it's 4

    3 sizeof("abcdefg");Some students here will think that the length of the pointer is the same as 2, but in fact, here is the exact length of static memory, and the result is 7. Because you told him the length of this static memory, why doesn't he know it, doesn't he?

    4 char arr[10];sizeof(arr) is a bit like 3 here, which is the length of the array 10.

    5 struct aa;sizeof(struct aa) The length of the struct aa type is 8, but it can also be commonly used types such as sizeof(char), sizeof(int) and so on.

    Finally, we know that the compiler defaults to the set of parameter parameters as the corresponding pointer, that is, as long as the parameter is an array, then it is a pointer, and the second clause above is 4

  10. Anonymous users2024-01-29

    sizeof is to determine the byte length of two strings, the length of the string is generally not used this function, more use strlen(), including the header file include

    if(strlen(a)>strlen(b)) long string address.

    else if(strlen(a)

  11. Anonymous users2024-01-28

    if(sizeof(a)>sizeof(b)) A long string address, assigned to c

    The use of sizeof here is just the length of the pointer. strlen should be used

  12. Anonymous users2024-01-27

    There are two forms of casting, known as explicit casts and implicit casts.

    1. Explicit cast casting.

    Explicit casts require the use of a cast-type operator in the following format:

    type() or (type).

    Type is a type descriptor, such as int and float. is an expression. After the cast conversion operator is operated, a numeric value with type is returned, and this cast conversion operation does not change the operand itself, and the operand itself does not change after the operation, for example:

    int nvar=0xab65;

    char cchar=char (nvar);

    The result of the above forced type conversion is to delete the two bytes at the top of the integer value 0xab65, and assign the content of the lower two bytes to the variable cchar as char values, and the value of nvar does not change after the type conversion.

    2. Implicit forced type conversion.

    Implicit type conversions occur in assignment expressions and function call expressions that have a return value. In an assignment expression, if the operand on the left and right sides of the assignment is of different types, the operand on the right side of the assignment is forcibly converted to the type value on the left side of the assignment and then assigned to the variable on the left side of the assignment. When a function is called, if the type of the expression after return is different from the type of the function return value, the value of the expression after return is forcibly converted to the function return value type when the value is returned, and then the value is returned, for example:

    int nvar;

    double dvar=;

    nvar=dvar;After this sentence is executed, the value of NVAR is 3, while the value of DVAR is still there.

  13. Anonymous users2024-01-26

    1. In the C language, the conversion is the data on the right closest to (double), and the operation level is lower than that of parentheses, but higher than multiplication and division, addition and subtraction, assignment characters, etc.; And consider the problem of k m: this key depends on the highest level of accuracy of binary operations, and among the two variables of binary operations: lower precision will automatically be converted to higher accuracy;

    2. Example: include

    main()

  14. Anonymous users2024-01-25

    You understand it quite right. The answer to both questions is: yes.

    At the beginning, val is defined as int, which means that the address of val (i.e. &val) is an integer number. But once you take out the address &val, you can redefine the contents of the address (char *) by telling the compiler that the pointer content of the address should be read as a char pointer.

    This can be thought of as a casting, but it is not standard, the standard castings are: (char) val or char(val)

    In the 32-bit Windows operating system, the number of ints occupies 4 bytes (bytes), and the lowest byte (that is, 8 bits) is automatically taken after being cast to char, which is done automatically, so you don't have to worry. However, since pointer is defined as a pointer to char, it is added (pointer++) by only one byte (8 bits).

  15. Anonymous users2024-01-24

    int *a, in fact, is equal to (int *)a; We can think of the data type of a as of type (int *), but int(*a) is a different story

    int (*a) is just a thing that can only be concepted, which means that a itself is an address type, *a takes the value of this address (variable, space) but declaring such a variable, of course, is equivalent to asking for fish Needless to say, the compiler definitely doesn't do vc++ seems to be compiled in the past, but memory can't accept it).

    p = (struct studedt *)malloc(sizeof(struct student));

    The malloc() function returns a normal address type (but the size of this address block is already sizeof()), if you want to point the struct student pointer p to it, you have to convert it to a struct student address that the struct student pointer p can point to, so it is forced to convert

    struct studedt) *malloc(size);malloc() gives an address, which is itself a pointer, *pointer data, it will be a constant (space), and then this constant will be forcibly converted into (struct studedt) like int a; short b = (short)a;Of course, compiling and not compiling is another thing (it can be compiled).

  16. Anonymous users2024-01-23

    Alloc the memory-space function malloc

    Call form: type specifier *) malloc(size).

    Type Specifier indicates what data type the area is used for.

    Type specifier *) indicates that the return value is cast to that type pointer.

    size" is an unsigned number.

    For example: pc=(char *)malloc(100);

    Indicates that 100 bytes of memory space are allocated and forcibly converted to a character array type, and the return value of the function is a pointer to the character array, and the pointer is assigned to the pointer variable pc.

  17. Anonymous users2024-01-22

    This doesn't work, p is the struct student* type, malloc returns the void* type.

    For type matching.

    Therefore, the value returned by malloc should also be converted to struct studedt * before it can be assigned.

    So it doesn't make sense to put the expansion outside.

  18. Anonymous users2024-01-21

    struct student *p;

    p = (struct studedt *)malloc(size)

    p is a pointer to a sturct student, but malloc(size) returns (void *) to match struct student *p, so as long as the two are the same, it's OK....p = (struct studedt *)malloc(size), the area this points to is a struct student type.

  19. Anonymous users2024-01-20

    I haven't written C for a long time, so I'll do my best to help you answer.

    typedef struct means a new data type like lnode.

    LinkList is a pointer to a data type such as a lnode, and this *linklist pointer points to the structure type of lnode.

    If *linklist is output, the result will be the data that the pointer points to. The output linklist is the address of this *linklist pointer.

    The p, q variables are used to store data such as linklist.

    sizeof() is the size of the returned type, and malloc is the size of the application. (linklist) is a cast.

Related questions
17 answers2024-04-12

First. Solution: Because a(n+1)=a(n)+1 (n(n+1)), so a(n+1)-a(n)=1 (n(n+1)) so a(n)-a(n-1)=1 (n(n-1))a(n-1)-a(n-2)=1 ((n-1)(n-2))a(2)-a(1)=1 (2*1). >>>More

19 answers2024-04-12

You also have to add a network card for 10 yuan.

As for the setup of the dual network cards. >>>More

4 answers2024-04-12

You don't know anything about lawyers?! Want to be a lawyer? >>>More

6 answers2024-04-12

Don't panic, let me tell you, the cost of studying in the United States is about 300,000 a year (my brother studied at Franklin & Marshall College in the United States) He is a college student, let me tell you, if you study really well. Go for IELTS and TOEFL. Tell you that if you have good grades, you can apply for a full scholarship. >>>More

7 answers2024-04-12

1.Equally, after the cell has stopped the plasmo-wall separation, there is no difference between the internal and external concentrations.