473,785 Members | 2,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a[i] = a[j]... Dangerous?

I'm a bit unclear whether a statement such as 'a[i] = a[j];' causes
undefined behavior or some other abnormalities. A statement such as
'a[i] = a[i++];' would definitely cause problems because of the double
use and side effect of 'i'. But in the former case, will the double
use of the array 'a' cause problems? Or will the right operand
('a[j]') of the assignment be evaluated first and then safely assigned
to the right operand ('a[i]')?

Thanks
Jun 30 '08
29 1360
On Jul 3, 10:37 pm, "Robbie Hatley"
<see.my.signat. ..@for.my.email .addresswrote:
I'd probably seen "memmove" in the libc header before, but using
it for that application simply never occurred to me.

And even after reading the instructions for memmove, I like my
version better, because it's more obvious what's happening:

for ( i = 0; i < 700 ; ++i ) blat[i] = blat[i+1]; // TRANSPARENT

Whereas with memmove, you can't "see under the hood":

memmove(blat, &blat[100], 700 * sizeof *blat); // OPAQUE

I even doubt if the compiled code is faster for memmove.
I'd guess it just uses a for loop. And if it does any
error checking, it could even be slower. (Though in
either case, we're talking maybe about 1 microsecond
every 20 minutes or so. Cheap.)
What other functions of standard C have you replaced with inline code?
It's bad practise, prone to errors and unproductive.
Jul 3 '08 #11

"vippstar" wrote:
Robbie Hatley wrote:
I'd probably seen "memmove" in the libc header before, but using
it for that application simply never occurred to me.

And even after reading the instructions for memmove, I like my
version better, because it's more obvious what's happening:

for ( i = 0; i < 700 ; ++i ) blat[i] = blat[i+1]; // TRANSPARENT

Whereas with memmove, you can't "see under the hood":

memmove(blat, &blat[100], 700 * sizeof *blat); // OPAQUE

I even doubt if the compiled code is faster for memmove.
I'd guess it just uses a for loop. And if it does any
error checking, it could even be slower. (Though in
either case, we're talking maybe about 1 microsecond
every 20 minutes or so. Cheap.)

What other functions of standard C have you replaced with inline
code?
Misquote. I didn't "replace" memmove with anything. As I said,
it never occured to me to use that function (at the time I wrote
the program, about 1 year ago). But even if I had known about
it at the time, I wouldn't have used it, because the hand-written
version is just as short and more transparent.
It's bad practise...
Not necessarily. If it's more clear, I'd call it GOOD practice.
... prone to errors ...
Not necessarily. If the hand-written version is simple, it's
generally no more "prone to error" than a version using a call
to a library function.

Specifically, in the "for-loop vs memmove" case you quote,
the library function is more error prone, because you have to
get three different arguments correct for it to work right.
... and unproductive ...
Nonsense. If it's more clear to the reader, then it's MORE
productive, because it takes the reader less time to understand,
and REDUCES chance of errors due to reader misunderstandin g.
(Hint: the reader may be someone other than the original author.)

Don't get me wrong, I use std library functions all the time.
But if I can do the same thing with a single, simple line of
easy-to-understand home-spun code, i'll use the home-spun
instead, because it's more TRANSPARENT:

// YES:
for ( i = 0; i < 700 ; ++i ) blat[i] = blat[i+1];

// NO:
memmove(blat, &blat[100], 700 * sizeof *blat);

I save library functions for things which can NOT be easily
handled by hand-written code, such as:

// Could do this by hand, but this is MUCH easier:
if (strcomp(str1, str2)) {......}

// This one, i don't even know if it CAN be done by hand,
// and even if it could, the home-spun version probably
// wouldn't be portable:
printf("Number of employees = %d\n", NumEmp);

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Jul 3 '08 #12
On Jul 4, 1:47 am, "Robbie Hatley"
<see.my.signat. ..@for.my.email .addresswrote:
"vippstar" wrote:
Robbie Hatley wrote:
I'd probably seen "memmove" in the libc header before, but using
it for that application simply never occurred to me.
And even after reading the instructions for memmove, I like my
version better, because it's more obvious what's happening:
for ( i = 0; i < 700 ; ++i ) blat[i] = blat[i+1]; // TRANSPARENT
Whereas with memmove, you can't "see under the hood":
memmove(blat, &blat[100], 700 * sizeof *blat); // OPAQUE
I even doubt if the compiled code is faster for memmove.
I'd guess it just uses a for loop. And if it does any
error checking, it could even be slower. (Though in
either case, we're talking maybe about 1 microsecond
every 20 minutes or so. Cheap.)
What other functions of standard C have you replaced with inline
code?

Misquote. I didn't "replace" memmove with anything. As I said,
it never occured to me to use that function (at the time I wrote
the program, about 1 year ago). But even if I had known about
it at the time, I wouldn't have used it, because the hand-written
version is just as short and more transparent.
It's bad practise...

Not necessarily. If it's more clear, I'd call it GOOD practice.
... prone to errors ...

Not necessarily. If the hand-written version is simple, it's
generally no more "prone to error" than a version using a call
to a library function.

Specifically, in the "for-loop vs memmove" case you quote,
the library function is more error prone, because you have to
get three different arguments correct for it to work right.
... and unproductive ...

Nonsense. If it's more clear to the reader, then it's MORE
productive, because it takes the reader less time to understand,
and REDUCES chance of errors due to reader misunderstandin g.
(Hint: the reader may be someone other than the original author.)

Don't get me wrong, I use std library functions all the time.
But if I can do the same thing with a single, simple line of
easy-to-understand home-spun code, i'll use the home-spun
instead, because it's more TRANSPARENT:
<snip example>

This group is for C discussions. Clearly you need to be taught the
basics of programming, which I don't think I can teach you in a post
or two. I'll ignore the rest of your replies for this particular
matter, it's likely that a flame war will come out of it without
anything important discussed.
Jul 3 '08 #13
[rolling one's own memmove code]

Robbie Hatley said:
>
If it's more clear to the reader, then it's MORE
productive, because it takes the reader less time to understand,
and REDUCES chance of errors due to reader misunderstandin g.
(Hint: the reader may be someone other than the original author.)
Most readers will find it easier to read memmove calls than hand-rolled
loops. Hint: for any code that is read by at least three people, most
readers are someone other than the original author.
Don't get me wrong, I use std library functions all the time.
But if I can do the same thing with a single, simple line of
easy-to-understand home-spun code, i'll use the home-spun
instead, because it's more TRANSPARENT:
The great thing about standard library functions is that they're easy to
understand because we've all used them a brazillion times before - so we
can see at a glance what the code does. If you replace the code with, say,
this:
>
// YES:
for ( i = 0; i < 700 ; ++i ) blat[i] = blat[i+1];
....we have to think more carefully about whether the code is right.
>
// NO:
memmove(blat, &blat[100], 700 * sizeof *blat);
But this doesn't do the same thing! Use this instead:

memmove(blat, blat + 1, 700 * sizeof *blat);
I save library functions for things which can NOT be easily
handled by hand-written code, such as:

// Could do this by hand, but this is MUCH easier:
if (strcomp(str1, str2)) {......}
The C language doesn't define a strcomp function.
// This one, i don't even know if it CAN be done by hand,
// and even if it could, the home-spun version probably
// wouldn't be portable:
printf("Number of employees = %d\n", NumEmp);
It can, and it would be. But that doesn't mean it would be *wise*.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 4 '08 #14
On 2008-07-03, Robbie Hatley <se************ **@for.my.email .addresswrote:
>
"vippstar" wrote:
>Robbie Hatley wrote:
I'd probably seen "memmove" in the libc header before, but using
it for that application simply never occurred to me.

And even after reading the instructions for memmove, I like my
version better, because it's more obvious what's happening:

for ( i = 0; i < 700 ; ++i ) blat[i] = blat[i+1]; // TRANSPARENT

Whereas with memmove, you can't "see under the hood":

memmove(blat, &blat[100], 700 * sizeof *blat); // OPAQUE

I even doubt if the compiled code is faster for memmove.
I'd guess it just uses a for loop. And if it does any
error checking, it could even be slower. (Though in
either case, we're talking maybe about 1 microsecond
every 20 minutes or so. Cheap.)

What other functions of standard C have you replaced with inline
code?

Misquote. I didn't "replace" memmove with anything. As I said,
it never occured to me to use that function (at the time I wrote
the program, about 1 year ago). But even if I had known about
it at the time, I wouldn't have used it, because the hand-written
version is just as short and more transparent.
>It's bad practise...

Not necessarily. If it's more clear, I'd call it GOOD practice.
>... prone to errors ...

Not necessarily. If the hand-written version is simple, it's
generally no more "prone to error" than a version using a call
to a library function.

Specifically, in the "for-loop vs memmove" case you quote,
the library function is more error prone, because you have to
get three different arguments correct for it to work right.
>... and unproductive ...

Nonsense. If it's more clear to the reader, then it's MORE
productive, because it takes the reader less time to understand,
and REDUCES chance of errors due to reader misunderstandin g.
(Hint: the reader may be someone other than the original author.)

Don't get me wrong, I use std library functions all the time.
But if I can do the same thing with a single, simple line of
easy-to-understand home-spun code, i'll use the home-spun
instead, because it's more TRANSPARENT:
Transparency is a desireable property in computer science, when
it refers to a lack of unwanted interference in data communication,
or storage access.

The kind of transparency you are talking about here simply
refers to an inline expansion of something that is abstract
to one of its possible implementation.

All you are doing is burdening the reader with more responsibility.
// YES:
for ( i = 0; i < 700 ; ++i ) blat[i] = blat[i+1];
If we include the declaration of i, this is a 19 node
abstract syntax tree.

Note that we ca make a small change to the above such that
its meaning radically changes.

for ( i = 0; i < 700 ; ++i ) blat[i] += blat[i+1];

Now it's no longer doing a memmove. So you see the reader has
to analyze the code carefully; or else such a little
detail can easily be missed.
// NO:
memmove(blat, &blat[100], 700 * sizeof *blat);
This is a 10 node abstract syntax tree. Only 9 if we change
&blat[100] to blat + 100.

You're saying it's okay to bloat up code by a factor of 1.9
to avoid using a library function.

And that's just the increase in raw synactic complexity.
There is an increase in semantic complexity also.

The meaning of the code is ``copy this much data from here
to there''. The memcpy call simply states that meaning!
It's a primitive, well-understood operation.

The expanded algorithm does /not/ just state that meaning;
the meaning emerges from the relationship of the individual
steps of the algorithm, as it unfolds in time.
I save library functions for things which can NOT be easily
handled by hand-written code, such as:

// Could do this by hand, but this is MUCH easier:
if (strcomp(str1, str2)) {......}
The function is called strcmp, not strcomp. (Are you sure you use it?)

The above code can be inlined with a syntactic bloat of over 3.

This, of course, is worse than 1.9, but not much:

// 5 node syntax tree

if (strcmp(s1, s2)) statement;

// 16 node syntax tree

while (*s1 && *s2)
if (*s1++ != *s2++) {
statement;
break;
}

The professional programmer won't tolerate any increase in syntactic
complexity for the sake of avoiding the use of a standard library function.
The exception would be when he is optimizing.

Jul 6 '08 #15
Kaz Kylheku wrote:
) The function is called strcmp, not strcomp. (Are you sure you use it?)

Perhaps he's speaking of some non-standard function that compares
case-insensitively (similar to the also non-standard strcasecmp)

) The professional programmer won't tolerate any increase in syntactic
) complexity for the sake of avoiding the use of a standard library function.
) The exception would be when he is optimizing.

In which case he should (IMO) keep the library function call that he's
replacing in a comment.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jul 7 '08 #16

Who let this troll in here?

"<vi******@gmai l.com>" spewed:
This group is for C discussions. Clearly you need to be taught the
basics of programming, which I don't think I can teach you in a post
or two. I'll ignore the rest of your replies for this particular
matter, it's likely that a flame war will come out of it without
anything important discussed.
On Usenet, I only talk to people who are capable of cordial,
on-topic, non-ad-hominem conversations. Clearly, you are not.

::: killfiles vi******@gmail. com :::

Buh-bye.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant

Jul 7 '08 #17

Richard Heathfield wrote:
Robbie Hatley said:

// YES:
for ( i = 0; i < 700 ; ++i ) blat[i] = blat[i+1];

...we have to think more carefully about whether the code
is right....
Typo. Scroll up a couple of messages, and you'll see it
started out like this:

for ( i = 0; i < 700 ; ++i ) blat[i] = blat[i+100];

Dunno how the two zeros got cut out.
// NO:
memmove(blat, &blat[100], 700 * sizeof *blat);

But this doesn't do the same thing!
It does, if you put the zeros back.
Use this instead:

memmove(blat, blat + 1, 700 * sizeof *blat);
No, a correct version would be:

memmove(blat, blat + 100, 700 * sizeof *blat);

But I MUCH prefer:

for ( i = 0 ; i < 700 ; ++i ) blat[i] = blat[i+100];

Because it says EXACTLY what it does, and EXACTLY how
it does it, in 52 characters, 14 of which are whitespace.

Whereas, memmove is an opaque "black box".
The C language doesn't define a strcomp function.
Typo. strcmp. Don't be silly; you can intuit that.
// This one, i don't even know if it CAN be done by hand,
// and even if it could, the home-spun version probably
// wouldn't be portable:
printf("Number of employees = %d\n", NumEmp);

It can, and it would be. But that doesn't mean it would
be *wise*.
Of course it's not wise. I never said it was. In fact,
I said the opposite. There are times and places for
library functions. Printing things is one of those.
But in my opinion, shifting some data 100 bytes to the
left is not.

--
for (i = 0; i < 700; ++i) blat[i] = blat[i+100]; /* :-P */
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Jul 7 '08 #18

"Kaz Kylheku" <kk******@gmail .comwrote:
The kind of transparency you are talking about here simply
refers to an inline expansion of something that is abstract
to one of its possible implementation.
Misquote. I never "expanded" memmove. memmove was never in
the program to begin with! You really do need to READ what
you're replying to, before replying to it.

The for loop in question wasn't "one possible implimentation
of memmove"; it wan't an attempt to impliment memmove at all!
It was written for one specific application in which I needed
to shift some data in a "partial-overwrite" fashion, while
insuring the following:

1. That the over-write destroys ONLY the oldest 100 bytes.
2. That the exact method was as I desired.
3. That the method be transparent to the reader.

A simple 1-line for loop, easily understandable by even
a newbie at C, served admirably.
All you are doing is burdening the reader with more
responsibility.
No. Library functions require looking-up syntax and
semantics, whereas for loops and pointers are part of the
basic language. So in those rare cases where the hand-
written version is simpler (clearer to the reader) than
a library function call, I tend to write it by hand.
... the reader has to analyze the code carefully; or else
such a little detail can easily be missed...
Always true when reading code, REGARDLESS of whether library
functions are used or not.
... This is a 10 node abstract syntax tree....
I think you're over-technicalizing a simple thing.
I just go by what WORKS, and what is CLEAR, "abstract
syntax trees" not withstanding.
The expanded algorithm does /not/ just state that meaning;
the meaning emerges from the relationship of the individual
steps of the algorithm, as it unfolds in time.
Some don't grok C. C'est la vie. If my boss hires someone
who can't understand for loops, that's his loss, not mine.
The professional programmer won't tolerate any increase in
syntactic complexity for the sake of avoiding the use of
a standard library function. The exception would be when
he is optimizing.
A mostly-true platitude. But like all platitudes, it has
it's limits of applicability, and if over-relied-on, can
be as harmful as it is helpful.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Jul 8 '08 #19

"Willem" <wi****@stack.n lwrote:
... he [Robbie Hatley] should (IMO) keep the library function
call that he's replacing in a comment....
Misquote. I never replaced any library function call. I wrote
the code in question from scratch, electing not to use any
library function calls.

Where are people get this "replaced a library function call"
stuff from? I never said that anywhere! I believe it was the
person calling hirself "vipstarr" who said I "replaced a library
function call", not me. I've placed that person in my killfile
due to obnoxiousness, trolling, and apparently-on-purpose misquotes.
Maybe this person is the finest fellow in the world and is just in
a bad mood today, but on Usenet, I can only judge by what I read.

This is the danger of replying to a reply to a reply to a reply
to a reply to a reply to something an OP said. Gets confusing,
doesn't it? You end up misquoting and misunderstandin g things.
Hence, best not to do that.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Jul 8 '08 #20

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

Similar topics

101
3394
by: Bill Cunningham | last post by:
I read an article in a book about Perl and Common Gateway Interface and it mentioned C. It said that C could damage your computer. I don't know wether it meant the standard or compiler issuses. I was a little upset. Well more upset. I sent Dennis Ritchie and email. I don't know if he'll respond if he gets it. Sometimes he does sometimes not. How can C damage your computer? Bill
1
2836
by: b83503104 | last post by:
When are they not consistent?
4
1302
by: cesark | last post by:
Hi ! I have important doubts about how to handle the security in asp.net vb.net web forms. Somebody can help me? 1. If you have setting ‘validateRequest=true’ in .net framework1.1, What can do you do to improve the security? Because although you have validations on server side you can enter dangerous characters in a text field, with the exception of telephone numbers or similar.
302
18618
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program flow can be altered by giving some specific calculated inputs to gets()? How could anyone do so once the executable binary have been generated? I have heard many of the security problems and other bugs are due to array overflows.
6
7467
by: Brendan | last post by:
Hi, I'm trying to mimic the IPC/messaging system of an specific OS in a portable way by using GCC's library. The IPC system uses buffered asynchronous messages, where any thread can send a message to any other thread (i.e. to the "threadID") without blocking, and the receiver does any security checks necessary. I'm trying to implement the portable/linux version on top of sockets/datagrams ("SOCK_DGRAM" in the local namespace), and so...
10
9363
by: lovecreatesbea... | last post by:
C stops the conversion from (char **) to (const char **). c-faq.com sec 11.10 has explanation on this point. But, for example, even the conversion from (char *) to (const char *) brings the same dangerous as in the previous conversion. Why the latter simple but dangerous one is allowed in C? $ cat f1.c int main(void) { const char c = 'a';
6
3578
by: Thomas.li | last post by:
Hi, I want to convert CString to LPBYTE like LPBYTE lpByte = (BYTE*)(LPCTSTR)cstring; is it very dangerous to do that?
233
8706
by: Julian | last post by:
'evening. I'm not new to C and have been programming in it since I was 8 but here's a strange problem I've never seen before. When I compile a program from our C course with a windows compiler there is no problem but when I try to compile it with a linux compiler it complains that a_03.c:(.text+0x4d): warning: the `gets' function is dangerous
0
9645
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
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
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...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.