-3

Are these definitions correct in C#?

  • Declaration = memory allocation
  • Initialization = set initial value
  • Definition = declaration and initialization
  • Assignement = set a new value
static void Main()
{
    // Declaration = memory allocation
    int x;        
            
    // Initialization = set initial value
    x = 0;
            
    // Definition = declaration and initialization
    int y = 0;
            
    // Assignement = set a new value
    y = 1;
}
Progman
  • 16,827
  • 6
  • 33
  • 48
Bey Réda
  • 57
  • 5
  • 2
    Let's see if the StackOverflow gods are gracious and [Jon Skeet](https://stackoverflow.com/users/22656/jon-skeet) shows up in the comments. Read up on this: https://medium.com/c-programming/c-memory-management-part-1-c03741c24e4b There is no simple yes or no answer to your question as it depends. More over you can't really generalize this to any language as it always depends. If declaration always meant allocation, you could crash your computer without your program ever doing or executing anything at all. Which would be very bad. –  Feb 12 '23 at 21:49
  • Maybe I should write this : Declaration = to bring a variable to the knowledge of the compiler – Bey Réda Feb 12 '23 at 21:56
  • In C# not really as C# uses something called JIT compilation. JIT meaning Just-In-Time. So basically only the parts of your code required to run your program are currently compiled and interpreted. The rest is loaded as-needed. So no, not everything you declare is known to the compiler all the time. See [this](https://stackoverflow.com/questions/95635/what-does-a-just-in-time-jit-compiler-do#:~:text=A%20JIT%20has%20access%20to,the%20program%20is%20first%20run) post for more information on JIT –  Feb 12 '23 at 22:01
  • The declaration of `int x;` (without explicit assignment) automatically assigns the `default(int)` value to `x` - which is `0`. You cannot have a declaration without an initialization. And there's no guarantee that there is any memory allocation at all - probably there is, but it's not always done. – Enigmativity Feb 12 '23 at 23:56

1 Answers1

0

Strictly in terms of C# the above are not exactly correct

// Declaration = memory allocation
int x;        

For types with value-semantics the above is true, but for types with reference-semantics it is not, because there is no allocation that happens. The difference is that memory allocation happens with the new keyword with references. With value types, they are stored in the stack, and the stack is pre-allocated to 4Mb size when a .NET process starts.

An example would be

string x;

the above statement only tells the compiler what x is, and nothing happens really in the program when this is compiled.

// Initialization = set initial value
x = 0;

Here again, I would argue the above is just an assignment, and not necessarily an initial value as nothing stops you from writing

x = 1;
x = 2;

There is nothing different between these two assignments, making one of the an initial value and the other not. This is because we don't know if x was assigned a value somewhere else before.

// Definition = declaration and initialization
int y = 0;

I would call it just an initialization as here fore sure a value is assigned fro the first time.

// Assignement = set a new value
y = 1;

As I said before the above is just an assignment.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133