using pointers to map to data

M

Matt Jackson

I'm trying to write a program to map the contents of compressed row
storage back to the original indices. What I envision is some program
that given F(i,j) will return the correct column entry for a give row i..

Consider this example:

int c[]={0,10,31,512,12521};

I would like to somehow point to this data by defining a double pointer:

int **b;

b=new int*[num_of_rows];

Then let each b represent a row...

finally have the various entries point to the real array data:

b[0][0]=c[0];
b[0][10]=c[1];
b[0][31]=c[2];
b[0][512]=c[3];
b[0][12521]=c[4];

thus storing only the values I need, rather than wasting enormous amount
of space by creating a 2d int array filled 95%+ with zeros.

Is this even possible? I've tried the following unsuccessfully, which
ironically compiles fine with the g++ but segfaults immediately...

#include <iostream>
using namespace std;


int main(){
int **A;
int *b;
b=new int[4];
b[0]=0;
b[1]=2;
b[2]=6;
b[3]=25;
int g=2;
A=new int* [25];//assume there are 25 rows...
A[0][0]=b[0];
A[0][2]=b[1];
A[0][6]=b[2];
A[0][25]=b[3];


int d=A[0][3]*2;

cout<<d;
}

Thanks for your help!

Matt
 
V

Victor Bazarov

Matt said:
I'm trying to write a program to map the contents of compressed row
storage back to the original indices. What I envision is some program
that given F(i,j) will return the correct column entry for a give row
i..
Consider this example:

int c[]={0,10,31,512,12521};

I would like to somehow point to this data by defining a double
pointer:
int **b;

b=new int*[num_of_rows];

Then let each b represent a row...

finally have the various entries point to the real array data:

b[0][0]=c[0];
b[0][10]=c[1];
b[0][31]=c[2];
b[0][512]=c[3];
b[0][12521]=c[4];

thus storing only the values I need, rather than wasting enormous
amount of space by creating a 2d int array filled 95%+ with zeros.

Is this even possible? I've tried the following unsuccessfully, which
ironically compiles fine with the g++ but segfaults immediately...

#include <iostream>
using namespace std;


int main(){
int **A;
int *b;
b=new int[4];
b[0]=0;
b[1]=2;
b[2]=6;
b[3]=25;
int g=2;
A=new int* [25];//assume there are 25 rows...
A[0][0]=b[0];
A[0][2]=b[1];
A[0][6]=b[2];
A[0][25]=b[3];


int d=A[0][3]*2;

cout<<d;
}


Well, it's possible. You're still doing too much work (maintaining
the dynamic memory yourself) and also wasting some space (when you
need only four values you still allocate 25 (actually you need 26
since the last one has the index 25).

You would be better off by using 'std::map':

std::map<int, std::map<int, int> > A;
A[0][0] = b[0];
A[25][17] = b[1];
A[2222][5555] = b[2];

V
 
A

arunkg999

The error is because you are allocating memory only for the first
dimension.
what you need to do is loop around the second dimension to allocate
memory.
rest will work fine.
 
A

arunkg999

The error is because you are allocating memory only for the first
dimension.
what you need to do is loop around the second dimension to allocate
memory.
rest will work fine.

//////////////////////////////////////////////////////////////////////
example
/////////////////////////////////////////////////////////////////////
for variable[X][Y]

variable = new int*[X];

for( i = 0; i < X + 1; ++i)
variable = new int[Y];
//////////////////////////////////////////////////////////////////////

tell me whether it worked or not
 
A

arunkg999

//////////////////////////////////////////////////////////////////////////
// Allocate Memory for variable[X][Y]
//////////////////////////////////////////////////////////////////////////

variable = new int*[X];

for( i = 0; i < X + 1; ++i)
variable = new int[Y];

//////////////////////////////////////////////////////////////////////////

sorry that i didnt added before.
look through this example.
 
V

Victor Bazarov

arunkg999 said:
//////////////////////////////////////////////////////////////////////////
// Allocate Memory for variable[X][Y]
//////////////////////////////////////////////////////////////////////////

variable = new int*[X];

for( i = 0; i < X + 1; ++i)
variable = new int[Y];


At the end you index variable[X], which is beyond the end of the allocated
array of X elements. Drop the "+ 1" from the "for" statement.
//////////////////////////////////////////////////////////////////////////

sorry that i didnt added before.
look through this example.

V
 
M

Matt Jackson

Thanks for all your replies... The stl map container did the trick!

Matt

Victor said:
arunkg999 said:
//////////////////////////////////////////////////////////////////////////
// Allocate Memory for variable[X][Y]
//////////////////////////////////////////////////////////////////////////

variable = new int*[X];

for( i = 0; i < X + 1; ++i)
variable = new int[Y];


At the end you index variable[X], which is beyond the end of the allocated
array of X elements. Drop the "+ 1" from the "for" statement.
//////////////////////////////////////////////////////////////////////////

sorry that i didnt added before.
look through this example.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top