Lesson 22. Pattern 14. Overloaded
functions
When porting a 32-bit program to a 64-bit platform, you may encounter changes in its logic related to
the use of overloaded functions. If a function is overlapped for 32-bit and 64-bit values, the access to it
with an argument of a memsize-type will be translated into different calls on different systems. This
technique may be useful as, for example, in this code:

static size_t GetBitCount(const unsigned __int32 &) {

    return 32;

}

static size_t GetBitCount(const unsigned __int64 &) {

    return 64;

}

size_t a;

size_t bitCount = GetBitCount(a);

But this change of logic is potentially dangerous. Imagine a program that uses a class to arrange the
stack. This class is specific in that way that it allows you to store values of different types:

class MyStack {

...

public:

    void Push(__int32 &);

    void Push(__int64 &);

    void Pop(__int32 &);

    void Pop(__int64 &);

} stack;

ptrdiff_t value_1;

stack.Push(value_1);

...

int value_2;

stack.Pop(value_2);
A careless programmer saves into and then selects from the stack values of different types ("ptrdiff_t"
and "int"). Their sizes coincide on the 32-bit system and everything is quite okay. But when the size of
the type "ptrdiff_t" changes on the 64-bit system, the number of bytes saved into the stack gets larger
than the number of bytes loaded then from the stack.

I think this type of errors is clear to you and you understand that one should be very careful about calls
to overloaded functions when passing actual arguments of a memsize-type.


Diagnosis
PVS-Studio does not diagnose this pattern of 64-bit errors. First, it is explained by the fact that we have
not encountered such an error in a real application yet, and second, diagnosis of such constructs
involves some difficulties. Please write to us if you encounter such an error in real code.

The course authors: Andrey Karpov (karpov@viva64.com), Evgeniy Ryzhkov (evg@viva64.com).

The rightholder of the course "Lessons on development of 64-bit C/C++ applications" is OOO "Program
Verification Systems". The company develops software in the sphere of source program code analysis.
The company's site: http://www.viva64.com.

Contacts: e-mail: support@viva64.com, Tula, 300027, PO box 1800.

Lesson 22. Pattern 14. Overloaded functions

  • 1.
    Lesson 22. Pattern14. Overloaded functions When porting a 32-bit program to a 64-bit platform, you may encounter changes in its logic related to the use of overloaded functions. If a function is overlapped for 32-bit and 64-bit values, the access to it with an argument of a memsize-type will be translated into different calls on different systems. This technique may be useful as, for example, in this code: static size_t GetBitCount(const unsigned __int32 &) { return 32; } static size_t GetBitCount(const unsigned __int64 &) { return 64; } size_t a; size_t bitCount = GetBitCount(a); But this change of logic is potentially dangerous. Imagine a program that uses a class to arrange the stack. This class is specific in that way that it allows you to store values of different types: class MyStack { ... public: void Push(__int32 &); void Push(__int64 &); void Pop(__int32 &); void Pop(__int64 &); } stack; ptrdiff_t value_1; stack.Push(value_1); ... int value_2; stack.Pop(value_2);
  • 2.
    A careless programmersaves into and then selects from the stack values of different types ("ptrdiff_t" and "int"). Their sizes coincide on the 32-bit system and everything is quite okay. But when the size of the type "ptrdiff_t" changes on the 64-bit system, the number of bytes saved into the stack gets larger than the number of bytes loaded then from the stack. I think this type of errors is clear to you and you understand that one should be very careful about calls to overloaded functions when passing actual arguments of a memsize-type. Diagnosis PVS-Studio does not diagnose this pattern of 64-bit errors. First, it is explained by the fact that we have not encountered such an error in a real application yet, and second, diagnosis of such constructs involves some difficulties. Please write to us if you encounter such an error in real code. The course authors: Andrey Karpov (karpov@viva64.com), Evgeniy Ryzhkov (evg@viva64.com). The rightholder of the course "Lessons on development of 64-bit C/C++ applications" is OOO "Program Verification Systems". The company develops software in the sphere of source program code analysis. The company's site: http://www.viva64.com. Contacts: e-mail: support@viva64.com, Tula, 300027, PO box 1800.