473,378 Members | 1,140 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.

using pointers to map to data

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[i] 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
Apr 5 '06 #1
6 1767
Matt Jackson wrote:
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[i] 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
--
Please remove capital As from my address when replying by mail
Apr 6 '06 #2
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.

Apr 6 '06 #3
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[i] = new int[Y];
//////////////////////////////////////////////////////////////////////

tell me whether it worked or not

Apr 6 '06 #4
//////////////////////////////////////////////////////////////////////////
// Allocate Memory for variable[X][Y]
//////////////////////////////////////////////////////////////////////////

variable = new int*[X];

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

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

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

Apr 6 '06 #5
arunkg999 wrote:
//////////////////////////////////////////////////////////////////////////
// Allocate Memory for variable[X][Y]
//////////////////////////////////////////////////////////////////////////

variable = new int*[X];

for( i = 0; i < X + 1; ++i)
variable[i] = 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
--
Please remove capital As from my address when replying by mail
Apr 6 '06 #6
Thanks for all your replies... The stl map container did the trick!

Matt

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

variable = new int*[X];

for( i = 0; i < X + 1; ++i)
variable[i] = 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

Apr 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
3
by: Christian F | last post by:
Hi, I'm a C-newbie and I would like to know if I am doing something wrong in the code below. It is working, but I'm afraid it might not be correct because I don't really understand everything of...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: natkw1 | last post by:
Hi, I'm attempting to understand the use of pointers(at least grasp how pointers work). I've read the FAQ on http://www.eskimo.com/~scs/C-faq/s6.html on pointers and arrays but I'm still a bit...
12
by: arganx | last post by:
Before I had a good idea how to use pointers, I was wondering - first, how to use them. And second, of what value would they be. Now that I have at least a pretty good idea of how to use them, I...
4
by: Martin | last post by:
Hello, I haven't found any answer on this - if it is possible or not. I have a PCI card with a memory on it. I have also a driver (written in C++) that maps the memory from the card to user...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
10
by: kkirtac | last post by:
Hi, i have a void pointer and i cast it to an appropriate known type before using. The types which i cast this void* to are, from the Intel's open source computer vision library. Here is my piece...
69
by: Horacius ReX | last post by:
Hi, I have the following program structure: main ..... int A=5; int* ptr= A; (so at this point ptr stores address of A) ..... .....
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.