How to Change Static to Dynamic Arrays?

Hello Professionals,

I had a discussion with my professor recently about the program I am currently working on. I explained to him that when I tried to initialize thousands of values in my arrays, my program has some problems in running.

For example:

1
2
#define NMAX 500
float aLU[NMAX][NMAX + 1];


These codes are not executed and thus the C++ compiler tells me that it not executable and has some problems with exceptionals and probe pages.

As a solution of my professor, he told me to replace this "static" coding to "dynamic" coding of arrays.

For example of static:

float a[10][5];

For example of dynamic:

1
2
3
4
5
float **a;
a = new (*float) [10];
for (int i=0,i<10; i++){
 a[i]=new float [5];
}


For the dynamic, I tried to type these codes to my C++ program, but at some point, it is not working, what is the wrong in my code? Thank you in advance.

Last edited on
The problem is, you don't have enough memory for a 500x501 array of floats. No matter how you try to allocate it, it just won't fit.

Memory, there's never enough of it. You have to find another way, as we all have to.
Another update:

I did incorporate this pattern now (from which I saw from StackOverFlow website):

https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new

From that pattern, I did edit mine like this:

1
2
3
4
float **a= new float*[1000];
for (int i = 0; i < 1000; i++) {
	a[i] = new float[1000];
}



However, line 2 shows there is error and I am still trying to figure out now what is the problem? :(
Last edited on
Either:
1
2
const std::size_t NMAX = 500 ;
static double aLU[NMAX][NMAX + 1]; // note: static 


Or, if dynamic arrays are required, use std::vector<>
https://cal-linux.com/tutorials/vectors.html

#include <vector> and then:

1
2
std::size_t NMAX = 500 ;
std::vector< std::vector<double> > aLU( NMAX, std::vector<double>(NMAX+1) );


Note: favour using double as the default floating point type.
Last edited on
Thank you very much @JLBorges and @kbw for your help...

I am still thinking why my format is not working?

https://i.imgur.com/mvSboGU.png

Provided that I think there is no problem with it?? :/

My professor advised me to use it but it seems like it is not working properly... I also saw the same format from the StackOverFlow but why it is not working properly... :S

I think my code has the same format as this code from this link:
https://stackoverflow.com/questions/16001803/pointer-to-pointer-dynamic-two-dimensional-array

1
2
3
4
5
6
7
int **board;
board = new int*[10]; // dynamic array (size 10) of pointers to int

for (int i = 0; i < 10; ++i) {
  board[i] = new int[10];
  // each i-th pointer is now pointing to dynamic array (size 10) of actual int values
}
I think I have found now the issue why it is not working...

Hayssstt... Why I haven't noticed it... So shame of myself... :/

All I need to do is... if I am going to use the pattern I have shown above:

1
2
3
4
float **a= new float*[1000];
for (int i = 0; i < 1000; i++) {
	a[i] = new float[1000];
}


All I have to do is to make sure that I initialized the line 1 "before" the int main() function...
...and the lines 2-4 should be incorporated inside the int main() function...

Example:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

float **a= new float*[1000];

int main(){

for (int i = 0; i < 1000; i++) {
	a[i] = new float[1000];
}
return 0;

}


Sorry for the confusion guys... :(

Thanks again for always help... God bless.
Last edited on
All I have to do is to make sure that I initialized the line 1 "before" the int main() function...
No.

...and the lines 2-4 should be incorporated inside the int main() function...
Yes.
Thanks @coder777. You are correct :D
Topic archived. No new replies allowed.