9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Eran Gabber eran@research.att.com
Subject: a compiler bug?
Date: Tue, 26 Mar 1996 16:34:01 -0500	[thread overview]
Message-ID: <19960326213401.lfBe5RuahTCxWs28GPAL_7nBagx3JKWy9FQpexy8yO4@z> (raw)

Hi,

I found that assigning structure displays into an array of structures 
with a post-increment index yields strange results.
The value of the index is wrong and the array has "holes".
The following C program demonstrates this behavior:

	#include <u.h>
	#include <libc.h>
	
	enum	{
		N = 10,
	};
	
	typedef	struct	{
		int p;
		int q;
	} T;
	
	static int na, nb;
	
	static T a[N], b[N];
	
	void
	main(int argc, char *argv[])
	{
		int i;
		T x;
	
		for (i = 0; i < N; i++) {
			x = (T){10+i, 20+i};
			a[na++] = x;
			b[nb++] = (T){10+i, 20+i};
		}
	
		print("na=%d nb=%d\n", na, nb);
	
		for (i = 0; i < N; i++)
			print("a[%d]: %d %d\n", i, a[i].p, a[i].q);
	
		for (i = 0; i < N; i++)
			print("b[%d]: %d %d\n", i, b[i].p, b[i].q);
	}
	
The contents of a and b should be equal, as well as na and nb.
The output is:
	
	term% 8.out
	na=10 nb=20
	a[0]: 10 20
	a[1]: 11 21
	a[2]: 12 22
	a[3]: 13 23
	a[4]: 14 24
	a[5]: 15 25
	a[6]: 16 26
	a[7]: 17 27
	a[8]: 18 28
	a[9]: 19 29
	b[0]: 10 0
	b[1]: 0 20
	b[2]: 11 0
	b[3]: 0 21
	b[4]: 12 0
	b[5]: 0 22
	b[6]: 13 0
	b[7]: 0 23
	b[8]: 14 0
	b[9]: 0 24
	
Regards,

	Eran

Eran Gabber		 			E-mail: eran@bell-labs.com
Bell Labs, Room MH 2C-234A			http://cm.bell-labs.com/is/eran
600 Mountain Avenue				Phone:	1-908-582-4354
Murray Hill, NJ 07974				Fax:	1-908-582-5809
	








             reply	other threads:[~1996-03-26 21:34 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-03-26 21:34 Eran [this message]
1996-03-27  6:27 forsyth
1996-03-27  6:56 forsyth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19960326213401.lfBe5RuahTCxWs28GPAL_7nBagx3JKWy9FQpexy8yO4@z \
    --to=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).