0

I have a project and I have to define an array of arrays of different dimensions (like a triangle) because I am not allowed to use std::vector or other container class. For this, I am using an array of pointers. Normally, I would do this:

int* triangle[n]; 
for(int i = 0; i < n; i++) {
    triangle[i] = new int[i + 1];
    for(int j = 0; j <= i; j++) cin >> triangle[i][j];
}

But I must not use dynamic memory! I thought that doing

int* triangle[n];
for(int i = 0; i < n; i++) {
    int row[i + 1];
    triangle[i] = row;
    for(int j = 0; j <= i; j++) cin >> triangle[i][j];
}

would do the trick. But it didn't. Instead, when I iterate over the arrays and print the contents, I get garbage. So, how can I replace a dynamic allocation with a static one?

Stefan Octavian
  • 593
  • 6
  • 17
  • why not use `std::vector`? – UnholySheep Feb 07 '18 at 21:05
  • I must not use it as well.I have to use arrays and pointers. It's a challenge. – Stefan Octavian Feb 07 '18 at 21:05
  • So the sizes of the array are compile time constants? otherwise you cannot avoid dynamic memory allocations – UnholySheep Feb 07 '18 at 21:06
  • 5
    *"I must not use it as well.I have to use arrays and pointers. It's a challenge."* More of a waste if time than a challenge if you ask me. – Baum mit Augen Feb 07 '18 at 21:09
  • UnholySheep Now you raise me another question: is declaring an array of a dynamic size a dynamic allocation? I thought dynamic allocation is only using ```new``` – Stefan Octavian Feb 07 '18 at 21:09
  • 2
    *" is declaring an array of a dynamic size"* it is not valid C++, it is VLA extension. – Jarod42 Feb 07 '18 at 21:10
  • 1
    Declaring arrays with non-compile time constant sizes isn't even standard C++. It's an extension that *some* compilers provide – UnholySheep Feb 07 '18 at 21:10
  • Baum not Augen, In my country, the school system of learning c++ is not so developed so if you are training for a contest (as in my case) you are not allowed to use anything that is out of the scholar programme. – Stefan Octavian Feb 07 '18 at 21:11
  • You already noticed that you're getting *garbage*, think about what happens to `row` at the end of the loop body and what that implies when you dereference one of those rows outside the loop... – BeyelerStudios Feb 07 '18 at 21:12
  • 1
    Not related, but use `@username` when replying to people not under their own posts. Otherwise they might not get a notification and not reply. – HolyBlackCat Feb 07 '18 at 21:12
  • 1
    In order to do a static allocation, you'll need to be able to provide all of the necessary array-sizes at compile-time. Assuming that's doable, then int triangle[n][p] (where p is the maximum size of a row) would do the trick. – Jeremy Friesner Feb 07 '18 at 21:13
  • @StefanOctavian *you are not allowed to use anything that is out of the scholar programme* -- So your school teaches to use illegal / invalid C++? Or maybe you should thank the commenters for pointing out to you that `int* triangle[n]; ` is not valid, and you may have had points taken away from you by your teacher if you kept that code in place. – PaulMcKenzie Feb 07 '18 at 21:31
  • @PaulMcKenzie That is not something my teacher taught me. I saw that it works in Code::Blocks, used it once, my teacher said nothing about that and I thought it is perfectly ok to use it. Yes, I should thank them for that. Also, there are no points, only grades. – Stefan Octavian Feb 08 '18 at 07:22
  • @StefanOctavian -- This is the reason why I wish that `gcc` (the compiler that is used in Code::Blocks) would turn **off** this option by default. Your post is one of many each day where the new programmer is led down the wrong track by writing invalid code. Your code will *not* compiler if given to a compile such as Visual C++, or even `gcc` when given the correct flags. – PaulMcKenzie Feb 08 '18 at 07:33

2 Answers2

3

So, if you want "array of arrays" simplified, your choice is array linearization. It means that instead of 7 arrays of 10 items each you should declare single array of 70 elements. Then, just change nested indexes with a function that calculates resulting shift in linearized array, and viola!

Here you can find one of such examples: How to map the indexes of a matrix to a 1-dimensional array (C++)?

In case you don't know in advance how long is your array, it may be a tough choice to determine preliminary reservation size (for example, STL containers like vector etc. do the same trick: they allocate a chunk of memory then grow the container until free capacity left, then re-allocate bigger chunk moving the data from old to new buffer, and again, and again, and again...)

Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42
0

As @BeyelerStudios pointed out in the comments, at the end of the for loop, the memory allocated for the row array gets freed. When I am trying to print out the contents, I am dereferencing pointers to this freed up memory, so I am getting garbage. Thank you!

Stefan Octavian
  • 593
  • 6
  • 17