0

For a homework of mine, I'm trying to mimic the way ArrayList was working in Java using C++. I want to create a class that contains an dynamically allocated array of pointers that are storing my user-defined class instances. However I was having a hard time fixing memory leaks, so I've decided to debug my code piece by piece and stumbled upon an error when trying to understand how to improve my code. After this long introduction my current problem is the following: I've created an array of integer pointers and then assigned them new pointers that are storing a automatically allocated integer. When trying to print the contents of this array I got nothing, however compiler does not complain about anything. If someone can show why am I mistaken while doing this I think I can fix my homework code as well.

int main() {

    int** a = new int* [4];

    for (int i = 0; i < 4; i++) {
        int b = 0;
        int* c = new int;
        *c = b;
        *(a + 1) = c;
    }

    for (int i = 0; i < 4; i++) {
        cout << **a << endl;
    }

    delete[] a;
    a = nullptr;

    return 0;
}

This is the test code I've written to see how the structer works. Like I've said earlier the terminal shows only a huge chunk of nothing.

  • First of all, for any pointer `a` and index `i` the expression `*(a + i)` is *exactly* equal to `a[i]`. The array-indexing syntax is easier to read, understand and write. Secondly, because of the previously mentioned equivalence, `*a` is the same as `a[0]`. And in your case `**a` is the same as `a[0][0]`. So the loop where you print values, you only print the same value over and over again. – Some programmer dude Oct 26 '22 at 06:35
  • Thirdly, you only initialize `a[1]`, and overwrite that value multiple times in the first loop. Which means you dereference the *uninitialized* pointer `a[0]` in the second loop, leading to *undefined behavior*. All in all I recommend you invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and learn about the standard containers, and treat pointers as an advanced topic. And when you learn about them, read more. – Some programmer dude Oct 26 '22 at 06:35
  • I understand this is homework, but later in C++ you should try to minimize the use of new/delete in your code (to avoid memory leaks) instead of `int** a = new int* [4];` use `std::vector>`. And `new int` is totally unecessary. I wish C++ teachers would start teaching this (instead of "C" style arrays). – Pepijn Kramer Oct 26 '22 at 06:35
  • Individual elements don't need to be pointers in this case, you can allocate ints directly. – HolyBlackCat Oct 26 '22 at 06:36
  • @PepijnKramer Probably just `std::vector`, since the elements have size 1. – HolyBlackCat Oct 26 '22 at 06:37
  • Yes I see now he later only puts one element in. So yeah that would be ok – Pepijn Kramer Oct 26 '22 at 06:41
  • Or even `std::array`. – Some programmer dude Oct 26 '22 at 06:41
  • My takeaway std::array/std::vector lead to better expression of intent in any case :) And "C" style arrays still cause too many issues. – Pepijn Kramer Oct 26 '22 at 06:42
  • *I'm trying to mimic the way ArrayList was working in Java using C++* -- That's your first mistake. Do not use Java or any other language as a model in writing C++ code. Going down this route, all you will end up with are 1) Buggy programs, 2) Inefficient programs, or 3) Programs that look weird to a C++ programmer. Your attempt of "ArrayList" falls into all 3 categories. Use what C++ has available, instead of trying to make C++ look like another programming language. You have `std::vector`, `std::array`, etc. – PaulMcKenzie Oct 26 '22 at 07:55

0 Answers0