Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > possibly undefined operation

Reply
Thread Tools

possibly undefined operation

 
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-31-2010
On Thu, 2010-12-30, Ike Naar wrote:
> On 2010-12-30, luser- -droog <> wrote:
>> Rewriting with a pointer makes it all better.

>
> Yes.
>
>> /* copy elements */
>> {
>> int n;
>> char *t;
>> n = tos-stac;
>> t = stac;
>> while (n--)
>> *tos++ = *t++;
>> }

>
> Why not use memcpy from <string.h>, and replace the whole thing by
>
> memcpy(tos, stac, tos-stac);


I'm too lazy to read the whole original code, but remember that with
memcpy() the source and destination regions may not overlap.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      12-31-2010
On 2010-12-31, Jorgen Grahn <grahn+> wrote:
> On Thu, 2010-12-30, Ike Naar wrote:
>> memcpy(tos, stac, tos-stac);

>
> I'm too lazy to read the whole original code, but remember that with
> memcpy() the source and destination regions may not overlap.


If you're too lazy to read the original code, then it's sufficient
to look at the mempcy call, from which you can deduce that source
and destination are adjacent and do not overlap.
(the operation only makes sense if you assume that stac and tos
are valid pointers into the same array and stac<=tos; these
assumptions hold in the original code).
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      01-02-2011
On Fri, 2010-12-31, Ike Naar wrote:
> On 2010-12-31, Jorgen Grahn <grahn+> wrote:
>> On Thu, 2010-12-30, Ike Naar wrote:
>>> memcpy(tos, stac, tos-stac);

>>
>> I'm too lazy to read the whole original code, but remember that with
>> memcpy() the source and destination regions may not overlap.

>
> If you're too lazy to read the original code, then it's sufficient
> to look at the mempcy call, from which you can deduce that source
> and destination are adjacent and do not overlap.
> (the operation only makes sense if you assume that stac and tos
> are valid pointers into the same array and stac<=tos; these
> assumptions hold in the original code).


Well, I was too lazy to realize that too, but you're right of course.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boolean operation and arithmetic operation Buzz Lightyear C++ 10 08-12-2009 01:27 PM
possibly undefined behavior Mark C Programming 44 06-22-2009 02:59 AM
I/O operation, file operation behaviou raan C++ 2 08-16-2007 07:13 PM
Does bit operation always work more efficiently than math operation? david ullua C Programming 13 03-01-2006 11:02 PM
Reference to possibly undefined variable Chris Beall Javascript 4 03-01-2005 10:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57