|
Ok, but then I think you link to the wrong post of Iain: you linked to the post which is just above yours, that's why it's a bit confusing
|
|
|
|
|
Cedric Moonen wrote: you linked to the post which is just above yours
I choose thatone because it refers to his (Iain's) previous one and clearly shows how the OP missed it.
But, as already admitted, my post was clumsy...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: But, as already admitted, my post was clumsy...
But just imagine how funny it would have been if Iain edited his post and added a link back to your post. I guess it would be an 'infinite loop' reply.
Maybe you should try it next time we get a plz urgentz homework inquiry.
Best Wishes,
-David Delaune
|
|
|
|
|
Randor wrote: But just imagine how funny it would have been if Iain edited his post and added a link back to your post
The thought did occur to me...
But I'm obviously a much nicer person.
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
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...
|
|
|
|
|
Thanks for your reply much appreciated
forgive my ignorance but i can t really visualize the
**M = new T [xdim*ydim] command line
i don t understand how that make the first pointer in
the array of pointers pointer toward the [x*y], will
it not be that all arrays pointers pointers toward it?
Thanks
|
|
|
|
|
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];
if (M == NULL) return;
M [0] = new int [xdim * ydim];
if (M[0] == NULL)
{
delete [] M;
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...
|
|
|
|
|
Iain Clarke wrote: The error checking of the original code is almost criminal, but if nothing fails, it's perfectly good
You may said that for all(*) kind of wrong error checking code (even for good error checking code).
(*) I mean 'reasonably' wrong, for instance
if (!M) format("C:");
is excluded.
BTW: great answer, my 5.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
i am getting this error.my project is in VC 6.0.When i searched in msdn, i got the below explanation :
"JIT Debugging failed with the following error: 0x80040003.
This error occurs if you attempt to use Visual Studio 6.0 as the Just-In-Time debugging debugger for the desktop process, usually explorer.exe. Visual Studio 6.0 does not support the debugging of the desktop process.
To fix this problem, use Visual Studio to debug the desktop process."
Anybody please explain me the above reason in detail.
Thanks in advance,
Anandi
|
|
|
|
|
Hi,
i want to write chinese text into a text file
i am using CString WriteString methos to do this writing
But it is failed to write into text file
help me if any ideas about writing chinese text into a file
thanks in advance
----------------------------
KRISHNA KUMAR T M
|
|
|
|
|
CString class has a WriteString method ???
«_Superman_»
|
|
|
|
|
|
|
krishnakumartm wrote: help me if any ideas about writing chinese text into a file
For starters, you code show a relevant code snippet, the contents of the file, and the expected results.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
hi experts please explain me how to add items to list box in vc++
kir_MFC
|
|
|
|
|
www.functionx.com/visualc/controls/listbox.htm go to dis link u'll get everything.
|
|
|
|
|
|
Are you using MFC?
Raw windows code?
Have you tried searching MSDN for "list box"?
Search for CListBox, LB_ADDSTRING. That should help a bit.
And please read the posting guidelines. "help" is not a good subject. Every poster to this forum wants help.
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
Iain Clarke wrote: Are you using MFC?
Yes(but the complete answer is better).
Iain Clarke wrote: Have you tried searching MSDN for "list box"?
No its not need for me because I dont have time and its urgent.
Iain Clarke wrote: Search for CListBox, LB_ADDSTRING. That should help a bit.
Its not pissble because I said its not need.
Iain Clarke wrote: And please read the posting guidelines. "help" is not a good subject
Whats your proposal? (its urgent?plz?{Message Delete]?...)
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
It took me a while before I realised your post wasn't by the origal author!
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
By sending it a LB_ADDSTRING message.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Hi,
I am using VS 2002 to compile a Win32 Dll Project, and in the source code, I have referred to some include files present in various other directories, so I included them using Addtional Include directories and added them.
In the Command Line link of the project setting, it displays the added link. These paths are relative to the path of the current .vcproj file.
when i compile, it gives error,
fatal error C1083: Cannot open include file:
What could be the cause of this issue..
Kindly help..
Thanks,
|
|
|
|
|
The file wasnt foung. Check whether it isnt a typo and the file is in the search path. Right click on the include and open the file...
Greetings from Germany
|
|
|
|
|
Hallo all,
Is there any function in VC++ for listing files in selected folder?
Thanks to all
Regards
Munna
|
|
|
|