1

In Microsoft Visual C++ 6.0, if I compile and run code as following:

#include "stdafx.h"

int main(int argc, char* argv[])
{
    int arr[5] = {1,2,3,4,5};
    printf("%d\n", sizeof &arr);
    printf("%d\n", sizeof(&arr));

    return 0;
}

The result is:

20
20

However, the result of other compiler such as gcc is:

4
4

Is this a bug of VC6 or it just follows a different compile rule?

The type of &arr should be an array pointer(int (*)[5]), so the result should be the size of a pointer rather than that of an array. Therefore, I think it is a bug of VC6.0. Analysis for this from assembler language side will be helpful.

  • 1
    If the array has 10 elements, what size do you get? This would allow you to find whether it's the size of array or a 20-bit pointer. https://stackoverflow.com/questions/6751749/what-is-the-size-of-a-pointer – VLL Dec 02 '22 at 08:55
  • Just to clarify, what specific commands are you running to compile this? I'm guessing it's `gcc code.cpp` for one version, and some equivalent to run VC6 on your windows version - the fact that you're compiling one as C and the other as C++ may be a factor (though `gcc` and `g++` gave the same output when I tried it). – ymbirtt Dec 02 '22 at 09:05
  • 1
    @VLL It will be 40 so that I'm sure it's the size of array. – Black phoen1x Dec 02 '22 at 09:54
  • 1
    @ymbirtt I am using Visual Studio 6 with MSDN in my Windows XP VM. I just pressed F7, then F5 to get into debug. – Black phoen1x Dec 02 '22 at 10:00
  • Can't reproduce in VS-2022 - this gives the size of a pointer for both. Could be a bug in the earlier VS. – Adrian Mole Dec 02 '22 at 10:52
  • ... in fact, neither can I reproduce it using the VS-2010 build tools. – Adrian Mole Dec 02 '22 at 11:02

0 Answers0