A
Anton Ishmurzin
Greetings,
I would like to know, what's the right way of creating/initialising dynamic
array (which is multidimensional, generally speaking).
So I have some data represented by a matrix. dimensions, as well as values,
are determined at runtime. In order to hide the initialization of the
dynamic array, I wrote a function, that has to do the job.
It does it, but I am a bit concerned about it's correctedness.
That's the program. Questions will follow.
----CUT-HERE----
// program start here
#include <iostream>
#define N 10
// let's assume that I get this N from somewhere else, say
// from the user input, but I put it here like a macro for the sake of
// clarity. It explains dynamic memory allocation I perform below.
using namespace std;
double ** initialize_c();
int main()
{
double ** c;
c = initialize_c(); // A
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
cout << c[j] << " ";
cout << endl;
};
return 0;
};
double ** initialize_c()
{
double ** c;
c = new double * [N];
for (int i = 0; i < N; i++)
c = new double[N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
c[j] = 1.0;
return c;
};
----CUT-HERE----
So here are the questions:
if I repeat the line A (for instance, put it into a loop), creates it a new
pointer c in each loop entry?
Because as far as I understand, two lines with 'new' within the
initialize_c() allocate new memory for the array.
If the answer to the first question is "yes", then how'd i change the code
in order to make it initialize the pointer c at one and the same address
always?
How to free memory after such an initialisation?
Should I put a "static" specifier when I declare "double ** c" in
initialize_c()?
What are the best practices to initialize a multidimensional dynamic array?
Best regards,
Anton.
I would like to know, what's the right way of creating/initialising dynamic
array (which is multidimensional, generally speaking).
So I have some data represented by a matrix. dimensions, as well as values,
are determined at runtime. In order to hide the initialization of the
dynamic array, I wrote a function, that has to do the job.
It does it, but I am a bit concerned about it's correctedness.
That's the program. Questions will follow.
----CUT-HERE----
// program start here
#include <iostream>
#define N 10
// let's assume that I get this N from somewhere else, say
// from the user input, but I put it here like a macro for the sake of
// clarity. It explains dynamic memory allocation I perform below.
using namespace std;
double ** initialize_c();
int main()
{
double ** c;
c = initialize_c(); // A
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
cout << c[j] << " ";
cout << endl;
};
return 0;
};
double ** initialize_c()
{
double ** c;
c = new double * [N];
for (int i = 0; i < N; i++)
c = new double[N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
c[j] = 1.0;
return c;
};
----CUT-HERE----
So here are the questions:
if I repeat the line A (for instance, put it into a loop), creates it a new
pointer c in each loop entry?
Because as far as I understand, two lines with 'new' within the
initialize_c() allocate new memory for the array.
If the answer to the first question is "yes", then how'd i change the code
in order to make it initialize the pointer c at one and the same address
always?
How to free memory after such an initialisation?
Should I put a "static" specifier when I declare "double ** c" in
initialize_c()?
What are the best practices to initialize a multidimensional dynamic array?
Best regards,
Anton.