2

When I try to find the sizeof(A) where A is of type int with size as 'n', n is an int that is not defined. I get an output of 496 and when I give a value to n and then check it, sizeof(A) gives me the same values of 496. I know Array is a static data type so it will have memory irrespective of 'n' but can anyone explain me where the value 496 came from?

int main()
{
    int n;
    int A[n];
    
    cout<<sizeof(A)<<"\n";
    
    cin>>n;
    
    cout<<sizeof(A);
    return 0;
}
JaMiT
  • 14,422
  • 4
  • 15
  • 31
Itzz_CJ
  • 70
  • 5
  • 14
    This is not valid C++. Not only is `n` never initialised, it is not a compile time constant. While some compilers support variable length arrays, the standard does not. Also a note: `A` does not have type `int`. It has type `int[n]` – Lala5th Aug 31 '21 at 03:20
  • Both of the sizeof functions has output 496. – Itzz_CJ Aug 31 '21 at 03:22
  • 2
    The value of `n` is unspecified at the point you use it, and thus your program's behavior is undefined. Also variable length arrays are not a standard part of C++, and you generally should not use them even when available. – Miles Budnek Aug 31 '21 at 03:22
  • @Lala5th Yeah that was my first doubt that I could not do it but one of our teachers told me to try it and I assure you declaring A[n] without declaring n works fine without any errors and it does give me an output. – Itzz_CJ Aug 31 '21 at 03:24
  • @MilesBudnek Yeah I know it is the worst way of doing things, I was just curious as to why the value was coming as 496. – Itzz_CJ Aug 31 '21 at 03:25
  • @Itzz_CJ I am not arguing whether you are getting an error or not, I am stating that it is not standard C++. Just because something is allowed by a compiler does not mean, that it is not ill-formed in the standard. You can ask why something is happening, but because you are using undefined behaviour, there is no way to tell. Look at the assembly generated, and look into how stack space is allocated/prepped for programs, but at that point the question isn't about C++ it is about your specific system which is not incredibly useful in general – Lala5th Aug 31 '21 at 03:25
  • @Lala5th Okay, and yeah I agree its not the standard way to do things, I was just curious as to what the reason behind 496 might be. So I thought maybe someone might have an explanation for why this might be the case. – Itzz_CJ Aug 31 '21 at 03:28
  • 4
    Undefined behavior is undefined. 496 is just as likely as any other result, no result, a crash, or your computer catching on fire. – Miles Budnek Aug 31 '21 at 03:29
  • @Lala5th okay ill try to look into my own stack space and try to find it. I thought it might have to do something with how GNU assigns the memory – Itzz_CJ Aug 31 '21 at 03:30
  • Okay one more doubt, as I am new at stackoverflow and my question is answered (kind of) so should I delete the question or let it remain? – Itzz_CJ Aug 31 '21 at 03:31
  • *"when I give a value to n"* -- Programs are run in order. What you do on one line has no effect on the lines that were executed before that line. Giving a value to `n` on one line has no effect on the lines that were executed before that line. So your demonstration of giving a value to `n` *after using `n`* demonstrates nothing useful. – JaMiT Aug 31 '21 at 03:46
  • @Itzz_CJ See https://stackoverflow.com/help/someone-answers – eerorika Aug 31 '21 at 03:46
  • @Itzz_CJ *and I assure you declaring A[n] without declaring n works fine without any errors and it does give me an output.* -- That code will not compile using Visual C++, for all the reasons given to you so far. If you were to apply for a C++ job, and that is a sample of the code you handed in to the prospective employer using `int A[n]`, you probably won't be hired. So your teacher is doing you a disservice by even introducing you to such code. – PaulMcKenzie Aug 31 '21 at 04:33
  • @Itzz_CJ Also, since it isn't real C++, a C++ compiler that offers this extension can do anything it wants with it. If `sizeof(A[n])` were a million, that would be valid since the code you're showing isn't C++. – PaulMcKenzie Aug 31 '21 at 04:36
  • You are right to think that there is something fishy going on. In the best case this is using a language extension provided by your compiler. and even in that case, since `n` is undefined it is double undefined behavior. cheers! – alfC Aug 31 '21 at 04:38
  • Please, let us know what is your compiler, (so we don't use it ;) ). – alfC Aug 31 '21 at 04:46
  • 1
    @alfC - I'm less concerned about the compiler, since there are a number of C++ compilers that support VLAs as a non-standard extension (compilers that don't support any non-standard extensions are as rare as hen's teeth). I'm more concerned about the fact that a teacher told a student to try it - in a way that implies it is correct, when it isn't. I don't have problems with a teacher who provides information (although this is non-standard, you will find that some C++ compilers support VLAs) but do have a problem with teachers providing "try it, it works" advice. – Peter Aug 31 '21 at 05:12

2 Answers2

5

where A is of type int with size as 'n'

int n;
int A[n];

The type of A is not "int with size as 'n'". The type of A is int[n] which is array of n integers. However, since n is not a compile time constant, the program is ill-formed. If we were to look past the ill-formedness, the value of n is indeterminate. Reading an indeterminate value results in undefined behaviour.

anyone explain me where the value 496 came from?

It came from undefined behaviour. You can find more details by reading the assembly of the compiled program that produced that result.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Yes, 'n' just had a garbage in it after declaring it first and as soon as i reloaded the program the value changed. – Itzz_CJ Aug 31 '21 at 03:47
  • @Itzz_CJ Such is undefined behaviour. The behaviour can change if you run the program again - but it isn't guaranteed to change. Nothing about the behaviour of the program is guaranteed. – eerorika Aug 31 '21 at 03:48
-3

The first cout statement cout<<sizeof(A)<<"\n"; in your code is giving 0 as output. Irrespective of what I take n as input, the next cout statement is also giving a 0. There are two declarations here, int n and int A[n]. As a beginner, it is fair to assume that n remains the same in both cases or has the same value, therefore the size shouldn't change. However, one is an integer(n), the other is an array of integer(A[n]). That makes all the difference!

The first time you print the size of A[n], you are getting a 0 because the array is only declared and not initialized so we know it's empty. The next time, you are taking n as an input, so its size should be 4 bytes(try it yourself) because it's an integer.

Having said that, it really depends on the type of compiler or operating system you are using. I got 4 as an output in one of the online compilers and when I tried implementing it on codeblocks and vscode, I got 32 and 80 respectively. Essentially, this is an undefined behaviour even if n had a garbage value!

hessam hedieh
  • 780
  • 5
  • 18
juhikushwah
  • 33
  • 1
  • 8