Saturday, July 31, 2021

Warning C6384 Dividing sizeof a pointer by another value.

when I tested the demo in the microsoft link, I ran into this warning:

Warning C6384 Dividing sizeof a pointer by another value.

  1. Incomplete Types
  2. An incomplete type is a type that describes an identifier but lacks information needed to determine the size of the identifier. An incomplete type can be:.

  3. Pointer array and sizeof confusion
  4. Note that sizeof is a compiler operator. It is rendered to a constant at compile time. Anything that could be changed at runtime (like the size of a new'ed array) cannnot be determined using sizeof..

  5. Introduction to size() in C++
  6. The std::size( ) function returns the size of the given variable, container or an array. The std::size( ) function is a built in function in the C++ STL (Standard Template Library). The std::size( )function is available if any of the headers are included like <array >, , , , , , , , , , , etc and hence the std::size( ) function can be apply to all these variables, containers or arrays..

  7. C6384
  8. warning C6384: dividing sizeof a pointer by another value

    This warning indicates that a size calculation might be incorrect. To calculate the number of elements in an array, one sometimes divides the size of the array by the size of the first element; however, when the array is actually a pointer, the result is typically different than intended.

    If the pointer is a function parameter and the size of the buffer was not passed, it is not possible to calculate the maximum buffer available. When the pointer is allocated locally, the size used in the allocation should be used.

  9. sizeof Operator
  10. Yields the size of its operand with respect to the size of type char..

    Remarks The result of the sizeof operator is of type size_t, an integral type defined in the include file < stddef.h > . This operator allows you to avoid specifying machine-dependent data sizes in your programs.

    The operand to sizeof can be one of the following:

    1. A type name. To use sizeof with a type name, the name must be enclosed in parentheses.
    2. An expression. When used with an expression, sizeof can be specified with or without the parentheses. The expression is not evaluated.

    Application/Use Case

    1. When the sizeof operator is applied to an object of type char, it yields 1.
    2. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier.
    3. To obtain the size of the pointer represented by the array identifier, pass it as a parameter to a function that uses sizeof.
    4. For example:

    When the sizeof operator is applied to a class, struct, or union type, the result is the number of bytes in an object of that type, plus any padding added to align members on word boundaries. The result does not necessarily correspond to the size calculated by adding the storage requirements of the individual members. The /Zp compiler option and the pack pragma affect alignment boundaries for members.

    The sizeof operator never yields 0, even for an empty class.

    The sizeof operator cannot be used with the following operands:

    • Functions. (However, sizeof can be applied to pointers to functions.)
    • Bit fields.
    • Undefined classes.
    • The type void.
    • Dynamically allocated arrays.
    • External arrays.
    • Incomplete types.
    • Parenthesized names of incomplete types.

    When the sizeof operator is applied to a reference, the result is the same as if sizeof had been applied to the object itself.

    When the sizeof operator is applied to a reference, the result is the same as if sizeof had been applied to the object itself.

    If an unsized array is the last element of a structure, the sizeof operator returns the size of the structure without the array.

    The sizeof operator is often used to calculate the number of elements in an array using an expression of the form:

    sizeof array / sizeof array[0]

  11. What is size_t in C?
  12. I am getting confused with size_t in C. I know that it is returned by the sizeof operator. But what exactly is it? Is it a data type? Let's say I have a for loop:.

    size_t already defined in the < stdio.h > header file, but it can also be defined by the < stddef.h >, < stdlib.h >, <string.h >, <time.h >, and <wchar.h > headers.

  13. What is the size_t data type in C?
  14. size_t is an unsigned integral data type which is defined in various header files such as:

    <stddef.h >, <stdio.h >, <stdlib.h >, <string.h >, < time.h >, <wchar.h >

  15. size_t : in C type
  16. size_t is the unsigned integer type of the result of sizeof , _Alignof (since C11) and offsetof, depending on the data model.

  17. std::size_t: in C++
  18. std::size_t is the unsigned integer type of the result of the sizeof operator as well as the sizeof... operator and the alignof operator (since C++11).

    Notes std::size_t can store the maximum size of a theoretically possible object of any type (including array). A type whose size cannot be represented by std::size_t is ill-formed (since C++14) On many platforms (an exception is systems with segmented addressing) std::size_t can safely store the value of any non-member pointer, in which case it is synonymous with std::uintptr_t.

    std::size_t is commonly used for array indexing and loop counting. Programs that use other types, such as unsigned int, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX or if it relies on 32-bit modular arithmetic.

    When indexing C++ containers, such as std::string, std::vector, etc, the appropriate type is the member typedef size_type provided by such containers. It is usually defined as a synonym for std::size_t.

  19. sizeof operator: in C++
  20. Queries size of the object or type. Used when actual size of the object must be known.

  21. Object :in C++
  22. C++ programs create, destroy, refer to, access, and manipulate objects.

    An object, in C++, has

    • size (can be determined with sizeof);
    • alignment requirement (can be determined with alignof);
    • storage duration (automatic, static, dynamic, thread-local);
    • lifetime (bounded by storage duration or temporary);
    • type;
    • value (which may be indeterminate, e.g. for default-initialized non-class types);
    • optionally, a name.

    The following entities are not objects: value, reference, function, enumerator, type, non-static class member, template, class or function template specialization, namespace, parameter pack, and this.

    A variable is an object or a reference that is not a non-static data member, that is introduced by a declaration.

  23. Objects and alignment: in C
  24. C programs create, destroy, access, and manipulate objects.

    An object, in C, is region of data storage in the execution environment, the contents of which can represent values (a value is the meaning of the contents of an object, when interpreted as having a specific type).

    Every object has

    • size (can be determined with sizeof)
    • alignment requirement (can be determined by _Alignof) (since C11)
    • storage duration (automatic, static, allocated, thread-local)
    • lifetime (equal to storage duration or temporary)
    • effective type (see below)
    • value (which may be indeterminate)
    • optionally, an identifier that denotes this object

    Objects are created by declarations, allocation functions, string literals, compound literals, and by non-lvalue expressions that return structures or unions with array members.

    Some Errors I met in testing these examples:

  25. C++ argument of type * is incompatible with parameter of type **
  26. According to visual studios it states that "argument of type 'char*' is incompatible with parameter of type 'char**'". It's referring to newArr..

  27. wcsncpy, wcsncpy_s
  28. Copies at most count characters of the wide string pointed to by src (including the terminating null wide character) to wide character array pointed to by dest.

  29. V514. Dividing sizeof a pointer by another value. There is a probability of logical error presence.
  30. The analyzer found a potential error related to division of the pointer's size by some value. Division of the pointer's size is rather a strange operation since it has no practical sense and most likely indicates an error or misprint in code..

    this offered a good solution

  31. 10.2 — Arrays (Part II)
  32. This lesson continues the discussion of arrays that began in lesson 10.1 -- Arrays (Part I)..

No comments:

Post a Comment