473,378 Members | 1,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Dynamic Mem Allocation problem or a Compiler Mem leak?

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
5 1458
JosAH
11,448 Expert 8TB
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
BenQ
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
JosAH
11,448 Expert 8TB
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
9,208 Expert Mod 8TB
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
JosAH
11,448 Expert 8TB
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

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

Similar topics

9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.