473,394 Members | 1,700 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,394 software developers and data experts.

MATRIXES - Dinamic Memory

Hi there,

Can somebody explain me how can i use matrixes with dinamic memory? I
use malloc and free (NOT new & delete). I want to access to the matrix
like Matrix[r][c] but i don't know how to do that with pointers and
malloc & free...

If I declare 4 statics matrixs of 576x720, the program brokes down (how
is it posible!?), so i must use dinamic memory (i suppose)...

Can somebody help me?


Txs a lot! ;)
Good luck for all time

Jul 23 '05 #1
3 2499

The usual way to implement [][] syntax is something like this:
class Row
{
public:

int& operator[]( int colIndex );
int operator[]( int colIndex ) const;

private:

// some collection of data (vec/deq)

};
class Matrix
{
public:

Row& operator[]( int rowIndex );
const Row& operator[]( int rowIndex ) const;

private:

// some collection of Row object (eg. std::vector or std::deque)

};

Jose Garcia wrote:
Hi there,

Can somebody explain me how can i use matrixes with dinamic memory? I
use malloc and free (NOT new & delete). I want to access to the matrix
like Matrix[r][c] but i don't know how to do that with pointers and
malloc & free...

If I declare 4 statics matrixs of 576x720, the program brokes down (how
is it posible!?), so i must use dinamic memory (i suppose)...

Can somebody help me?


Txs a lot! ;)
Good luck for all time

Jul 23 '05 #2
Jose Garcia wrote:
Can somebody explain [to] me how can I use matrixes with dinamic memory?
I use malloc and free (NOT new & delete).
I want to access to the matrix like Matrix[r][c]
but I don't know how to do that with pointers and malloc & free.

If I declare 4 static matrices of 576x720,
the program breaks down (How is it possible?)
So I must use dinamic memory (I suppose).
Try increasing the stack size limit first.
Can somebody help me?


Try using a matrix class library.
Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library

http://www.netwood.net/~edwin/svmtl/

Then, take a look at
The Object-Oriented Numerics Page

http://www.oonumerics.org/oon/
Jul 23 '05 #3
> Can somebody explain [to] me how can I use matrixes with dinamic
memory?
I use malloc and free (NOT new & delete).
I want to access to the matrix like Matrix[r][c]
but I don't know how to do that with pointers and malloc & free. If I declare 4 static matrices of 576x720,
the program breaks down (How is it possible?)
So I must use dinamic memory (I suppose).

Use a class who's operator[] returns a pointer to the type.
Example:
template < class T>
class dynamic_2d_array
{
public:
dynamic_2d_array(int row, int col):m_row(row),m_col(col),
m_data((row!=0&&col!=0)?new T[row*col]:NULL){}
dynamic_2d_array(const
dynamic_2d_array&src):m_row(src.m_row),m_col(src.m _col),
m_data((src.m_row!=0&&src.m_col!=0)?new T[src.m_row*src.m_col]:NULL){
for(int r=0;r<m_row;++r)for(int c=0;c<m_col;++c) (*this)[r][c] =
src[r][c];
}
~dynamic_2d_array(){if(m_data) delete []m_data;}
inline T* operator[](int i) {return (m_data + (m_col*i));}
inline T const*const operator[](int i) const {return (m_data +
(m_col*i));}
protected:
dynamic_2d_array& operator=(const dynamic_2d_array&);
private:
const int m_row;
const int m_col;
T* m_data;
};

For more information, check out the following link:
http://www.tek-tips.com/faqs.cfm?fid=5575

Jul 23 '05 #4

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

Similar topics

1
by: dddddd | last post by:
i need do create simple dinamic site - built mainly of pictures(they are the only dinamic part). pictuers differ by the -Location- and by -Time- (in which they were taken)... so on the index...
1
by: Roberto Dias | last post by:
I'm a newbie in C++ programming. I bought a book yet and I have learned by means internet donwloadble materials. I feel not confortable using multi-dimensional arrays. I simply cannot understand...
6
by: gp | last post by:
Hi all, I'm using Microsoft Visual C++ 6.0, I would like to see, debugging my project, all the elements of my dinamic objects.... I have a dinamic array and a STL vector and I need to know...
14
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
3
by: Jose Garcia | last post by:
Hi there, Can somebody explain me how can i use matrixes with dinamic memory? I use malloc and free (NOT new & delete). I want to access to the matrix like Matrix but i don't know how to do that...
1
by: KinGPIN | last post by:
hi ; I have a homework about CMatrix; and i had a problem ;in my homowork i hace functions and operators written for CMatrix class(this was my first homework); it is wanted to inherite the...
10
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: ...
2
by: eriwik | last post by:
I'm working on an application that performs calculations on triangles of a 3D-model. As part of those computations I have to calculate a value for each pair of triangles and the number of triangles...
4
by: ishakteyran | last post by:
hello to all.. i have a realy tough assignment which requires me to add, substract, multiply, and get inverse of non-sparse and sparse matrixes.. in a more clear way it wants me to to the...
6
by: berte | last post by:
what's wrong this code ? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> typedef struct{ char *name; int no;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.