Click here to Skip to main content
15,908,111 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
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 
I can sympathise with your problem understanding this. I find it rather tangled too.

This is a good programming lesson - just because you *can* write something one way, doesn't mean you should. Even if you work alone, always think of the next person to read your code. After all, that "next" person might be you after a few years.

The error checking of the original code is almost criminal, but if nothing fails, it's perfectly good - just really hard to understand.

Let's pretend T is an int, for ease of understanding.

Part of the headache is that you have a 2D array (int ** / int [][]) - but that is allocated by a helper function. So that helper function is passed a pointer to a 2d array, hence the third *. If you remove one star off the code, it may become simpler...

Original code:
*M = new T* [xdim]; **M = new T [xdim*ydim];
if (*M==NULL || **M==NULL) return false;
for(int i=1; i < xdim; i++)
(*M)[i] = (*M)[i-1] + ydim;


Remove one level of indirection:
void function (int xdim, int ydim)
{
   int **M   

   M = new int * [xdim]; // M now points to an array of int *'s. They point to junk at the moment.
   // M[0] is the first pointer in that array - M[1] is 2nd - but none of them point anywhere yet.
   if (M == NULL) return;   // checking error in the right place...

   //*M = new int [xdim * ydim]; is equivalent to...
   M [0] = new int [xdim * ydim];  // so, we've allocated a big lump of continuous ram, ready to be filled with ints. and M[0] points to the beginning.
   if (M[0] == NULL)
   {
      delete [] M;  // use the [], as T might be a type with a destructor
      return;
   }

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

This last one is a biiiit clever. Get some or, or a debugger, and follow it through. M is an array of pointers to ints. M[0] points to the first one in this big bunch of ints. M[1] will point to an int that is ydim later in this big bunch. M[2] will point to one ydim later than that... and so on.

I hope removing one level of indirection, and replacing *(M + n) with M[n] makes it simpler for you!

Iain.

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

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 
QuestionVS 2002: Not taking header files from Additional Include directories Pin
Shiva Prasad14-Jan-09 21:36
Shiva Prasad14-Jan-09 21:36 
AnswerRe: VS 2002: Not taking header files from Additional Include directories Pin
KarstenK14-Jan-09 23:07
mveKarstenK14-Jan-09 23:07 

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.