Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamically allocated two-dimension arrays

Status
Not open for further replies.

Lili70

Programmer
Jun 9, 2003
3
US
I have a problem with accessing two-dimension array in the subprogram which was allocated dynamically in the main program.
The declaration is:

const short mdeme;
double (*nv)[mdeme] = new double[nval][mdeme];

When I'm tring to initialize an array in subprogram I'm getting an error: Access violation.
With the debugger I see that firt step into the subprogram is changing the address of the pointer to the array to 0's.
I tried different ways and still can't understand what I'm doing wrong?
 
Dynamically allocated arrays are tricky. Adding more dimensions to the array makes it even trickier. If you're using C++, I recommend using the STL's vector type. So a typedef for a two-dimensional array would look like this:

[tt]template<type _E> typedef vector2<_E> vector<vector<_E>>;[/tt]

So for a dynamically allocated two-dimensional array of 'double' values would look like this:

[tt]vector2<double> nv;[/tt]

Then you can initialize it to any size, like this:

[tt]nv[4][8] = 123.456;[/tt]

And the vector will automatically grow and allocate more storage to accomodate the largest dimension.


[sub]I REALLY hope that helps.[/sub]
Will
 
Will :

>>Then you can initialize it to any size, like this
>>nv[4][8] = 123.456;
>>And the vector will automatically grow and allocate more storage to accomodate the largest dimension.


Is it really possible for a vector to resize just like that ?

/JOlesen
 
Thanks for answers, but..
1.I couldn't find quit decent literature about vector<vector>
2.it's imposible(at least in my little experiense) to resize vector by simple nv[][] = 1. I have to use resize method, which allow you to use one dimension at once.
I had no problem(or almost no problem :)) to work with a container as a vector, but not with vector<vector>. And I don't want declare few extra vectors just to create vector inside of another vector.
I have no problem also to use in C++ classes, but in this situation I have to avoid it. Whole package written on C by another programmer and I'm trying to switch it to C++ with as less &quot;blood&quot; as possible. :)
The most interesting things that with declaration using &quot;malloc&quot; I have no problems. Why it's not tricky with C?
 
Hi,
i don't know that the following code can help you or not.
int i;
double **nv;
nv = new double*[10];
for (int i=0; i<10;i++)
nv = new double[20];


//delete
for(i=0; i< 10; i++)
delete[]nv;

delete[]nv;
 
Hi,
the correct is :
Code:
  int i;
  double **nv;
  nv = new double*[10];
  for (int i=0; i<10;i++)
    nv[i] = new double[20];


  //delete
  for(i=0; i< 10; i++)
      delete[]nv[i];
  delete[]nv;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top