public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: optimization/5159: ptr[i] = ptr[++i]; // works differently with malloc() and -O
@ 2001-12-18 15:22 neil
  0 siblings, 0 replies; 2+ messages in thread
From: neil @ 2001-12-18 15:22 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, nobody, robc

Synopsis: ptr[i] = ptr[++i]; // works differently with malloc() and -O

State-Changed-From-To: open->closed
State-Changed-By: neil
State-Changed-When: Tue Dec 18 15:22:36 2001
State-Changed-Why:
    Not a bug - you should read up on sequence points.  Basically, multiple reads of a variable (i) modified between sequence points is undefined.  Your code falls into this category.  So,
    
    ptr[i] = ptr[++i];
    
    is fundamentally always meaningless.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=5159


^ permalink raw reply	[flat|nested] 2+ messages in thread

* optimization/5159: ptr[i] = ptr[++i]; // works differently with malloc() and -O
@ 2001-12-18 14:56 robc
  0 siblings, 0 replies; 2+ messages in thread
From: robc @ 2001-12-18 14:56 UTC (permalink / raw)
  To: gcc-gnats


>Number:         5159
>Category:       optimization
>Synopsis:       ptr[i] = ptr[++i]; // works differently with malloc() and -O
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Tue Dec 18 14:56:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     RobC
>Release:        gcc version 2.95.2 19991024 (release)
>Organization:
>Environment:
Linux 2.2.19, Pentium II
>Description:
ptr[i] = ptr[++i];
when ptr is a pointer and compiled with -O then it behaves differently.
>How-To-Repeat:
compile with "gcc -O"
>Fix:
ptr[i] = ptr[i+1];
i++;
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="test.c"
Content-Disposition: inline; filename="test.c"

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i=0;
	int *pos = malloc(sizeof(int) * 2);

	pos[0] = 0;
	pos[1] = 1;
	
	pos[i] = pos[++i];

	printf("%i %i\n", pos[0], pos[1]);

	return(0);
}


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-12-18 23:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-12-18 15:22 optimization/5159: ptr[i] = ptr[++i]; // works differently with malloc() and -O neil
  -- strict thread matches above, loose matches on Subject: below --
2001-12-18 14:56 robc

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).