dynamic memory allocation with multidimensional array

How can i create a multidimensional array for more than 3 dimension like below: int dim1, dim2, dim3;
dim1=10000; dim2=99; dim3=9;
#define s(i,j,k) (array[dim2*dim3*i + dim3*j + k])
int * array = (double *)malloc(dim1*dim2*dim3*sizeof(int));
If someone knows please tell me, i really need this thing.
Last edited on
How many elements are in 1D array? dim1
How many elements are in 2D array? dim1*dim2
How many elements are in 3D array? dim1*dim2*dim3

Do you notice any rule or trend in those three? Product of dimensions?
If i do:
#define s(a,b,c,d) (array([dim2*dim3*dim4*a+dim3*dim4*b+dim4*c+d])
double * array=(double *)malloc(dim1*dim2*dim3*dim4*sizeof(double));
for 4d array, Is this pattern ok?
I wrote this code but its showing segmentation error:
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
int main()
{
int d1,d2,d3,d4,d5,d6,d7,d8,d9;
d1=100, d2=100, d3=100,d4=255,d5=255,d6=255,d7=255,d8=255,d9=255;
#define s(i,j,k,l,m,n,o,p,q) (array[d2*d3*d4*d5*d6*d7*d8*d9*i+d3*d4*d5*d6*d7*d8*d9*j+d4*d5*d6*d7*d8*d9*k+d5*d6*d7*d8*d9*l+d6*d7*d8*d9*m+d7*d8*d9*n+d8*d9*o + d9*p + q])
double * array = (double *)malloc(d1*d2*d3*d4*d5*d6*d7*d8*d9*sizeof(double));
s(45,45,45,45,45,45,45,45,45)=45;
cout<<s(45,45,45,45,45,45,45,45,45)<<endl;
}
Last edited on
First, certainly malloc() is returning NULL because the memory allocation request fails.
Second, malloc() and free() are C-isms and should not be used in favor of new and new[], delete and delete[].
Third, you are not freeing your memory. Preferably use a container, a unique_ptr, or at least delete it.

Finally, why on earth do you need a nine-dimensional array?
Here's why malloc() is probably returning a null pointer:

Let's do some basic mathematics.
These are the dimensions of your arrays, zero-indexed.
d = (100, 100, 100, 255, 255, 255, 255, 255, 255)
Mathematically, the expression for the number of memory cells you need (doubles, in this case), is given by

Π(d_i + 1) = 101*101*101*256*256*256*256*256*256 = (101^3) * (256*6) = 290 003 949 979 965 587 456 cells of memory. This quantity is then multiplied times the sizeof each element to get the number of bytes required.

Trying to dynamically allocate that much memory is unrealistic. If you need this much data, you'll need something special-purpose. Most of us don't have that much disk-space, not to mention random-access memory. malloc() returns (void*)0 when it fails.

Edit: I got the number several orders of magnitude too small. This problem is (completely) intractable; find another approach.

Please use code tags.
Last edited on
Sir isn't their any way i can do this operation, by any means. momory is not problem for me, problem is this thing, cas of which my algorithm is stuck. this one is last thing which i need. isn't their any other method by which i can occupy large memory with vectors/arrays or containers. isn't their? Although it may take large memory, but by finishing this operation my algorithm will work very very fast, as theory says which i'm trying to test, but stuck cas of this problem.
Last edited on
I don't think you grasped the size of the numbers involved.
If you could store the data on a computer hard drive with a size of say 1Tbytes, it would take yourself and about two billion others (roughly one third of the population of the planet Earth), each having a similar computer all added together, to hold that much data.

sizeof(double) = 8 bytes.
d1*d2*d3*d4*d5*d6*d7*d8*d9*sizeof(double) = 2199535975125000000000 bytes
express in Tbytes = 2199535975.125
share among two billion computers hard drives.


You need to reconsider a different approach to the problem.




Dinesh999lama
You have already posted on this topic - and got pretty well the same response.
In your other thread the indices with 256 elements were apparently for each of the rgb possibilities at two points, but this shows a fundamental confusion about the number of elements in an array and the range of values that each element could take - which could actually be stored in one byte.

I suspect that you need at most
101x101x101x3x3
memory stores, but perhaps you could try to explain what exactly you are assigning your 9 dimensions to.
Well the first one was working but as i do something like s(3,4,5)=3 than access s(3,4,6) than its value is also coming as 3. till 3 and 5 dimension the method i posted in this question is working fine for small size, but as i go more big size it is showing segmentation error. Here the values of arrays which has not been given should only show 0 but it is not happening as dimension of array increases.

This is for computer vision project, here in first cpp file the computer saves all the possible values of d1,d2,..rp0,gp0.. which is like a computer recognising a rigit object. I've pasted last part of my code:

std::vector<float> xp0, yp0, zp0, xp1, yp1, zp1, xp2, yp2, zp2, cp0, cp1, cp2;
for(int p0=0;p0<=no[nobj] && ros::ok();p0++) {
xp0.push_back(xw.at(p0)); yp0.push_back(yw.at(p0)); zp0.push_back(zw.at(p0));
//cp0.push_back(cc.at(p0));
for(int p1=0;p1<=no[nobj] && p1!=p0 && ros::ok();p1++) {
xp1.push_back(xw.at(p1)); yp1.push_back(yw.at(p1)); zp1.push_back(zw.at(p1));
//cp1.push_back(cc.at(p1));
float d1=sqrt(pow(xp1.back()-xp0.back(),2)+pow(yp1.back()-yp0.back(),2)+pow(zp1.back()-zp0.back(),2))*1000;
if(d1>20 && d1<150 ) {
for(int p2=0;p2<=no[nobj] && p2!=p1 && ros::ok();p2++) {
xp2.push_back(xw.at(p2)); yp2.push_back(yw.at(p2)); zp2.push_back(zw.at(p2));
// cp2.push_back(cc.at(p2));
float d2=sqrt(pow(xp2.back()-xp1.back(),2)+pow(yp2.back()-yp1.back(),2)+pow(zp2.back()-zp1.back(),2))*1000;
float d1d=sqrt(pow(xp2.back()-xp0.back(),2)+pow(yp2.back()-yp0.back(),2)+pow(zp2.back()-zp0.back(),2))*1000;
if(d2 > 20 && d2 < 150) { s=s+1;
xo.data = p0; bagw.write("numbers", ros::Time::now(), xo);
xo.data = d1; bagw.write("numbers", ros::Time::now(), xo);
xo.data = d2; bagw.write("numbers", ros::Time::now(), xo);
xo.data = d1d; bagw.write("numbers", ros::Time::now(), xo);
xo.data = rp.at(p0); bagw.write("numbers", ros::Time::now(), xo);
xo.data = gp.at(p0); bagw.write("numbers", ros::Time::now(), xo);
xo.data = bp.at(p0); bagw.write("numbers", ros::Time::now(), xo);
...
}}}}}

In next cpp file, the computer uses this datas of a particular object to detect an object from a 2d colored image. The data has to be analyzed and saved from a 3d registered image, like by using kinect camera. In second file if the object is present is image in any oriantation, distance, the computer will detect and recognize it, even if the object is partially behind another object. so the advantage of this algorithm is that even from a 2d color image, this will extract the 3d information of an object like its distance, oriantation and determine which object it is. the second code is something like:

pcl::PointCloud<pcl::PointXYZRGB> output;
pcl::fromROSMsg(*input,output);
int iv=-1; int ip0, d1, d2, d1d, cp0, cp1, cp2;
rosbag::Bag bag("cup.bag");
rosbag::View view(bag, rosbag::TopicQuery("numbers"));
BOOST_FOREACH(rosbag::MessageInstance const m, view)
{
std_msgs::Float32::ConstPtr i = m.instantiate<std_msgs::Float32>();
iv = iv+1;
if(iv%13==0) {
ip0 =(int) i->data; }
if(iv%13==1) {
d1 =(int) i->data; }
if(iv%13==2) {
d2 =(int) i->data; }
if(iv%13==3) {
d1d =(int) i->data; }
if(iv%13==4) {
cp0 =(int) i->data; }
if(iv%13==5) {
cp1 =(int) i->data; }
if(iv%13==6) {
cp2 =(int) i->data; }
// cout<<d1<<"\t"<<d2<<"\t"<<d1d<<"\t"<<cp0<<"\t"<<cp1<<"\t"<<cp2<<endl;
if(d1<150 && d1>0 && d2<150 && d2>0 && d1d<150 && d1d>0 && cp0<256 && cp1<256 && cp2<256 && cp0>0 && cp1>0 && cp2>0) {
cout<<d1<<"\t"<<d2<<"\t"<<d1d<<"\t"<<cp0<<"\t"<<cp1<<"\t"<<cp2<<endl;
s(d1,d2,d1d,cp0,cp1,cp2)++;
int is=s(d1,d2,d1d,cp0,cp1,cp2);
p0(is,d1,d2,d1d,cp0,cp1,cp2)=ip0;
cout<<is<<"\t"<< p0(is,d1,d2,d1d,cp0,cp1,cp2)<<endl;
}
}
bag.close();
...
pcl::PointXYZRGB p; vector<int> iP0;
for(int i=0;i<=im;i++) {
int xa1=xa[i], ya1=ya[i];
p=output.at(ya1,xa1); int ra=p.r, ba=p.b, ga=p.g;
for(int r=10;r<=100 && ros::ok();r++) {
float la=360/(2*3.145*r);
for(float t=0;t<360 && ros::ok();t+=la) {
int xb1=xa1+r*cos(t*(PI/180)); int yb1=ya1+r*sin(t*(PI/180));
p=output.at(yb1,xb1); int rb=p.r, bb=p.b, gb=p.g;
float ab1=sqrt(pow(xb1-xa1,2)+pow(yb1-ya1,2));
if(ab1>2 && ab1<99) {
for(int rd=10;rd<=100 && ros::ok();rd++) {
la=360/(2*3.145*rd);
for(float td=0;td<360 && ros::ok();td+=la) {
int xc1=xa1+r*cos(td*(PI/180)); int yc1=ya1+r*sin(td*(PI/180));
p=output.at(yc1,xc1); int rc=p.r, bc11=p.b, gc=p.g;
float bc1=sqrt(pow(xc1-xb1,2)+pow(yc1-yb1,2));
float ac1=sqrt(pow(xc1-xa1,2)+pow(yc1-ya1,2));
if(bc1 && ac1 > 10) {
for(int d1=2;d1<=10 && ros::ok();d1++) { // ad represents all the elements of d1 vector
for(int d2=2;d2<=10 && ros::ok();d2++) { // de represents all the elements of d2 vector
d1d=sqrt(ac1*ac1+pow(sqrt(d1*d1-ab1*ab1)-sqrt(d2*d2-bc1*bc1),2));
if(d1d>2 && d1d<99) {
//for(int i=1;i<=s(d1,d2,d1d,ra,ga,ba,rb,gb,bb,rc,gc,bc11);i++) {
//cout<<i<<"\t"<<p0(i,d1,d2,d1d,ra,ga,ba,rb,gb,bb,rc,gc,bc11)<<endl;
//iP0.push_back(p0(i,d1,d2,d1d,ra,ga,ba,rb,gb,bb,rc,gc,bc11));
} //}
}}}}}}}}


Till now if the object is rigid than acc to theory this will easily recognize any object from any distance and any angle of view. the first method which i posted in first quetion just didn't showed error but didn't worked. this one works without error but as size increases segmentation error comes.
I'm combining ROS, Gazebo, PCL and will be using OpenCV for this task.
Last edited on
dinesh999lama

I think I struggled in your code at the line
}}}}}}}}
though I did notice lots of calculations of distances between two points.

Could you possibly point out which line in your code actually contains an array with 9 dimensions.
actually i was trying to use p0() as 9d array at minimum, but if it is possible to make its size 13 than it would be more good. but in another forum i got the answer how to solve this problem. I am learning tensorflow to solve this problem, and as i see tensorflow is mainly made for this kind of big arrays.
actually i was trying to use p0() as 9d array at minimum, but if it is possible to make its size 13


!!!!!!
Topic archived. No new replies allowed.