Click here to Skip to main content
16,006,378 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: help on dynamic array (matix) Pin
CPallini15-Jan-09 1:50
mveCPallini15-Jan-09 1:50 
GeneralRe: help on dynamic array (matix) Pin
Cedric Moonen15-Jan-09 2:43
Cedric Moonen15-Jan-09 2:43 
GeneralRe: help on dynamic array (matix) Pin
CPallini15-Jan-09 2:57
mveCPallini15-Jan-09 2:57 
GeneralRe: help on dynamic array (matix) Pin
Cedric Moonen15-Jan-09 3:00
Cedric Moonen15-Jan-09 3:00 
GeneralRe: help on dynamic array (matix) Pin
CPallini15-Jan-09 3:31
mveCPallini15-Jan-09 3:31 
GeneralRe: help on dynamic array (matix) Pin
Randor 15-Jan-09 3:53
professional Randor 15-Jan-09 3:53 
GeneralRe: help on dynamic array (matix) Pin
Iain Clarke, Warrior Programmer15-Jan-09 6:16
Iain Clarke, Warrior Programmer15-Jan-09 6:16 
AnswerRe: help on dynamic array (matix) Pin
Iain Clarke, Warrior Programmer14-Jan-09 23:53
Iain Clarke, Warrior Programmer14-Jan-09 23:53 
You're missing a vital stage.

How is M declared?

Let's rearrange the layout. (ps - you should already have done this. The guidelines are there for a REASON, dammit)

*M = new T* [xdim];

The first line sets the place that M points to to be an array of pointers to type T. So, M is probably a T***. Yikes...
**M = new T [xdim*ydim];

We will now make the first of those array elements point to a big NxM chunk of T.

But what if the first allocation fails? Then the second allocation will dereference NULL and crash - if you're lucky.

if (*M==NULL || **M==NULL)
   return false;

Now we test for success - but it's already too late if one has failed.

for(int i=1; i < xdim; i++)
  (*M)[i] = (*M)[i-1] + ydim;

The first array pointer points to the start of this chunk of Ts - so we want the second one to point to the same place as the first - but offset by ydim.

So, (*M)[i][j] will give you a two dimensional array. I'm putting my psychic hat on, and guessing this is in a function called like:
M **My2DArray;
if (!AllocateUsingTooMuchCleverness (&M))
   return ....


I just really hope whoever wrote it provides a nice function for tidying up afterwards, as that's not intuitive either. It should be along the lines of:
delete [] **M;
delete [] *M;


As this must be C++, this could have been done sooooooo much better. It has one bug in it, and is over complex and "clever", even by my pointer-fanboy standards.

This sort of thing should really be in a C2DMatrix class, and written a lot less fragile-ly. You could then extend the class to act on sparse matrices, and so on.

Iain.

Codeproject MVP for C++, I can't believe it's for my lounge posts...

GeneralRe: help on dynamic array (matix) Pin
Member 465588515-Jan-09 6:01
Member 465588515-Jan-09 6:01 
GeneralRe: help on dynamic array (matix) Pin
Iain Clarke, Warrior Programmer15-Jan-09 6:35
Iain Clarke, Warrior Programmer15-Jan-09 6:35 
GeneralRe: help on dynamic array (matix) Pin
CPallini15-Jan-09 11:43
mveCPallini15-Jan-09 11:43 
QuestionJIT Debugging failed with the following error: 0x80040003 Pin
Anandi.VC14-Jan-09 22:55
Anandi.VC14-Jan-09 22:55 
QuestionCStdio writestring failed while writing chinese text into a file Pin
krishnakumartm14-Jan-09 22:37
krishnakumartm14-Jan-09 22:37 
QuestionRe: CStdio writestring failed while writing chinese text into a file Pin
«_Superman_»14-Jan-09 22:50
professional«_Superman_»14-Jan-09 22:50 
AnswerRe: CStdio writestring failed while writing chinese text into a file Pin
Nishad S14-Jan-09 22:51
Nishad S14-Jan-09 22:51 
AnswerRe: CStdio writestring failed while writing chinese text into a file Pin
Hamid_RT15-Jan-09 1:55
Hamid_RT15-Jan-09 1:55 
AnswerRe: CStdio writestring failed while writing chinese text into a file Pin
David Crow15-Jan-09 2:53
David Crow15-Jan-09 2:53 
Questionhelp Pin
kir_MFC14-Jan-09 21:37
kir_MFC14-Jan-09 21:37 
AnswerRe: help Pin
sam_psycho14-Jan-09 21:48
sam_psycho14-Jan-09 21:48 
AnswerRe: help [modified] Pin
Emilio Garavaglia14-Jan-09 21:54
Emilio Garavaglia14-Jan-09 21:54 
AnswerRe: help Pin
Iain Clarke, Warrior Programmer15-Jan-09 0:04
Iain Clarke, Warrior Programmer15-Jan-09 0:04 
JokeRe: help Pin
Hamid_RT15-Jan-09 1:44
Hamid_RT15-Jan-09 1:44 
GeneralRe: help Pin
Iain Clarke, Warrior Programmer15-Jan-09 1:54
Iain Clarke, Warrior Programmer15-Jan-09 1:54 
GeneralRe: help Pin
CPallini15-Jan-09 2:14
mveCPallini15-Jan-09 2:14 
AnswerRe: help Pin
David Crow15-Jan-09 2:58
David Crow15-Jan-09 2:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.