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

question on _strrev

when using _strrev to reverse a string, does it actually go through and
pick up each character in turn and put it to the new position, or does it do
some clever pointer arithmetic meaning it doesn't take longer? the longer
the string is? I'm afraid I have no knowledge of machine code so am unable
to determine this by stepping through it.
Nov 17 '05 #1
8 1450
_strrev has time complexity O(n). There is no way this can be written to be
O(1) with conventional architectures.

Ronald Laeremans
Visual C++ team

"songie D" <so****@d.com> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
when using _strrev to reverse a string, does it actually go through and
pick up each character in turn and put it to the new position, or does it
do
some clever pointer arithmetic meaning it doesn't take longer? the longer
the string is? I'm afraid I have no knowledge of machine code so am unable
to determine this by stepping through it.

Nov 17 '05 #2
songie D wrote:
when using _strrev to reverse a string, does it actually go through and
pick up each character in turn and put it to the new position, or does it do
some clever pointer arithmetic meaning it doesn't take longer? the longer
the string is? I'm afraid I have no knowledge of machine code so am unable
to determine this by stepping through it.


Well, you ought to have the CRT source code, and I'd bet the function is
expressed in C there. The usual approach goes something like this:

if (length < 2)
return;

char* p = ptr_to_first_char;
char* q = ptr_to_last_char;

while (p < q)
{
swap(*p, *q);
++p;
--q;
}

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #3
ok thanks...but it is O(n) and not O(n-squared) or anything?

"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
_strrev has time complexity O(n). There is no way this can be written to be O(1) with conventional architectures.

Ronald Laeremans
Visual C++ team

"songie D" <so****@d.com> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
when using _strrev to reverse a string, does it actually go through and pick up each character in turn and put it to the new position, or does it do
some clever pointer arithmetic meaning it doesn't take longer? the longer the string is? I'm afraid I have no knowledge of machine code so am unable to determine this by stepping through it.


Nov 17 '05 #4
that simple eh. nice

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:f2********************************@4ax.com...
songie D wrote:
when using _strrev to reverse a string, does it actually go through andpick up each character in turn and put it to the new position, or does it dosome clever pointer arithmetic meaning it doesn't take longer? the longer
the string is? I'm afraid I have no knowledge of machine code so am unableto determine this by stepping through it.


Well, you ought to have the CRT source code, and I'd bet the function is
expressed in C there. The usual approach goes something like this:

if (length < 2)
return;

char* p = ptr_to_first_char;
char* q = ptr_to_last_char;

while (p < q)
{
swap(*p, *q);
++p;
--q;
}

--
Doug Harrison
Microsoft MVP - Visual C++

Nov 17 '05 #5
In article <uH**************@TK2MSFTNGP10.phx.gbl>, so****@D.com
says...
that simple eh. nice


Usually it's shorter, though one statement is arguably more complex:

while (p<q)
swap(*p++, *q--);

--
Later,
Jerry.

The universe is a figment of its own imagination.
Nov 17 '05 #6
That isn't shorter when it comes down the assembly and machine language.
:-)

It'll compile the same as the previous three line will.

"Jerry Coffin" <jc*****@taeus.us> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <uH**************@TK2MSFTNGP10.phx.gbl>, so****@D.com
says...
that simple eh. nice


Usually it's shorter, though one statement is arguably more complex:

while (p<q)
swap(*p++, *q--);

--
Later,
Jerry.

The universe is a figment of its own imagination.

Nov 17 '05 #7
In article <IO********************@comcast.com>, m@msn.com says...
That isn't shorter when it comes down the assembly and machine language.
:-)

It'll compile the same as the previous three line will.


That depends on the compiler, optimization, etc. Just for example,
with VC++ 7.1 with optimization turned off, the one I posted is
minutely shorter and more efficient. Even minimal optimization will
usually make the difference disappear though.

I'd also note that as posted, both contained one potential problem --
the call to swap passed *p and *q as the arguments. In C++, this
could be made to work by using pass-by-reference. In C, you'd have
to pass p and q as the parameters instead.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Nov 17 '05 #8
Yes, it is O(n). Which I think you have seen from the other message in the
thread.

Ronald

"songie D" <so****@D.com> wrote in message
news:uT**************@tk2msftngp13.phx.gbl...
ok thanks...but it is O(n) and not O(n-squared) or anything?

"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
_strrev has time complexity O(n). There is no way this can be written to

be
O(1) with conventional architectures.

Ronald Laeremans
Visual C++ team

"songie D" <so****@d.com> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
> when using _strrev to reverse a string, does it actually go through and > pick up each character in turn and put it to the new position, or does it > do
> some clever pointer arithmetic meaning it doesn't take longer? the longer > the string is? I'm afraid I have no knowledge of machine code so am unable > to determine this by stepping through it.
>
>



Nov 17 '05 #9

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

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.