www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/07/06/07:30:47

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Arbitrary variable order ?
Date: 6 Jul 2000 11:18:27 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 53
Message-ID: <8k1pu3$aui$1@nets3.rz.RWTH-Aachen.DE>
References: <396433B4 DOT 4370ADC2 AT oeaw DOT ac DOT at>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 962882307 11218 137.226.32.75 (6 Jul 2000 11:18:27 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 6 Jul 2000 11:18:27 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Harald Jeszenszky <harald DOT jeszenszky AT oeaw DOT ac DOT at> wrote:

> 	...
> 	int copy_start;
> 	int variable_1;
> 	...
> 	int variable_n;
> 	int copy_end;
> 	...

> The statement

> 	memcpy(buffer, &copy_start, &copy_end - &copy_start - 1);

> should copy the contents of variable_1 to variable_n to the buffer. This
> works fine on the old platform but fails with DJGPP.

No surprise, there. If that hack ever worked on some other platform,
that was by luck, not by design. There is no guarantee whatsoever in
the definition of the C programming language, or in any compiler
documentation I've seen, that this kind of variable ordering must stay
intact. To be precise: the result of pointer subtraction is undefined
if the two pointers do not point into one and the same 'object'
(array, structure, or other monolithic datastructure).

If such conditions were to be asserted, optimizing C compilers would
essentially become extremely hard, if not impossible to create.
Actually, if that earlier platform really allowed this reliably, it
must have have had an extremely silly non-optimizing compiler, IMHO.

> In the DJGPP version the variables are stored in arbitrary order in 
> memory !!! The address of the variable copy_end was less than the
> address of the variable copy_start !!!

No need to shout out in surprise ('!!!'). Actually, you may consider
yourself lucky that the compiler didn't optimize some of the variable
away completely, or used the same piece of storage for two variables
that were found to have non-overlapping lifetimes.

> Is there a way to force the compiler to map the variables in memory in
> the same order as they are declared?

Not for variables declared independently of each other. You really
should move them into a struct. After all, that's what data structures
were originally invented for: to keep in one place what belongs
together. It'd even get you rid of that memcpy() call: you could
replace it by structure assignment.

Or, if you really have to insist on keeping the source code unchanged,
use 'gcc -O0' mode.
-- 
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019