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

  1. what difference between pointers , references
  2. why support both data types

here goes:

  1. 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).
  2. 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

Popular posts from this blog

c++ - What's the differece between of link to a dynamic file and as a input object? -

javascript - Feed FileReader from server side files -

Android Unit Testing / Mockito: android.location.Location not mocked -