473,698 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Double increment question.

Hello, group. I've been doing too much C++ programming lately, and
I'm starting to become rusty at some aspects of the C way of doing
things, esp. efficient low-level data copies.

Specificially, I just wrote the following, but I don't know if this
is safe:

void Func(char* left, char* right)
{
chat temp_left [17] = {'\0'};
chat temp_right [17] = {'\0'};
int i;
char *ptr1, *ptr2;

/* ... do some stuff ... */

/* Copy the left part to temp_left: */
ptr1 = temp_left;
ptr2 = left;
while (ptr2 < right) *ptr1++ = *ptr2++; // WILL THIS WORK???

/* ... do some other stuff ... */

return;
}

I'm pretty sure that's a conceptually sound way of copying the characters
starting at address "left", up-to-but-not-including address "right",
to array "temp_left" .

But my question is, will I get into trouble with that double increment?
Is it guaranteed that (*ptr2) will always get assigned to (*ptr1) before
either increment occurs???

Sorry if this seems like a dorky question. I'm rusty.

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
home dot pac bell dot net slant earnur slant
Jul 25 '06
45 4099
av wrote:
On Wed, 26 Jul 2006 13:39:24 +0100, Chris Dollin wrote:
>>av wrote:
>>> "We follow two rules in the matter of optimization:
Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet."
-- M.A. Jackson

these rules are wrong

Why do you think that?

Address what needs to be addressed when it needs to be addressed.

("Not optimising" doesn't mean "write stupidly slow code".)

i have a routine R1 that with input A has output B that follow the
algo C.
optimisation is
search a routine R2 that with input A has output B that follow an algo
H that has minimum instructions for doing calculation

are we agreeing on that definition?
Sort of. Nearish. Along those lines. (People often say "optimisati on"
and mean "improvemen t" or "dramatic improvement" rather than "can't
make it any faster".)
why i would not try to write the "H" routine?
EG:

Life is short and other things have priority.

H is not robust against requirement changes.

Making H take zero time would only improve overall performance
by 1%.

It isn't possible to write unit tests for H without destroying its
performance advantages.

The module containing R1 will anyway be thrown away next week
when the real data arrives.

H is very hard to understand. Likely someone else will break
it after you've changed projects.

H is subject to IP protection.
in my little experience errors could be in R1 too ...
and think on that routine can make to see errors in R1 too
If you want to work on making code better, as in less buggy,
work on making it better, not on making it faster.

--
Chris "TDD" Dollin
"Who do you serve, and who do you trust?" /Crusade/

Jul 27 '06 #41
On 2006-07-27, av <av@ala.awrot e:
On Wed, 26 Jul 2006 16:45:48 GMT, Andrew Poelstra wrote:
>>On 2006-07-26, av <av@ala.awrot e:
>>On Tue, 25 Jul 2006 07:58:19 GMT, Robbie Hatley wrote:
"Richard Riley" wrote:
"Robbie Hatley" <bo***********@ no.spamwrites:
>
Hello, group. I've been doing too much C++ programming lately, and
I'm starting to become rusty at some aspects of the C way of doing
things, esp. efficient low-level data copies:
while (ptr2 < right) *ptr1++ = *ptr2++; // WILL THIS WORK???
Is it guaranteed that (*ptr2) will always get assigned to (*ptr1) before
either increment occurs???
>
Yes.

Thanks.

Same as in C++ when dealing with char pointers isnt it?

If it works that way with the one, I suppose it does with the
other. However, my usual way of coping strings in C++ is:

std::string str1 ("Fred"); // make string str1 containing "Fred"
std::string str2 (str1); // make string str2 and copy str1 to str2

A bit simpler than in C. :-)

in c++ all can be more simple and secure than c

Hardly. In C you can see exactly what's happening. No black boxes, hidden
*this pointers, overridden functions, *shudder* passing by reference with
no discernable change in how you call it, and no *also shudder* overloaded
operators.

in borland c++ i can see what happen and in 100-150 routines until now
i have no problem in this direction
you sound like someone that has never tried ...
I have tried; the fact that << can mean absolutely anything one wants when
applied to an object caused me to revert to a language where thinks are
easy to understand at first blush.
>>Finally, C itself is not insecure. Just because you're a bad programmer
(and a lazy typist) does not mean that you should switch to a language
where your inadequacies are hidden. It means that you should learn your
language well. Buffer overflows are easy to avoid ("Don't use gets()!"
will prevent 90% of them), as are pretty well all of the common pitfalls.

i not agree
You're entitled to your opinion.

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 1 days
Jul 27 '06 #42
av
On Thu, 27 Jul 2006 19:23:45 GMT, Andrew Poelstra wrote:
>On 2006-07-27, av <av@ala.awrot e:
>On Wed, 26 Jul 2006 16:45:48 GMT, Andrew Poelstra wrote:
>>>On 2006-07-26, av <av@ala.awrot e:
On Tue, 25 Jul 2006 07:58:19 GMT, Robbie Hatley wrote:
>"Richard Riley" wrote:
>"Robbie Hatley" <bo***********@ no.spamwrites:
>>
Hardly. In C you can see exactly what's happening. No black boxes, hidden
*this pointers, overridden functions, *shudder* passing by reference with
no discernable change in how you call it, and no *also shudder* overloaded
operators.

in borland c++ i can see what happen and in 100-150 routines until now
i have no problem in this direction
you sound like someone that has never tried ...
I have tried; the fact that << can mean absolutely anything one wants when
applied to an object caused me to revert to a language where thinks are
easy to understand at first blush.
you can use the debug and see what happen; there is no problem if you
know assembly; the hard work of place the small string "a<<b" with the
right function name
"this_could_be_ the_lenght_of_n ame_that_<<_can _call_in_c++(a, b)"
is done from compiler (checking if type are ok too)
and i not have the need of type all that name
Jul 29 '06 #43
On 2006-07-29, av <av@ala.awrot e:
On Thu, 27 Jul 2006 19:23:45 GMT, Andrew Poelstra wrote:
>>I have tried; the fact that << can mean absolutely anything one wants when
applied to an object caused me to revert to a language where thinks are
easy to understand at first blush.

you can use the debug and see what happen; there is no problem if you
know assembly; the hard work of place the small string "a<<b" with the
right function name
"this_could_be_ the_lenght_of_n ame_that_<<_can _call_in_c++(a, b)"
is done from compiler (checking if type are ok too)
and i not have the need of type all that name
So, I should learn the assembly language of every system that I'm ever going
to use?

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 42
Jul 29 '06 #44

Richard Heathfield wrote:
Robbie Hatley said:
Hello, group. I've been doing too much C++ programming lately, and
I'm starting to become rusty at some aspects of the C way of doing
things, esp. efficient low-level data copies.

Specificially, I just wrote the following, but I don't know if this
is safe:

It isn't, but perhaps not for the reason you imagine.
void Func(char* left, char* right)
{
chat temp_left [17] = {'\0'};
chat temp_right [17] = {'\0'};
int i;
char *ptr1, *ptr2;

/* ... do some stuff ... */

/* Copy the left part to temp_left: */
ptr1 = temp_left;
ptr2 = left;
while (ptr2 < right)

This comparison compares the ptr2 pointer value with a pointer to a
completely different object. Bad idea, unless you know for sure (and how
can you?) that 'right' is actually a pointer into the same object that
'left' points to.

*ptr1++ = *ptr2++; // WILL THIS WORK???

Yes, because ptr1 and ptr2 do not at any time point to the same char.
Even if they did point to the same char, the behavior
would still be defined.

Jul 31 '06 #45
en******@yahoo. com said:
>
Richard Heathfield wrote:
>Robbie Hatley said:
<snip>
>>
*ptr1++ = *ptr2++; // WILL THIS WORK???

Yes, because ptr1 and ptr2 do not at any time point to the same char.

Even if they did point to the same char, the behavior
would still be defined.
Yes, on reflection of course you're right.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 31 '06 #46

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

Similar topics

20
2308
by: Martijn | last post by:
Hi, Those familiar with Windows programming may be familiar with the windowsx.h header. In this header macros exist that use double casts, like so (hope this is readable): #define ComboBox_LimitText(hwndCtl,cchLimit) \ ((int)(DWORD)SendMessage((hwndCtl),CB_LIMITTEXT,(WPARAM)(int)(cchLimit),0))
22
2993
by: Fred Ma | last post by:
I'm using the expression "int a = ceil( SomeDouble )". The man page says that ceil returns the smallest integer that is not less than SomeDouble, represented as a double. However, my understanding is that a double has nonuniform precision throughout its value range. Will a double always be able to exactly represent any value of type int? Could someone please point me to an explanation of how this is ensured, given that the details of...
6
7269
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math methods deal with doubles and I'm having problems with casting them back to floats. For example: (double)0.1f = 0.10000000149011612 I don't need the accuracy of doubles, but the speed of floats would be benificial, but it seems I'm either...
5
6353
by: Nonoize | last post by:
Hi all, Really dumb question but I don't know how to resolve it. Looked in help and evry book I have. I have a table where the primary key was set as an Integer and its reached over 140K worth of records and the numbering has restarted from 1. I realize now that I should have set it to double. Can someone please advise how I can save my existing records and restart the numbering from say
60
7201
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
13
6183
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is ------------------ b += (float)(mode *val); On 32 bit(intel , vs 2003, C++), some watch variables are
2
3354
by: =?Utf-8?B?Q3Jpcw==?= | last post by:
I have a string representing a very large number that I need to increment by 1. Ocurs that when I try to Convert.ToDouble("123456789012345678901234567890"), the string representation of my double gives: 1.2345678901234567E+29. But I need to full representation, without the exponent. Does anybody can help me ? Thanks.
11
3901
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
8
2086
by: bintom | last post by:
Why doe the following C++ code behaves as it does? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3 i=1; cout << (i++)++; Output: Error. LValue required i=1; cout << ++(i++); Output: Error. LValue required
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.