1

This question may be a little bit self-explanatory for some of you but I am really trying to understand what are the similarities between: List myList = new ArrayList<Vertex>(); { In JAVA } and Vertex* myList = new Vertex[size] {In C++}. What's the point of that * in C++ that does not exist in Java. I know that it points to the first element of the list..when are we supposed to use *? Is it just for vectors or?

Ryk
  • 123
  • 7
  • `Vertex* myList = new Vertex[size]` has been replaced by `std::vector myList(size)`. Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Nov 30 '18 at 20:07
  • Vertex* myList means that myList is a pointer to a Vertex (because standart C arrays are stored as a pointer to the first element). https://www.tutorialspoint.com/cplusplus/cpp_pointers.htm – Lev Leontev Nov 30 '18 at 20:12
  • It is a mistake to think that Java `ArrayList` is like an array. They are two very different things. – Andy Turner Nov 30 '18 at 20:12
  • 1
    @AndyTurner I wouldn't say they're so different that comparisons can't be made, but this is definitely a bad example. Still, I think this question is more "What is a pointer?" than a question about arrays. – Christopher Schneider Nov 30 '18 at 20:14
  • There is an interesting thing in the description of go's type syntax which explains about `*`: although we tend to write `Vertex* myList`, the original intent was to write `Vertex *myList`, to mean that "the type of `*myList` is `Vertex`". Since `*` dereferences a pointer type to its type, this means that `myList` is a pointer to a `Vertex`. It's a bit roundabout, but that is, apparently, where the notation arose. – Andy Turner Nov 30 '18 at 20:27

1 Answers1

1

We cannot talk about similarities/differences when these two languages have different assumptions.

In Java everything is a reference - you are creating objects, they are being stored somewhere (on stack/heap) but you are accessing them only by reference. That means that in myList there will be address kept for the ArrayList instance (real instance - in memory)

In C++ you can access object as objects (with all after-effects like copying whole object when passing as argument to function) but also by reference (using pointers - just to avoid such after-effects but also to use some special gifts given to pointers like iterating over memory cells).

Yeah - about iterating over computer memory - the funny fact in C++ is that it's allocating solid block of memory (I mean - next N cells/addresses in row) to put array there - that's why you can declare your array as

int* a = new int[100]; // this is called dynamic allocation

because what you need is the address of the first element, and the object size (given by pointer type). You can jump to the next element simply by doing

a++

To differ using object/pointer you need (or do not need) asterix operator * and that's why it occurs in C++.

read also What exactly is the purpose of the (asterisk) in pointers?

m.antkowicz
  • 13,268
  • 18
  • 37
  • Oh alright. But which one is better in C++ then, access by reference or by object? – Ryk Nov 30 '18 at 20:18
  • actually when it comes to arrays you can only access them as pointers - even when you are declaring array as array (I mean `int a[100]`) it still will be sent to functions and kept under `a` variable as pointer the the first element - that limitation is because of performance (try to imagine copying 1mln elements on function call). Of course dereffered array element like `a[100]` or `a++` is an object unless you are keeping another pointers in array - why not – m.antkowicz Nov 30 '18 at 20:20
  • Oh so basically whenever I declare an array `int a[100]` c++ has to copy that for me into a pointer variable `int* a` which is means extra work? – Ryk Nov 30 '18 at 20:24
  • it does not mean extra work - I'm pretty sure that it's being changed during compilation – m.antkowicz Nov 30 '18 at 20:25
  • I never checked that but really I don't think so – m.antkowicz Nov 30 '18 at 20:27