Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "C++ void Pointer" and "C# var"

Tags:

c++

c#

While learning C#, this question came to my mind. What is the difference between void and var? Here are the two example I would like to share:

void * voidInt = (void *) 7;
void * voidChar = (void *) 'F';
void * voidCharArray = (void *) "AbcString";

And this is example of var:

var varInt = 7;
var varChar = 'F';
var varCharArray = "AbcString";
  • Is void an anonymous datatype?
  • If yes, then what is the major difference between var and void?

Can someone help me clear this situation?

like image 322
FaizanHussainRabbani Avatar asked Jan 01 '15 06:01

FaizanHussainRabbani


1 Answers

The other answers here are pretty good but I think they do not get clearly to the fundamentals. It is the fundamentals you are confused about, so let's address those.

  • A variable is a storage location that contains a value.
  • A variable is associated with a type.
  • A local variable has a name.

So voidInt, voidChar, voidCharArray, varInt, varChar and varCharArray are all variables, and they all have types associated with them. Each variable can be assigned a value of that type or produce a value of that type, depending on whether the variable is being written to or read from.

OK, so now what are pointers?

  • A type has a corresponding pointer type. (Note that in unsafe C# only the unmanaged types have corresponding pointer types.)
  • The void * type is a special pointer type.
  • A pointer is a value.
  • A pointer of type T* may be dereferenced to produce a variable of type T. T* must not be void*.
  • A pointer may be explicitly converted to or from any integral type, though these operations are permitted to lose information and are dependent on implementation details.
  • Any pointer value may be implicitly converted to void*.
  • Any void* value may be explicitly converted to any pointer type value.

And what is var in C#?

  • var is a "syntactic sugar" that tells the compiler to deduce the type of the variable from the initialzier rather than requiring that it be written out.

And what are "anonymous types" in C#?

  • Some expressions in C# have a type that is not declared and has no name; these are known as the "anonymous" types.

So now we can look at your program and see what each line does.

void * voidInt = (void *) 7;

voidInt is a variable of type void*. The value assigned to it is the conversion of the integer 7 to a pointer, which is almost certainly a garbage pointer on any modern operating system. This code is essentially nonsensical.

More sensible code would be:

int myInt = 7;
int* intPtr = &myInt;
void* voidInt = intPtr;

This means that myInt is a variable which holds the value 7, intPtr is a variable which holds a pointer; when that pointer is dereferenced it produces variable myInt. voidInt is a variable which holds any pointer, and the value read from intPtr is a pointer. So now voidInt and intPtr both hold a pointer to variable myInt.

void * voidChar = (void *) 'F';

Same thing here. The character F is treated as a number and converted to a pointer value, which is stored in the variable. This is not sensible. Sensible code would be something like:

char myChar = 'F';
void *voidChar = &myChar;

But this makes perfect sense:

void * voidCharArray = (void *) "AbcString";

A string literal in C++ is convertible to a char* which is a pointer to the storage for the first character, and that pointer is convertible to void*.

What about this?

var varInt = 7;
var varChar = 'F';
var varCharArray = "AbcString";

This is just a pleasant way to write

int varInt = 7;
char varChar = 'F';
string varCharArray = "AbcString";

Each variable has its given type, and each assignment stores a value of that type in the variable.

What about anonymous types?

var anon = new { X = 123, Y = 456 };

This makes a variable of anonymous type, where the anonymous type has two properties X and Y both of type int. The type has no name, so there is no way to write out the type in the declaration, hence var must be used.

The key thing here is to make sure that you have a grasp of the fundamentals: pointers are values, they may be dereferenced, and doing so produces a variable. Since pointers are values they may themselves be stored in variables of pointer type. This has almost nothing to do with var, which is a pleasant way in C# to make the compiler do the work of figuring out what type a variable should have.

like image 153
Eric Lippert Avatar answered Oct 01 '22 14:10

Eric Lippert