By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,586 Members | 992 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,586 IT Pros & Developers. It's quick & easy.

Dynamic Mem Allocation problem or a Compiler Mem leak?

P: 2
Hi every one,
I've been working on a dynamic array and trying to access to the memory address that correspond to it.
Here is the code:

[i]#include <iostream.h>
#include <fstream.h>

class Matrix{
public:
double ***a;
Matrix (int isize, int jsize, int ksize)
{
a = new double **[isize];
for (int i=0; i<isize; i++)
{
a[i]= new double * [jsize];
for (int j=0; j<jsize; j++)
{
a[i][j] = new double [ksize];
}
}
}//end matrix
double **operator [](int index)
{
return (double **) a [index];
}// end opt
};//end class


int main() {




int ii=3, jj=3, kk=3;
Matrix A(ii , jj , kk);
//delete []A;
//A = NULL;

for (int i=0; i<ii; i++)
for (int j=0; j<jj; j++)
for (int k=0; k<kk;k++)
cout<<i<<"\t"<<j<<"\t"<<k<<"\t"<<&A[j][k]<<endl;

//delete A;


}//end main


and the results says:

0 0 0 0x804b028
0 0 1 0x804b030
0 0 2 0x804b038
0 1 0 0x804b048
0 1 1 0x804b050
0 1 2 0x804b058
0 2 0 0x804b068
0 2 1 0x804b070
0 2 2 0x804b078
1 0 0 0x804b098
1 0 1 0x804b0a0
1 0 2 0x804b0a8
1 1 0 0x804b0b8
1 1 1 0x804b0c0
1 1 2 0x804b0c8
1 2 0 0x804b0d8
1 2 1 0x804b0e0
1 2 2 0x804b0e8
2 0 0 0x804b108
2 0 1 0x804b110
2 0 2 0x804b118
2 1 0 0x804b128
2 1 1 0x804b130
2 1 2 0x804b138
2 2 0 0x804b148
2 2 1 0x804b150
2 2 2 0x804b158


which seems that it jumps one mem cell after the completion of the last loop.
Any ideas are appreciated.

regards.
Aug 6 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
which seems that it jumps one mem cell after the completion of the last loop.
Any ideas are appreciated.

regards.
Did you expect it to be one contiguous area then? You're allocating several
slices for the several parts of your array. Those slices could end up anywhere
in memory; which is not a bad thing but those slices can't be contiguous areas
because of a bit of bookkeeping needed for each area.

kind regards,

Jos
Aug 6 '07 #2

P: 2
Thanks Jos, but what if I want to creat a 3d dynamic array that takes memory like non_dynamic arrayes in C++? (continus mem and row major style )

regards,
Ben
Aug 6 '07 #3

Expert 10K+
P: 11,448
Thanks Jos, but what if I want to creat a 3d dynamic array that takes memory like non_dynamic arrayes in C++? (continus mem and row major style )

regards,
Ben
If you know the dimensions at compile time, you can make a two dimensional
slice of the matrix like this:

Expand|Select|Wrap|Line Numbers
  1. typedef double (*slice_t)[3][4];
  2.  
and allocate everything in one chunk:

Expand|Select|Wrap|Line Numbers
  1. slice_t matrix= (slice_t)calloc(5, sizeof(*slice_t));
  2.  
That gives you 5 slices of 3x4 elements each.

If you don't know the dimensions at compile time you have to do it your way
but don't allocate the actual data elements as different slices, but make all
the other pointer point to the right cells in that one big chunk of allocated
memory.

kind regards,

Jos
Aug 6 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Thanks Jos, but what if I want to creat a 3d dynamic array that takes memory like non_dynamic arrayes in C++? (continus mem and row major style )
Just create the 3D array on the heap like you do on the stack:

Expand|Select|Wrap|Line Numbers
  1. int (*arr)[3][4]  = new int[2][3][4];
  2. //same as:
  3. int arr[2][3][4];
  4.  
Remember, C++ only has one-dimensional arrays. It is the first index that declares the number of elements. The other indexes just describe the elements.

Also, do not use malloc() and free() in C++.
Aug 6 '07 #5

Expert 10K+
P: 11,448
Also, do not use malloc() and free() in C++.
My bad; I was thinking C instead of C++. Forget my previous reply please.

kind regards,

Jos
Aug 6 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.