|
This is what I got
char *start = string;
char *left = string;
char ch;
while (*string++);
string -= 2;
while (left < string)
{
ch = *left;
*left++ = *string;
*string-- = ch;
}
return(start);
Now what do u have???
|
|
|
|
|
No temps in reverseString at all, and only one loop which goes halfway along the string. It is an in-place reverse, so it overwrites whats there.
If I was feeling really keen I could turn it into a template too
#include <iostream><br />
#include <cstdio><br />
<br />
using namespace std;<br />
<br />
void reverseString(char *str, const int &len)<br />
{<br />
for(int i=0; i<(len-1)-i; ++i)<br />
str[i] ^= str[(len-1)-i] ^= str[i] ^= str[(len-1)-i];<br />
}<br />
<br />
int main()<br />
{<br />
char str[] = "String to reverse!";<br />
int len = strlen(str);<br />
<br />
cout << str << endl;<br />
reverseString(str, len);<br />
cout << str << endl;<br />
return 0;<br />
}
--
Ian Darling
"The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
|
|
|
|
|
So, what's wrong with std::reverse ? The compiler's supplied library is usually pretty well optimised for the task in hand.
In the general case, forget about trying to eliminate variables, loops etc in your code. The compiler can do that when it optimises - unrolling loops, enregistering variables, removing common terms.
Compile your code in release mode for a simple implementation, then see what the compiler did with it. Shortest C++ code is not necessarily shortest or fastest object code.
So, the best optimised code is probably
void ReverseString( char* sz, size_t len )
{
char ch;
for ( size_t idx = 0; idx < len / 2; ++idx )
{
ch = sz[idx];
sz[idx] = sz[len - idx - 1];
sz[len - idx - 1] = ch;
}
} The compiler hoists the division of len by 2 out of the loop (and also converts it to a right-shift by 1). It also converts the sz[len - idx - 1] terms into a pointer and moves it back down from the end.
Finally, it uses the dl register (the least significant byte of edx to store ch , which is never stored to main memory.
Stick to simple code.
|
|
|
|
|
My guess would be this is what the lecturer is looking for too
--
The Obliterator
|
|
|
|
|
Thank you for your comments.
I agree with you that code should be simple, atleast for maintenance purposes.
|
|
|
|
|
__inline char *StrRev(char *Source, char *Dest)
{
char *p = strchr(Source, '\0'), *p2 = Dest;
while( --p >= Source )
*p2++ = *p;
*p2 = '\0';
return Dest;
};
Phil
|
|
|
|
|
Nice.
Actually the initial problem required the original string to be reversed (which i forgot to mention). I have used your nice logic to modify my function
void Reverse(char *src)<br />
{<br />
char *p = strchr(src, '\0') - 1;<br />
<br />
while (src < p)<br />
{<br />
*src ^= *p ^= *src ^= *p;<br />
src++;<br />
p--;<br />
}<br />
}
-Melwyn
|
|
|
|
|
hi,
I want to start a procedure wenn my dialog has been drawn and initialiezed. Witch Windows Message Handler do i have to use?
greetz willem
|
|
|
|
|
OnInitDialog/WM_INITDIALOG
|
|
|
|
|
no it doesn't work;
BOOL CImportProcessDlg::OnInitDialog()
{
CDialog::OnInitDialog();
for (long i = 1; i < 100000; i++)
{
m_Tekst.Format("%d", i);
UpdateData(FALSE);
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
if i run this the dialog just waits till i = 1000000 and then it pop's up... :S
|
|
|
|
|
Try this:- Add a hidden button to your dialog, with id
ID_RUN_LOOP .
- Write a handler for the
ID_RUN_LOOP command. The handler should execute the for loop.
- In your dialog's
OnInitDialog() handler, do this: PostMessage (WM_COMMAND, ID_RUN_LOOP); This should cause the loop to be executed after your dialog has been initialized and shown on the screen.
/ravi
Let's put "civil" back in "civilization"
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
Another way is to use Timers.
1. Create a timer in OnInitDialog()
2. In the timer function do the for loop and then destroy the timer.
|
|
|
|
|
I have a DLL I wish to use in my app, but I have no associated header, def, lib, etc fils... just the DLL itself.
How exactly do I work with this?
I've tried creating a definition file for it but only 4 functions are generated (DllCanUnloadNow, DllGetClassObject, DllRegisterServer & DllUnregisterServer).
I'm know the DLL provides other functions but how to get to them???
I can load the library but where to go from there? I've tried using GetProcAddress() to retrieve the address of what I think the function I want is called but without success.
Cheers all
--
The Obliterator
|
|
|
|
|
You can see the exact names of the exported functions through Dependency Walker (a tool in vc++). However, u won't be able to see the no. of function parameters, their data types etc.
|
|
|
|
|
Thanks, but that lists just the same 4.
I must be missing something really obvious!
--
The Obliterator
|
|
|
|
|
Those particular functions should be a warning sign: the DLL implements some COM objects.
You could use the OLE/COM Object Viewer (supplied with VC6) to see if it's got a type library (use File > View TypeLib). From here, you can save an IDL file.
|
|
|
|
|
Perfect! Thats what I needed...
I'm not used to these new fangled ways!
Figured out I have to #import the dll into my project and used the idl file to get to various interfaces... cheers.
Seems a very complicated way to me!
--
The Obliterator
|
|
|
|
|
Obliterator wrote:
I've tried creating a definition file for it but only 4 functions are generated
How did you do this?
|
|
|
|
|
|
Hello,
Kindly help me as soon as possible.
Problem:
When i run the "Dialog Based" Application in VC++, then the focus is set currently to the BUtton or any other control.
I want, that the focus should always be set on the "Dialog" even if we press "Button" .
e.g. 1) Run the Application.
2) When i click the button, and after clicking it, the focus should be return to the Dialog.
Hoping for a positive response
EsHbAn BaHaDuR
|
|
|
|
|
I've read a few articles about creating in VC++ 6 programmes with windows that minimize in the system tray. Is it possible to accomplish the same thing with C++ Builder?
|
|
|
|
|
Yes, you can make the same function calls to achieve the effect
|
|
|
|
|
http://www.bcbdev.com/faqs/faq73.htm
|
|
|
|
|
Hi:
I want to change the combobox's style in programme(want it could be edited or not),Can I set the "CBS_DROPDOWN " or "CBS_DROPDOWNLIST " style to the combobox in my programme?I use "modifystyleex",but nothing happen. How to do?
Thanks
Benben
|
|
|
|
|
When I was reading the CListCtrl::SortItems method, I noticed at MSDN there's a sample on how to sort items of a CListCtrl , the sample says:
Example
static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
CString strItem2 = pListCtrl->GetItemText(lParam2, 0);
return strcmp(strItem2, strItem1);
}
void snip_CListCtrl_SortItems()
{
extern CListCtrl* pmyListCtrl;
pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
}
While it says "The lParam of an item is just its index", I however found its actually the item's 32-bit data, usually 0. Why does MSDN say its an index? Is there some "hidden" way we can get the lParam's as indices? Thanks
|
|
|
|