logical difference between pointers and references in Go and C++? -
regardless of provided convenience of references on pointers such needleless of dereferencing , rules specific use of each ,
is there logical reason provide 2 language constructs pointers , references or syntactic sugar?
(i guess ultimate underlying implementation compiler use same steps references pointers implying/checking rules defined references language.)
note : question not rules have defined languages on references such "references not allowed assign null in c++ pointers" etc.
you asking 2 questions, if understand correctly
- what difference between pointers , references
- why support both data types
here goes:
- a pointer refers location in memory datatype resides. size of pointer fixed, given underlying hardware, 4 or 8 bytes - totally regardless of in fact pointing to. furthermore, pointer can passed function using invalid value -
foo(reintepret_cast<int *>(0xdeadbeef) );
. in contrast, reference ensures underlying data valid - since reference alias the object , can't moved being (providing the referenced object still in scope - edited per remark below). - there are reasons support both types. first reason ensure data passed functions valid - without wasting cycles on testing pointer validity (not
null
). second reason 1 can sure not data pointing valid location, pointing @ valid data object. main reason reference allows enjoy benefit of calling function without passing arguments value, yet still maintaining guarantee argument refers valid value.
Comments
Post a Comment