Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!uunet!mcvax!kth!draken!tut!tukki!tarvaine From: tarvaine@tukki.jyu.fi (Tapani Tarvainen) Newsgroups: comp.std.c Subject: pointers & order of execution Keywords: pointer subtraction, order of execution, realloc Message-ID: <921@tukki.jyu.fi> Date: 17 Jun 89 13:47:51 GMT References: <920@tukki.jyu.fi> Reply-To: tarvaine@tukki.jyu.fi (Tapani Tarvainen) Organization: University of Jyvaskyla, Finland Lines: 41 Consider the following code fragment: b = (char *) malloc(n); c = b + x; ... t = b; b = (char *) realloc(b, m); /*1*/ i = b - t; c += i; The idea is that c keeps pointing to the same thing. Is this guaranteed to work? I think not: pointer subtraction assumes the pointers point to the same structure, which b and t don't (unless pANS says something about realloc in this context?). And indeed, it may fail with Turbo C and probably any 80x86 C with large data models. (The problem came up when porting Gnu grep to ms-dos. See article <920@tukki.jyu.fi> in gnu.utils.bug for details.) Then how about this: /*2*/ c = b + (c - t); Is this guaranteed to work, or is the compiler free to rearrange it as c = (b - t) + c; even though b - t is illegal (and fails)? I know it can be done safely like this: i = c - t; c = b + i; which is what I did, but I'd like to know what pANS says about /*2*/. -- Tapani Tarvainen BitNet: tarvainen@finjyu Internet: tarvainen@jylk.jyu.fi -- OR -- tarvaine@tukki.jyu.fi